summaryrefslogtreecommitdiff
path: root/spec/javascripts/filtered_search/filtered_search_dropdown_manager_spec.js
blob: ed0b0196ec426c2b29625f7a0d854221c5c3f96f (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
require('~/extensions/array');
require('~/filtered_search/filtered_search_tokenizer');
require('~/filtered_search/filtered_search_dropdown_manager');

(() => {
  describe('Filtered Search Dropdown Manager', () => {
    describe('addWordToInput', () => {
      function getInputValue() {
        return document.querySelector('.filtered-search').value;
      }

      function setInputValue(value) {
        document.querySelector('.filtered-search').value = value;
      }

      beforeEach(() => {
        const input = document.createElement('input');
        input.classList.add('filtered-search');
        document.body.appendChild(input);
      });

      afterEach(() => {
        document.querySelector('.filtered-search').outerHTML = '';
      });

      describe('input has no existing value', () => {
        it('should add just tokenName', () => {
          gl.FilteredSearchDropdownManager.addWordToInput('milestone');
          expect(getInputValue()).toBe('milestone:');
        });

        it('should add tokenName and tokenValue', () => {
          gl.FilteredSearchDropdownManager.addWordToInput('label', 'none');
          expect(getInputValue()).toBe('label:none ');
        });
      });

      describe('input has existing value', () => {
        it('should be able to just add tokenName', () => {
          setInputValue('a');
          gl.FilteredSearchDropdownManager.addWordToInput('author');
          expect(getInputValue()).toBe('author:');
        });

        it('should replace tokenValue', () => {
          setInputValue('author:roo');
          gl.FilteredSearchDropdownManager.addWordToInput('author', '@root');
          expect(getInputValue()).toBe('author:@root ');
        });

        it('should add tokenValues containing spaces', () => {
          setInputValue('label:~"test');
          gl.FilteredSearchDropdownManager.addWordToInput('label', '~\'"test me"\'');
          expect(getInputValue()).toBe('label:~\'"test me"\' ');
        });
      });
    });
  });
})();