Blame view

test/unit/specs/select.spec.js 9.38 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
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
  
  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');
  
          expect(vm.$children[0].showPlaceholder).to.equal(true);
          done();
        });
      });
  
      it('should create a Select component and take a pre-selected value', done => {
        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: 'Foo'}, {value: 2, label: 'Bar'}]
            };
          }
        });
        vm.$nextTick(() => {
          const selectedValueSpan = vm.$el.querySelector('.ivu-select-selected-value');
          expect(selectedValueSpan.textContent).to.equal('Bar');
          expect(selectedValueSpan.style.display).to.not.equal('none');
          expect(vm.$children[0].selectedSingle).to.equal('Bar');
          expect(vm.$children[0].model).to.equal(2);
          done();
        });
      });
  
a9e0f79d   Sergio Crisostomo   Use textContent t...
59
      it('should accept normal characters', done => {
3b9de249   Sergio Crisostomo   Add simple unit t...
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
        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();
        });
      });
  
      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'}]
            };
          }
        });
        vm.$nextTick(() => {
          const placeholderSpan = vm.$el.querySelector('.ivu-select-placeholder');
          const selectedValueSpan = vm.$el.querySelector('.ivu-select-selected-value');
          expect(placeholderSpan.style.display).to.equal('none');
          expect(selectedValueSpan.style.display).to.not.equal('none');
          done();
        });
      });
  
      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')];
          expect(defaultSelect.className).to.equal('ivu-select ivu-select-single');
          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() {
          return vm.$children[0].options.length > 0;
        };
        const callback = function() {
          if (vm.$children[0].options == 0) return setTimeout(waitForIt.bind(null, done), 50);
          expect(JSON.stringify(vm.$children[0].options)).to.equal(JSON.stringify(laterOptions));
  
          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
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
    describe('Behavior tests', () => {
      it('should create different and independent instances', done => {
        const options = [
          {value: 'beijing', label: 'Beijing'},
          {value: 'stockholm', label: 'Stockholm'},
          {value: 'lisboa', label: 'Lisboa'}
        ];
  
        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();
        SelectB.toggleMenu();
  
        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();
          });
      });
    });
  
3b9de249   Sergio Crisostomo   Add simple unit t...
234
    describe('Performance tests', () => {
9c32a056   Sergio Crisostomo   Optimize append a...
235
      it('should handle big numbers of options', done => {
3b9de249   Sergio Crisostomo   Add simple unit t...
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
266
267
268
269
270
271
272
        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() {
          return vm.$children[0].options.length == manyLaterOptions.length;
        };
        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);
      });
    });
  });