Blame view

test/unit/specs/select.spec.js 16.7 KB
a8707afa   Sergio Crisostomo   Add tests for 1807
1
  import {createVue, destroyVM, waitForIt, promissedTick} from '../util';
3b9de249   Sergio Crisostomo   Add simple unit t...
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
  
  describe('Select.vue', () => {
    let vm;
    afterEach(() => {
      destroyVM(vm);
    });
  
    describe('Props tests', () => {
      it('should create a Select component with passed placeholder', done => {
        const placeholder = 'Hi! Select something!';
        vm = createVue({
          template: `
            <Select placeholder="${placeholder}">
              <Option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          `,
          data() {
            return {
              value: '',
              options: [{value: 1, label: 'Foo'}, {value: 2, label: 'Bar'}]
            };
          }
        });
        vm.$nextTick(() => {
          const placeholderSpan = vm.$el.querySelector('.ivu-select-placeholder');
          expect(placeholderSpan.textContent).to.equal(placeholder);
          expect(placeholderSpan.style.display).to.not.equal('none');
3b9de249   Sergio Crisostomo   Add simple unit t...
29
30
31
32
33
34
          done();
        });
      });
  
      it('should create a Select component and take a pre-selected value', done => {
        vm = createVue({
afef4ce7   Sergio Crisostomo   Fix unit tests an...
35
36
37
38
39
40
41
42
43
44
45
            template: `
                <Select :value="value">
                  <Option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
             `,
            data() {
                return {
                    value: 2,
                    options: [{value: 1, label: 'Foo'}, {value: 2, label: 'Bar'}]
                };
            }
3b9de249   Sergio Crisostomo   Add simple unit t...
46
        });
afef4ce7   Sergio Crisostomo   Fix unit tests an...
47
48
49
        waitForIt(
            () => {
                const selectedValueSpan = vm.$el.querySelector('.ivu-select-selected-value');
9366c9a7   Sergio Crisostomo   Select improvemen...
50
                return selectedValueSpan && selectedValueSpan.textContent === 'Bar';
afef4ce7   Sergio Crisostomo   Fix unit tests an...
51
52
53
54
55
56
57
58
59
60
61
62
            },
            () => {
                const selectedValueSpan = vm.$el.querySelector('.ivu-select-selected-value');
                const {label, value} = vm.$children[0].values[0];
  
                expect(selectedValueSpan.textContent).to.equal('Bar');
                expect(selectedValueSpan.style.display).to.not.equal('none');
                expect(label).to.equal('Bar');
                expect(value).to.equal(2);
                done();
            }
        );
3b9de249   Sergio Crisostomo   Add simple unit t...
63
64
      });
  
a9e0f79d   Sergio Crisostomo   Use textContent t...
65
      it('should accept normal characters', done => {
3b9de249   Sergio Crisostomo   Add simple unit t...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        vm = createVue({
          template: `
            <Select :value="2">
              <Option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          `,
          data() {
            return {
              value: '',
              options: [{value: 1, label: '> 100$'}, {value: 2, label: '< 100$'}]
            };
          }
        });
        vm.$nextTick(() => {
          const selectedValueSpan = vm.$el.querySelector('.ivu-select-selected-value');
          expect(selectedValueSpan.textContent).to.equal('< 100$');
          done();
        });
      });
  
dfae43a7   Sergio Crisostomo   correct input val...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
      it('should display normal characters in input when in filterable mode', done => {
        vm = createVue({
          template: `
            <Select v-model="value" filterable>
              <Option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          `,
          data() {
            return {
              value: 2,
              options: [{value: 1, label: '> 100$'}, {value: 2, label: '< 100$'}]
            };
          }
        });
        vm.$nextTick(() => {
          const input = vm.$el.querySelector('.ivu-select-input');
          expect(input.value).to.equal('< 100$');
          done();
        });
      });
  
3b9de249   Sergio Crisostomo   Add simple unit t...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
      it('should use the value\'s label instead of placeholder when both are set', done => {
        vm = createVue({
          template: `
            <Select placeholder="Choose anything!" :value="2">
              <Option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          `,
          data() {
            return {
              value: '',
              options: [{value: 1, label: 'Foo'}, {value: 2, label: 'Bar'}]
            };
          }
        });
afef4ce7   Sergio Crisostomo   Fix unit tests an...
121
122
123
          waitForIt(
              () => {
                  const selectedValueSpan = vm.$el.querySelector('.ivu-select-selected-value');
9366c9a7   Sergio Crisostomo   Select improvemen...
124
                  return selectedValueSpan && selectedValueSpan.textContent === 'Bar';
afef4ce7   Sergio Crisostomo   Fix unit tests an...
125
126
127
128
129
130
131
132
133
134
              },
              () => {
                  const placeholderSpan = vm.$el.querySelector('.ivu-select-placeholder');
                  const selectedValueSpan = vm.$el.querySelector('.ivu-select-selected-value');
                  expect(placeholderSpan).to.equal(null);
                  expect(!!selectedValueSpan.style.display).to.not.equal('none');
                  expect(selectedValueSpan.textContent).to.equal('Bar');
                  done();
              }
          );
3b9de249   Sergio Crisostomo   Add simple unit t...
135
136
137
138
139
140
141
142
143
144
145
146
      });
  
      it('should set different classes for different sizes', done => {
        vm = createVue(`
          <div>
            <Select placeholder="Choose anything!"><Option v-for="item in []" :value="item" :key="item">{{item}}</Option></Select>
            <Select placeholder="Choose anything!" size="large"><Option v-for="item in []" :value="item" :key="item">{{item}}</Option></Select>
            <Select placeholder="Choose anything!" size="small"><Option v-for="item in []" :value="item" :key="item">{{item}}</Option></Select>
          </div>
  	  `);
        vm.$nextTick(() => {
          const [defaultSelect, largeSelect, smallSelect] = [...vm.$el.querySelectorAll('.ivu-select')];
b07521b4   梁灏   update Select uni...
147
          expect(defaultSelect.className).to.equal('ivu-select ivu-select-single ivu-select-default');
3b9de249   Sergio Crisostomo   Add simple unit t...
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
          expect(largeSelect.classList.contains('ivu-select-large')).to.equal(true);
          expect(smallSelect.classList.contains('ivu-select-small')).to.equal(true);
          done();
        });
      });
  
      it('should set new options', done => {
        const laterOptions = [{value: 1, label: 'Foo'}, {value: 2, label: 'Bar'}];
  
        vm = createVue({
          template: `
            <Select>
              <Option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          `,
          data() {
            return {
              value: '',
              options: []
            };
          },
          mounted() {
            this.$nextTick(() => (this.options = laterOptions));
          }
        });
        const condition = function() {
afef4ce7   Sergio Crisostomo   Fix unit tests an...
174
175
          const componentOptions = vm.$children[0].flatOptions;
          return componentOptions && componentOptions.length > 0;
3b9de249   Sergio Crisostomo   Add simple unit t...
176
177
        };
        const callback = function() {
3b9de249   Sergio Crisostomo   Add simple unit t...
178
179
180
181
182
183
184
185
186
187
188
189
          const renderedOptions = vm.$el.querySelectorAll('.ivu-select-dropdown-list li');
          expect(renderedOptions.length).to.equal(laterOptions.length);
  
          const labels = [...renderedOptions].map(el => el.textContent).join('<>');
          const expected = laterOptions.map(o => o.label).join('<>');
          expect(labels).to.equal(expected);
          done();
        };
        waitForIt(condition, callback);
      });
    });
  
a8707afa   Sergio Crisostomo   Add tests for 1807
190
    describe('Behavior tests', () => {
afef4ce7   Sergio Crisostomo   Fix unit tests an...
191
192
193
194
195
196
        it('should create different and independent instances', done => {
            const options = [
                {value: 'beijing', label: 'Beijing'},
                {value: 'stockholm', label: 'Stockholm'},
                {value: 'lisboa', label: 'Lisboa'}
            ];
a8707afa   Sergio Crisostomo   Add tests for 1807
197
  
afef4ce7   Sergio Crisostomo   Fix unit tests an...
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
            vm = createVue({
                template: `
        <div>
          <i-select v-model="modelA" multiple style="width:260px">
            <i-option v-for="item in cityList" :value="item.value" :key="item.value">{{ item.label }}</i-option>
          </i-select>
          <i-select v-model="modelB" multiple style="width:260px">
            <i-option v-for="item in cityList" :value="item.value" :key="item.value">{{ item.label }}</i-option>
          </i-select>
        </div>
      `,
                data() {
                    return {
                        cityList: [],
                        modelA: [],
                        modelB: []
                    };
                },
                mounted() {
                    setTimeout(() => (this.cityList = options), 200);
                }
            });
            const [SelectA, SelectB] = vm.$children;
            SelectA.toggleMenu(null, true);
            SelectB.toggleMenu(null, true);
  
            new Promise(resolve => {
                const condition = function() {
                    const optionsA = SelectA.$el.querySelectorAll('.ivu-select-item');
                    const optionsB = SelectB.$el.querySelectorAll('.ivu-select-item');
                    return optionsA.length > 0 && optionsB.length > 0;
                };
                waitForIt(condition, resolve);
            })
                .then(() => {
                    // click in A options
                    const optionsA = SelectA.$el.querySelectorAll('.ivu-select-item');
                    optionsA[0].click();
                    return promissedTick(SelectA);
                })
                .then(() => {
                    expect(SelectA.value[0]).to.equal(options[0].value);
                    expect(SelectA.value.length).to.equal(1);
                    expect(SelectB.value.length).to.equal(0);
  
                    // click in B options
                    const optionsB = SelectB.$el.querySelectorAll('.ivu-select-item');
                    optionsB[1].click();
                    optionsB[2].click();
                    return promissedTick(SelectB);
                })
                .then(() => {
                    // lets check the values!
                    const getSelections = component => {
                        const tags = component.$el.querySelectorAll('.ivu-select-selection .ivu-tag');
                        return [...tags].map(el => el.textContent.trim()).join(',');
                    };
                    const selectAValue = getSelections(SelectA);
                    const selectBValue = getSelections(SelectB);
  
                    expect(selectAValue).to.equal(options[0].label);
                    expect(selectBValue).to.equal(options.slice(1, 3).map(obj => obj.label.trim()).join(','));
  
                    done();
                }).catch(err => {
                console.log(err);
                done(false);
            });
a8707afa   Sergio Crisostomo   Add tests for 1807
266
        });
a8707afa   Sergio Crisostomo   Add tests for 1807
267
  
afef4ce7   Sergio Crisostomo   Fix unit tests an...
268
269
270
271
272
273
274
        it('should create update model with value, and label when asked', done => {
            const options = [
                {value: 'beijing', label: 'Beijing'},
                {value: 'stockholm', label: 'Stockholm'},
                {value: 'lisboa', label: 'Lisboa'}
            ];
            let onChangeValueA, onChangeValueB;
a8707afa   Sergio Crisostomo   Add tests for 1807
275
  
afef4ce7   Sergio Crisostomo   Fix unit tests an...
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  
            vm = createVue({
                template: `
                    <div>
                      <i-select v-model="modelA" style="width:260px" @on-change="onChangeA">
                        <i-option v-for="item in cityList" :value="item.value" :key="item.value">{{ item.label }}</i-option>
                      </i-select>
                      <i-select v-model="modelB" label-in-value style="width:260px" @on-change="onChangeB">
                        <i-option v-for="item in cityList" :value="item.value" :key="item.value">{{ item.label }}</i-option>
                      </i-select>
                    </div>
                  `,
                data() {
                    return {
                        cityList: options,
                        modelA: [],
                        modelB: []
                    };
                },
                methods: {
                    onChangeA(val){
                        onChangeValueA = val;
                    },
                    onChangeB(val){
                        onChangeValueB = val;
                    }
                }
            });
            const [SelectA, SelectB] = vm.$children;
            SelectA.toggleMenu(null, true);
            SelectB.toggleMenu(null, true);
  
  
            new Promise(resolve => {
                const condition = function() {
                    const optionsA = SelectA.$el.querySelectorAll('.ivu-select-item');
                    const optionsB = SelectB.$el.querySelectorAll('.ivu-select-item');
                    return optionsA.length > 0 && optionsB.length > 0;
                };
                waitForIt(condition, resolve);
            })
            .then(() => {
                // click in A options
                const optionsA = SelectA.$el.querySelectorAll('.ivu-select-item');
                optionsA[0].click();
                return promissedTick(SelectA);
            })
            .then(() => {
                expect(vm.modelA).to.equal(options[0].value);
                expect(onChangeValueA).to.equal(options[0].value);
  
                // click in B options
                const optionsB = SelectB.$el.querySelectorAll('.ivu-select-item');
                optionsB[2].click();
                return promissedTick(SelectB);
            })
            .then(() => {
                expect(vm.modelB).to.equal(options[2].value);
                expect(JSON.stringify(onChangeValueB)).to.equal(JSON.stringify(options[2]));
                done();
            });
        });
    });
  
    describe('Public API', () => {
        it('The "setQuery" method should behave as expected', (done) => {
  
            const options = [
                {value: 'beijing', label: 'Beijing'},
                {value: 'stockholm', label: 'Stockholm'},
                {value: 'lisboa', label: 'Lisboa'}
            ];
  
            vm = createVue({
                template: `
                  <Select v-model="value" filterable>
                      <Option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</Option>
                  </Select>
                  `,
                data() {
                    return {
                        value: '',
                        options: options
                    };
                }
            });
            const [Select] = vm.$children;
            Select.setQuery('i');
            vm.$nextTick(() => {
                const query = 'i';
                const input = vm.$el.querySelector('.ivu-select-input');
                expect(input.value).to.equal(query);
  
                const renderedOptions = [...vm.$el.querySelectorAll('.ivu-select-item')].map(el => el.textContent);
                const filteredOptions = options.filter(option => JSON.stringify(option).includes(query)).map(({label}) => label);
                expect(JSON.stringify(renderedOptions)).to.equal(JSON.stringify(filteredOptions));
  
                // reset query
                // setQuery(null) should clear the select
                Select.setQuery(null);
                vm.$nextTick(() => {
                    const input = vm.$el.querySelector('.ivu-select-input');
                    expect(input.value).to.equal('');
  
                    const renderedOptions = [...vm.$el.querySelectorAll('.ivu-select-item')].map(el => el.textContent);
                    expect(JSON.stringify(renderedOptions)).to.equal(JSON.stringify(options.map(({label}) => label)));
                    done();
                });
            });
  
        });
  
        it('The "clearSingleSelect" method should behave as expected', (done) => {
  
            // clearSingleSelect
            const options = [
                {value: 'beijing', label: 'Beijing'},
                {value: 'stockholm', label: 'Stockholm'},
                {value: 'lisboa', label: 'Lisboa'}
            ];
            const preSelected = 'lisboa';
  
            vm = createVue({
                template: `
                  <Select v-model="value" clearable>
                      <Option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</Option>
                  </Select>
                  `,
                data() {
                    return {
                        value: preSelected,
                        options: options
                    };
                }
            });
            const [Select] = vm.$children;
            vm.$nextTick(() => {
                expect(Select.publicValue).to.equal(preSelected);
                Select.clearSingleSelect();
                expect(typeof Select.publicValue).to.equal('undefined');
                done();
            });
        });
a8707afa   Sergio Crisostomo   Add tests for 1807
419
420
    });
  
3b9de249   Sergio Crisostomo   Add simple unit t...
421
    describe('Performance tests', () => {
9c32a056   Sergio Crisostomo   Optimize append a...
422
      it('should handle big numbers of options', done => {
3b9de249   Sergio Crisostomo   Add simple unit t...
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
        const manyLaterOptions = Array.apply(null, Array(200)).map((_, i) => {
          return {
            value: i + 1,
            label: Math.random().toString(36).slice(2).toUpperCase()
          };
        });
        const start = +new Date();
        vm = createVue({
          template: `
            <Select>
              <Option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          `,
          data() {
            return {
              value: '',
              options: []
            };
          },
          mounted() {
            this.$nextTick(() => (this.options = manyLaterOptions));
          }
        });
        const condition = function() {
afef4ce7   Sergio Crisostomo   Fix unit tests an...
447
448
          const componentOptions = vm.$children[0].flatOptions;
          return componentOptions && componentOptions.length === manyLaterOptions.length;
3b9de249   Sergio Crisostomo   Add simple unit t...
449
450
451
452
453
454
455
456
457
458
459
460
        };
        const callback = function() {
          const end = +new Date();
          const renderedOptions = vm.$el.querySelectorAll('.ivu-select-dropdown-list li');
          expect(renderedOptions.length).to.equal(manyLaterOptions.length);
          expect(end - start).to.be.not.above(1000);
          done();
        };
        waitForIt(condition, callback);
      });
    });
  });