search_bar_spec.rb 2.83 KB
Newer Older
1 2
require 'rails_helper'

3
describe 'Search bar', :js do
4
  include FilteredSearchHelpers
5

6
  let!(:project) { create(:project) }
Clement Ho's avatar
Clement Ho committed
7
  let!(:user) { create(:user) }
8
  let(:filtered_search) { find('.filtered-search') }
9 10

  before do
11
    project.add_maintainer(user)
12
    sign_in(user)
13 14
    create(:issue, project: project)

15
    visit project_issues_path(project)
16 17
  end

18 19 20
  def get_left_style(style)
    left_style = /left:\s\d*[.]\d*px/.match(style)
    left_style.to_s.gsub('left: ', '').to_f
Clement Ho's avatar
Clement Ho committed
21 22
  end

23 24 25 26 27
  describe 'keyboard navigation' do
    it 'makes item active' do
      filtered_search.native.send_keys(:down)

      page.within '#js-dropdown-hint' do
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
28
        expect(page).to have_selector('.droplab-item-active')
29 30 31 32 33 34
      end
    end

    it 'selects item' do
      filtered_search.native.send_keys(:down, :down, :enter)

35
      expect_tokens([author_token])
36
      expect_filtered_search_input_empty
37 38 39
    end
  end

Clement Ho's avatar
Clement Ho committed
40
  describe 'clear search button' do
41 42 43 44 45
    it 'clears text' do
      search_text = 'search_text'
      filtered_search.set(search_text)

      expect(filtered_search.value).to eq(search_text)
46
      find('.filtered-search-box .clear-search').click
Clement Ho's avatar
Clement Ho committed
47

48 49 50 51 52 53 54 55 56
      expect(filtered_search.value).to eq('')
    end

    it 'hides by default' do
      expect(page).to have_css('.clear-search', visible: false)
    end

    it 'hides after clicked' do
      filtered_search.set('a')
57
      find('.filtered-search-box .clear-search').click
Clement Ho's avatar
Clement Ho committed
58

59 60 61 62 63 64
      expect(page).to have_css('.clear-search', visible: false)
    end

    it 'hides when there is no text' do
      filtered_search.set('a')
      filtered_search.set('')
Clement Ho's avatar
Clement Ho committed
65

66 67 68 69 70 71 72 73
      expect(page).to have_css('.clear-search', visible: false)
    end

    it 'shows when there is text' do
      filtered_search.set('a')

      expect(page).to have_css('.clear-search', visible: true)
    end
Clement Ho's avatar
Clement Ho committed
74 75

    it 'resets the dropdown hint filter' do
76
      filtered_search.click
Clement Ho's avatar
Clement Ho committed
77 78 79
      original_size = page.all('#js-dropdown-hint .filter-dropdown .filter-dropdown-item').size

      filtered_search.set('author')
Clement Ho's avatar
Clement Ho committed
80

Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
81
      expect(find('#js-dropdown-hint')).to have_selector('.filter-dropdown .filter-dropdown-item', count: 1)
Clement Ho's avatar
Clement Ho committed
82

83
      find('.filtered-search-box .clear-search').click
84
      filtered_search.click
Clement Ho's avatar
Clement Ho committed
85

Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
86
      expect(find('#js-dropdown-hint')).to have_selector('.filter-dropdown .filter-dropdown-item', count: original_size)
Clement Ho's avatar
Clement Ho committed
87 88 89
    end

    it 'resets the dropdown filters' do
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
90 91 92 93
      filtered_search.click

      hint_offset = get_left_style(find('#js-dropdown-hint')['style'])

Clement Ho's avatar
Clement Ho committed
94 95 96
      filtered_search.set('a')

      filtered_search.set('author:')
Clement Ho's avatar
Clement Ho committed
97

Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
98
      find('#js-dropdown-hint', visible: false)
Clement Ho's avatar
Clement Ho committed
99

100
      find('.filtered-search-box .clear-search').click
101
      filtered_search.click
Clement Ho's avatar
Clement Ho committed
102

103
      expect(find('#js-dropdown-hint')).to have_selector('.filter-dropdown .filter-dropdown-item', count: 6)
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
104
      expect(get_left_style(find('#js-dropdown-hint')['style'])).to eq(hint_offset)
Clement Ho's avatar
Clement Ho committed
105
    end
106 107
  end
end