summaryrefslogtreecommitdiff
path: root/spec/features/issues/filtered_search/dropdown_emoji_spec.rb
blob: a5c3ab7e7d08cf424da7bf33ca21a662faa02840 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
require 'rails_helper'

describe 'Dropdown emoji', :js do
  include FilteredSearchHelpers

  let!(:project) { create(:project, :public) }
  let!(:user) { create(:user, name: 'administrator', username: 'root') }
  let!(:issue) { create(:issue, project: project) }
  let!(:award_emoji_star) { create(:award_emoji, name: 'star', user: user, awardable: issue) }
  let(:filtered_search) { find('.filtered-search') }
  let(:js_dropdown_emoji) { '#js-dropdown-my-reaction' }

  def send_keys_to_filtered_search(input)
    input.split("").each do |i|
      filtered_search.send_keys(i)
    end

    sleep 0.5
    wait_for_requests
  end

  def dropdown_emoji_size
    all('gl-emoji[data-name]').size
  end

  def click_emoji(text)
    find('#js-dropdown-my-reaction .filter-dropdown .filter-dropdown-item', text: text).click
  end

  before do
    project.add_maintainer(user)
    create_list(:award_emoji, 2, user: user, name: 'thumbsup')
    create_list(:award_emoji, 1, user: user, name: 'thumbsdown')
    create_list(:award_emoji, 3, user: user, name: 'star')
    create_list(:award_emoji, 1, user: user, name: 'tea')
  end

  context 'when user not logged in' do
    before do
      visit project_issues_path(project)
    end

    describe 'behavior' do
      it 'does not open when the search bar has my-reaction:' do
        filtered_search.set('my-reaction:')

        expect(page).not_to have_css(js_dropdown_emoji)
      end
    end
  end

  context 'when user loggged in' do
    before do
      sign_in(user)

      visit project_issues_path(project)
    end

    describe 'behavior' do
      it 'opens when the search bar has my-reaction:' do
        filtered_search.set('my-reaction:')

        expect(page).to have_css(js_dropdown_emoji, visible: true)
      end

      it 'closes when the search bar is unfocused' do
        find('body').click

        expect(page).to have_css(js_dropdown_emoji, visible: false)
      end

      it 'shows loading indicator when opened' do
        slow_requests do
          filtered_search.set('my-reaction:')

          expect(page).to have_css('#js-dropdown-my-reaction .filter-dropdown-loading', visible: true)
        end
      end

      it 'hides loading indicator when loaded' do
        send_keys_to_filtered_search('my-reaction:')

        expect(page).not_to have_css('#js-dropdown-my-reaction .filter-dropdown-loading')
      end

      it 'loads all the emojis when opened' do
        send_keys_to_filtered_search('my-reaction:')

        expect(dropdown_emoji_size).to eq(4)
      end

      it 'shows the most populated emoji at top of dropdown' do
        send_keys_to_filtered_search('my-reaction:')

        expect(first('#js-dropdown-my-reaction .filter-dropdown li')).to have_content(award_emoji_star.name)
      end
    end

    describe 'filtering' do
      before do
        filtered_search.set('my-reaction')
        send_keys_to_filtered_search(':')
      end

      it 'filters by name' do
        send_keys_to_filtered_search('up')

        expect(dropdown_emoji_size).to eq(1)
      end

      it 'filters by case insensitive name' do
        send_keys_to_filtered_search('Up')

        expect(dropdown_emoji_size).to eq(1)
      end
    end

    describe 'selecting from dropdown' do
      before do
        filtered_search.set('my-reaction')
        send_keys_to_filtered_search(':')
      end

      it 'selects `None`' do
        find('#js-dropdown-my-reaction .filter-dropdown-item', text: 'None').click

        expect(page).to have_css(js_dropdown_emoji, visible: false)
        expect_tokens([reaction_token('None', false)])
        expect_filtered_search_input_empty
      end

      it 'selects `Any`' do
        find('#js-dropdown-my-reaction .filter-dropdown-item', text: 'Any').click

        expect(page).to have_css(js_dropdown_emoji, visible: false)
        expect_tokens([reaction_token('Any', false)])
        expect_filtered_search_input_empty
      end

      it 'fills in the my-reaction name' do
        click_emoji('thumbsup')

        wait_for_requests

        expect(page).to have_css(js_dropdown_emoji, visible: false)
        expect_tokens([reaction_token('thumbsup')])
        expect_filtered_search_input_empty
      end
    end

    describe 'input has existing content' do
      it 'opens my-reaction dropdown with existing search term' do
        filtered_search.set('searchTerm my-reaction:')

        expect(page).to have_css(js_dropdown_emoji, visible: true)
      end

      it 'opens my-reaction dropdown with existing assignee' do
        filtered_search.set('assignee:@user my-reaction:')

        expect(page).to have_css(js_dropdown_emoji, visible: true)
      end

      it 'opens my-reaction dropdown with existing label' do
        filtered_search.set('label:~bug my-reaction:')

        expect(page).to have_css(js_dropdown_emoji, visible: true)
      end

      it 'opens my-reaction dropdown with existing milestone' do
        filtered_search.set('milestone:%v1.0 my-reaction:')

        expect(page).to have_css(js_dropdown_emoji, visible: true)
      end

      it 'opens my-reaction dropdown with existing my-reaction' do
        filtered_search.set('my-reaction:star my-reaction:')

        expect(page).to have_css(js_dropdown_emoji, visible: true)
      end
    end

    describe 'caching requests' do
      it 'caches requests after the first load' do
        filtered_search.set('my-reaction')
        send_keys_to_filtered_search(':')
        initial_size = dropdown_emoji_size

        expect(initial_size).to be > 0

        create_list(:award_emoji, 1, user: user, name: 'smile')
        find('.filtered-search-box .clear-search').click
        filtered_search.set('my-reaction')
        send_keys_to_filtered_search(':')

        expect(dropdown_emoji_size).to eq(initial_size)
      end
    end
  end
end