summaryrefslogtreecommitdiff
path: root/spec/support/filtered_search_helpers.rb
blob: f3f96bd1f0a01eb97869f2eab22a3e0807cd89dc (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
module FilteredSearchHelpers
  def filtered_search
    page.find('.filtered-search')
  end

  # Enables input to be set (similar to copy and paste)
  def input_filtered_search(search_term, submit: true, extra_space: true)
    search = search_term
    if extra_space
      # Add an extra space to engage visual tokens
      search = "#{search_term} "
    end

    filtered_search.set(search)

    if submit
      # Wait for the lazy author/assignee tokens that
      # swap out the username with an avatar and name
      wait_for_requests
      filtered_search.send_keys(:enter)
    end
  end

  # Enables input to be added character by character
  def input_filtered_search_keys(search_term)
    # Add an extra space to engage visual tokens
    filtered_search.send_keys("#{search_term} ")
    filtered_search.send_keys(:enter)
  end

  def expect_filtered_search_input(input)
    expect(find('.filtered-search').value).to eq(input)
  end

  def clear_search_field
    find('.filtered-search-box .clear-search').click
  end

  def reset_filters
    clear_search_field
    filtered_search.send_keys(:enter)
  end

  def init_label_search
    filtered_search.set('label:')
    # This ensures the dropdown is shown
    expect(find('#js-dropdown-label')).not_to have_css('.filter-dropdown-loading')
  end

  def expect_filtered_search_input_empty
    expect(find('.filtered-search').value).to eq('')
  end

  # Iterates through each visual token inside
  # .tokens-container to make sure the correct names and values are rendered
  def expect_tokens(tokens)
    page.within '.filtered-search-box .tokens-container' do
      page.all(:css, '.tokens-container li .selectable').each_with_index do |el, index|
        token_name = tokens[index][:name]
        token_value = tokens[index][:value]
        token_emoji = tokens[index][:emoji_name]

        expect(el.find('.name')).to have_content(token_name)

        if token_value
          expect(el.find('.value')).to have_content(token_value)
        end

        # gl-emoji content is blank when the emoji unicode is not supported
        if token_emoji
          selector = %(gl-emoji[data-name="#{token_emoji}"])
          expect(el.find('.value')).to have_css(selector)
        end
      end
    end
  end

  def create_token(token_name, token_value = nil, symbol = nil)
    { name: token_name, value: "#{symbol}#{token_value}" }
  end

  def author_token(author_name = nil)
    create_token('Author', author_name)
  end

  def assignee_token(assignee_name = nil)
    create_token('Assignee', assignee_name)
  end

  def milestone_token(milestone_name = nil, has_symbol = true)
    symbol = has_symbol ? '%' : nil
    create_token('Milestone', milestone_name, symbol)
  end

  def label_token(label_name = nil, has_symbol = true)
    symbol = has_symbol ? '~' : nil
    create_token('Label', label_name, symbol)
  end

  def emoji_token(emoji_name = nil)
    { name: 'My-Reaction', emoji_name: emoji_name }
  end

  def default_placeholder
    'Search or filter results...'
  end

  def get_filtered_search_placeholder
    find('.filtered-search')['placeholder']
  end

  def remove_recent_searches
    execute_script('window.localStorage.clear();')
  end

  def set_recent_searches(key, input)
    execute_script("window.localStorage.setItem('#{key}', '#{input}');")
  end

  def wait_for_filtered_search(text)
    Timeout.timeout(Capybara.default_max_wait_time) do
      loop until find('.filtered-search').value.strip == text
    end
  end
end