summaryrefslogtreecommitdiff
path: root/spec/lib/event_filter_spec.rb
blob: bab48796b8cc206e6582c29f48026cea5f3cf3e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe EventFilter do
  describe '#filter' do
    it 'returns "all" if given filter is nil' do
      expect(described_class.new(nil).filter).to eq(described_class::ALL)
    end

    it 'returns "all" if given filter is ""' do
      expect(described_class.new('').filter).to eq(described_class::ALL)
    end

    it 'returns "all" if given filter is "foo"' do
      expect(described_class.new('foo').filter).to eq('all')
    end
  end

  describe '#apply_filter' do
    let_it_be(:public_project) { create(:project, :public) }
    let_it_be(:push_event)     { create(:push_event,        project: public_project) }
    let_it_be(:merged_event)   { create(:event, :merged,    project: public_project, target: public_project) }
    let_it_be(:created_event)  { create(:event, :created,   project: public_project, target: create(:issue, project: public_project)) }
    let_it_be(:updated_event)  { create(:event, :updated,   project: public_project, target: create(:issue, project: public_project)) }
    let_it_be(:closed_event)   { create(:event, :closed,    project: public_project, target: create(:issue, project: public_project)) }
    let_it_be(:reopened_event) { create(:event, :reopened,  project: public_project, target: create(:issue, project: public_project)) }
    let_it_be(:comments_event) { create(:event, :commented, project: public_project, target: public_project) }
    let_it_be(:joined_event)   { create(:event, :joined,    project: public_project, target: public_project) }
    let_it_be(:left_event)     { create(:event, :left,      project: public_project, target: public_project) }
    let_it_be(:wiki_page_event) { create(:wiki_page_event) }
    let_it_be(:wiki_page_update_event) { create(:wiki_page_event, :updated) }
    let_it_be(:design_event) { create(:design_event) }

    let(:filtered_events) { described_class.new(filter).apply_filter(Event.all) }

    context 'with the "push" filter' do
      let(:filter) { described_class::PUSH }

      it 'filters push events only' do
        expect(filtered_events).to contain_exactly(push_event)
      end
    end

    context 'with the "merged" filter' do
      let(:filter) { described_class::MERGED }

      it 'filters merged events only' do
        expect(filtered_events).to contain_exactly(merged_event)
      end
    end

    context 'with the "issue" filter' do
      let(:filter) { described_class::ISSUE }

      it 'filters issue events only' do
        expect(filtered_events).to contain_exactly(created_event, updated_event, closed_event, reopened_event)
      end
    end

    context 'with the "comments" filter' do
      let(:filter) { described_class::COMMENTS }

      it 'filters comment events only' do
        expect(filtered_events).to contain_exactly(comments_event)
      end
    end

    context 'with the "team" filter' do
      let(:filter) { described_class::TEAM }

      it 'filters team events only' do
        expect(filtered_events).to contain_exactly(joined_event, left_event)
      end
    end

    context 'with the "all" filter' do
      let(:filter) { described_class::ALL }

      it 'returns all events' do
        expect(filtered_events).to eq(Event.all)
      end
    end

    context 'with the "design" filter' do
      let(:filter) { described_class::DESIGNS }

      it 'returns only design events' do
        expect(filtered_events).to contain_exactly(design_event)
      end
    end

    context 'with the "wiki" filter' do
      let(:filter) { described_class::WIKI }

      it 'returns only wiki page events' do
        expect(filtered_events).to contain_exactly(wiki_page_event, wiki_page_update_event)
      end
    end

    context 'with an unknown filter' do
      let(:filter) { 'foo' }

      it 'returns all events' do
        expect(filtered_events).to eq(Event.all)
      end
    end

    context 'with a nil filter' do
      let(:filter) { nil }

      it 'returns all events' do
        expect(filtered_events).to eq(Event.all)
      end
    end
  end

  describe '#active?' do
    let(:event_filter) { described_class.new(described_class::TEAM) }

    it 'returns false if filter does not include the given key' do
      expect(event_filter.active?('foo')).to eq(false)
    end

    it 'returns false if the given key is nil' do
      expect(event_filter.active?(nil)).to eq(false)
    end

    it 'returns true if filter does not include the given key' do
      expect(event_filter.active?(described_class::TEAM)).to eq(true)
    end
  end
end