Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ci_key_field_spec.js « components « ci_variable_list « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bcc29f22dd1ad2b98aa7d7f6dbeedf92ff1a047c (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { mount } from '@vue/test-utils';
import { GlButton, GlFormInput } from '@gitlab/ui';
import { AWS_ACCESS_KEY_ID, AWS_DEFAULT_REGION } from '~/ci_variable_list/constants';
import CiKeyField from '~/ci_variable_list/components/ci_key_field.vue';

import {
  awsTokens,
  awsTokenList,
} from '~/ci_variable_list/components/ci_variable_autocomplete_tokens';

const doTimes = (num, fn) => {
  for (let i = 0; i < num; i += 1) {
    fn();
  }
};

describe('Ci Key field', () => {
  let wrapper;

  const createComponent = () => {
    wrapper = mount({
      data() {
        return {
          inputVal: '',
          tokens: awsTokenList,
        };
      },
      components: { CiKeyField },
      template: `
        <div>
          <ci-key-field
            v-model="inputVal"
            :token-list="tokens"
          />
        </div>
      `,
    });
  };

  const findDropdown = () => wrapper.find('#ci-variable-dropdown');
  const findDropdownOptions = () => wrapper.findAll(GlButton).wrappers.map(item => item.text());
  const findInput = () => wrapper.find(GlFormInput);
  const findInputValue = () => findInput().element.value;
  const setInput = val => findInput().setValue(val);
  const clickDown = () => findInput().trigger('keydown.down');

  afterEach(() => {
    wrapper.destroy();
  });

  describe('match and filter functionality', () => {
    beforeEach(() => {
      createComponent();
    });

    it('is closed when the input is empty', () => {
      expect(findInput().isVisible()).toBe(true);
      expect(findInputValue()).toBe('');
      expect(findDropdown().isVisible()).toBe(false);
    });

    it('is open when the input text matches a token', () => {
      setInput('AWS');
      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdown().isVisible()).toBe(true);
      });
    });

    it('shows partial matches at string start', () => {
      setInput('AWS');
      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdown().isVisible()).toBe(true);
        expect(findDropdownOptions()).toEqual(awsTokenList);
      });
    });

    it('shows partial matches mid-string', () => {
      setInput('D');
      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdown().isVisible()).toBe(true);
        expect(findDropdownOptions()).toEqual([
          awsTokens[AWS_ACCESS_KEY_ID].name,
          awsTokens[AWS_DEFAULT_REGION].name,
        ]);
      });
    });

    it('is closed when the text does not match', () => {
      setInput('elephant');
      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdown().isVisible()).toBe(false);
      });
    });
  });

  describe('keyboard navigation in dropdown', () => {
    beforeEach(() => {
      createComponent();
    });

    describe('on down arrow + enter', () => {
      it('selects the next item in the list and closes the dropdown', () => {
        setInput('AWS');
        return wrapper.vm
          .$nextTick()
          .then(() => {
            findInput().trigger('keydown.down');
            findInput().trigger('keydown.enter');
            return wrapper.vm.$nextTick();
          })
          .then(() => {
            expect(findInputValue()).toBe(awsTokenList[0]);
          });
      });

      it('loops to the top when it reaches the bottom', () => {
        setInput('AWS');
        return wrapper.vm
          .$nextTick()
          .then(() => {
            doTimes(findDropdownOptions().length + 1, clickDown);
            findInput().trigger('keydown.enter');
            return wrapper.vm.$nextTick();
          })
          .then(() => {
            expect(findInputValue()).toBe(awsTokenList[0]);
          });
      });
    });

    describe('on up arrow + enter', () => {
      it('selects the previous item in the list and closes the dropdown', () => {
        setInput('AWS');
        return wrapper.vm
          .$nextTick()
          .then(() => {
            doTimes(3, clickDown);
            findInput().trigger('keydown.up');
            findInput().trigger('keydown.enter');
            return wrapper.vm.$nextTick();
          })
          .then(() => {
            expect(findInputValue()).toBe(awsTokenList[1]);
          });
      });

      it('loops to the bottom when it reaches the top', () => {
        setInput('AWS');
        return wrapper.vm
          .$nextTick()
          .then(() => {
            findInput().trigger('keydown.down');
            findInput().trigger('keydown.up');
            findInput().trigger('keydown.enter');
            return wrapper.vm.$nextTick();
          })
          .then(() => {
            expect(findInputValue()).toBe(awsTokenList[awsTokenList.length - 1]);
          });
      });
    });

    describe('on enter with no item highlighted', () => {
      it('does not select any item and closes the dropdown', () => {
        setInput('AWS');
        return wrapper.vm
          .$nextTick()
          .then(() => {
            findInput().trigger('keydown.enter');
            return wrapper.vm.$nextTick();
          })
          .then(() => {
            expect(findInputValue()).toBe('AWS');
          });
      });
    });

    describe('on click', () => {
      it('selects the clicked item regardless of arrow highlight', () => {
        setInput('AWS');
        return wrapper.vm
          .$nextTick()
          .then(() => {
            wrapper.find(GlButton).trigger('click');
            return wrapper.vm.$nextTick();
          })
          .then(() => {
            expect(findInputValue()).toBe(awsTokenList[0]);
          });
      });
    });

    describe('on tab', () => {
      it('selects entered text, closes dropdown', () => {
        setInput('AWS');
        return wrapper.vm
          .$nextTick()
          .then(() => {
            findInput().trigger('keydown.tab');
            doTimes(2, clickDown);
            return wrapper.vm.$nextTick();
          })
          .then(() => {
            expect(findInputValue()).toBe('AWS');
            expect(findDropdown().isVisible()).toBe(false);
          });
      });
    });

    describe('on esc', () => {
      describe('when dropdown is open', () => {
        it('closes dropdown and does not select anything', () => {
          setInput('AWS');
          return wrapper.vm
            .$nextTick()
            .then(() => {
              findInput().trigger('keydown.esc');
              return wrapper.vm.$nextTick();
            })
            .then(() => {
              expect(findInputValue()).toBe('AWS');
              expect(findDropdown().isVisible()).toBe(false);
            });
        });
      });

      describe('when dropdown is closed', () => {
        it('clears the input field', () => {
          setInput('elephant');
          return wrapper.vm
            .$nextTick()
            .then(() => {
              expect(findDropdown().isVisible()).toBe(false);
              findInput().trigger('keydown.esc');
              return wrapper.vm.$nextTick();
            })
            .then(() => {
              expect(findInputValue()).toBe('');
            });
        });
      });
    });
  });
});