Blame view

test/unit/specs/date-picker.spec.js 14.1 KB
57f0582b   Sergio Crisostomo   Pass correct argu...
1
  import { createVue, destroyVM, stringToDate, dateToString, dateToTimeString, promissedTick } from '../util';
fd05bc44   Sergio Crisostomo   add basic DatePic...
2
3
4
5
6
7
8
9
10
11
12
13
  
  describe('DatePicker.vue', () => {
    let vm;
    afterEach(() => {
      destroyVM(vm);
    });
  
    it('should create a DatePicker component and open the calendar with the current month', done => {
      vm = createVue(`
        <Date-Picker></Date-Picker>
      `);
      const picker = vm.$children[0];
5426dcf9   Sergio Crisostomo   fix specs, fix me...
14
      picker.$el.querySelector('input.ivu-input').focus();
fd05bc44   Sergio Crisostomo   add basic DatePic...
15
16
17
18
19
20
21
22
      vm.$nextTick(() => {
        const calendarBody = vm.$el.querySelector('.ivu-picker-panel-body .ivu-date-picker-cells:first-of-type');
        const calendarCells = [...calendarBody.querySelectorAll('.ivu-date-picker-cells-cell')].filter(el => {
          const prevMonth = el.classList.contains('ivu-date-picker-cells-cell-prev-month');
          const nextMonth = el.classList.contains('ivu-date-picker-cells-cell-next-month');
          return !prevMonth && !nextMonth;
        });
        const today = new Date();
7d6ea205   Sergio Crisostomo   fix broken spec
23
        const daysInCurrentMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0).getDate();
fd05bc44   Sergio Crisostomo   add basic DatePic...
24
25
26
27
        expect(daysInCurrentMonth).to.equal(calendarCells.length);
        done();
      });
    });
db1b716f   Sergio Crisostomo   Add more DatePick...
28
  
57f0582b   Sergio Crisostomo   Pass correct argu...
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
    it('should pass correct arguments to on-change event', done => {
      const now = new Date();
      const nowDate = dateToString(now);
      const nowTime = dateToTimeString(now);
      const nextHour = dateToTimeString(now.getTime() + 36e5);
      const nextWeek = new Date(now.getTime() + 6048e5);
  
      let dateValue, dateRangeValue, timeValue, timeRangeValue;
      vm = createVue({
        template: `
          <div>
            <date-picker type="date" @on-change="onChangeDate"></date-picker>
            <date-picker type="daterange" @on-change="onChangeDateRange"></date-picker>
            <time-picker type="time" @on-change="onChangeTime"></time-picker>
            <time-picker type="timerange" @on-change="onChangeTimeRange"></time-picker>
          </div>
        `,
        methods: {
          onChangeDate(val) {
            dateValue = val;
          },
          onChangeDateRange(val) {
            dateRangeValue = val;
          },
          onChangeTime(val) {
            timeValue = val;
          },
          onChangeTimeRange(val) {
            timeRangeValue = val;
          },
        }
      }, true);
  
      vm.$nextTick(() => {
        const [datePicker, dateRangePicker, timePicker, timeRangePicker] = vm.$children;
  
        datePicker.handleInputChange({target: {value: nowDate}});
        dateRangePicker.handleInputChange({target: {value: [
            nowDate,
            dateToString(nextWeek)
          ].join(' - ')
        }});
  
        timePicker.handleInputChange({target: {value: nowTime}});
        const timeRangeString = [
            nowTime,
            nextHour
        ].join(' - ');
        timeRangePicker.handleInputChange({target: {
          value: timeRangeString
        }});
  
        vm.$nextTick(() => {
          // DATE
b42322fe   Sergio Crisostomo   Pass Strings to @...
83
84
          expect(typeof dateValue).to.equal('string');
          expect(dateValue).to.equal(nowDate);
57f0582b   Sergio Crisostomo   Pass correct argu...
85
86
          // DATERANGE
          expect(Array.isArray(dateRangeValue)).to.equal(true);
b42322fe   Sergio Crisostomo   Pass Strings to @...
87
88
89
          expect(dateRangeValue[0]).to.equal(nowDate);
          expect(dateRangeValue[1]).to.equal(dateToString(nextWeek));
  
57f0582b   Sergio Crisostomo   Pass correct argu...
90
91
92
93
94
95
96
97
98
99
100
101
102
          // TIME
          expect(typeof timeValue).to.equal('string');
          expect(timeValue).to.equal(nowTime);
          // TIMERANGE
          expect(Array.isArray(timeRangeValue)).to.equal(true);
          expect(timeRangeValue[0]).to.equal(nowTime);
          expect(timeRangeValue[1]).to.equal(nextHour);
  
          done();
        });
      });
    });
  
db1b716f   Sergio Crisostomo   Add more DatePick...
103
104
105
106
107
108
    it('should create a DatePicker component of type="datetimerange"', done => {
      vm = createVue(`
        <Date-Picker type="datetimerange"></Date-Picker>
      `);
      const picker = vm.$children[0];
      expect(picker.$children.length).to.equal(2);
5426dcf9   Sergio Crisostomo   fix specs, fix me...
109
      expect(Array.isArray(picker.internalValue)).to.equal(true);
db1b716f   Sergio Crisostomo   Add more DatePick...
110
111
112
113
114
115
116
117
118
      done();
    });
  
    it('should create a datetimerange component and pick 2 dates in the current month', done => {
      vm = createVue(`
        <Date-picker type="datetimerange"></Date-picker>
      `);
  
      const picker = vm.$children[0];
75cb2998   Sergio Crisostomo   Add keyboard navi...
119
      picker.handleFocus({type: 'focus'});
db1b716f   Sergio Crisostomo   Add more DatePick...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
      vm.$nextTick(() => {
        const displayField = vm.$el.querySelector('.ivu-input');
        const clickableCells = vm.$el.querySelectorAll('.ivu-date-picker-cells-cell');
        const lastMonthClass = 'ivu-date-picker-cells-cell-prev-month';
        const firstDayInMonthIndex = [...clickableCells].findIndex(cell => !cell.classList.contains(lastMonthClass));
  
        clickableCells[firstDayInMonthIndex].firstElementChild.click();
        vm.$nextTick(() => {
          clickableCells[firstDayInMonthIndex + 4].firstElementChild.click();
          vm.$nextTick(() => {
            const dayOne = new Date();
            dayOne.setDate(1);
            dayOne.setHours(0, 0, 0, 0);
            const dayFive = new Date(dayOne.getTime());
            dayFive.setDate(5);
            dayFive.setHours(0, 0, 0, 0);
  
            // check pickers internal value
5426dcf9   Sergio Crisostomo   fix specs, fix me...
138
            const [startInternalValue, endInternalValue] = picker.internalValue; // Date Objects
db1b716f   Sergio Crisostomo   Add more DatePick...
139
140
141
            expect(Math.abs(dayOne - startInternalValue)).to.equal(0);
            expect(Math.abs(dayFive - endInternalValue)).to.equal(0);
  
5426dcf9   Sergio Crisostomo   fix specs, fix me...
142
143
144
145
146
147
148
            /*
                      const [startInternalValue, endInternalValue] = picker.internalValue; // Date Objects
            expect(dateToString(dayOne)).to.equal(dateToString(startInternalValue));
            expect(dateToString(dayFive)).to.equal(dateToString(endInternalValue));
  
             */
  
db1b716f   Sergio Crisostomo   Add more DatePick...
149
150
151
152
153
154
155
156
157
158
159
            // check pickers display value
            const [startDisplayValue, endDisplayValue] = displayField.value.split(' - ').map(stringToDate); // Date Objects
            expect(Math.abs(dayOne - startDisplayValue)).to.equal(0);
            expect(Math.abs(dayFive - endDisplayValue)).to.equal(0);
  
            done();
          });
        });
      });
    });
  
9ffdd6da   Sergio Crisostomo   Propagate type ch...
160
    it('should change type progamatically', done => {
5426dcf9   Sergio Crisostomo   fix specs, fix me...
161
      // https://jsfiddle.net/hq7cLz83/
9ffdd6da   Sergio Crisostomo   Propagate type ch...
162
163
164
165
166
167
168
169
170
171
      vm = createVue({
        template: '<Date-picker :type="dateType"></Date-picker>',
        data() {
          return {
            dateType: 'month'
          };
        }
      });
  
      const picker = vm.$children[0];
75cb2998   Sergio Crisostomo   Add keyboard navi...
172
      picker.handleFocus({type: 'focus'});
9ffdd6da   Sergio Crisostomo   Propagate type ch...
173
174
175
176
177
178
      vm.$nextTick(() => {
        const panel = vm.$el.querySelector('.ivu-picker-panel-content');
        const dayPanel = panel.querySelector('[class="ivu-date-picker-cells"]');
        const monthPanel = panel.querySelector('.ivu-date-picker-cells-month');
        const yearPanel = panel.querySelector('.ivu-date-picker-cells-year');
  
5426dcf9   Sergio Crisostomo   fix specs, fix me...
179
        expect(dayPanel).to.equal(null);
9ffdd6da   Sergio Crisostomo   Propagate type ch...
180
        expect(monthPanel.style.display).to.equal('');
5426dcf9   Sergio Crisostomo   fix specs, fix me...
181
        expect(yearPanel).to.equal(null);
9ffdd6da   Sergio Crisostomo   Propagate type ch...
182
183
184
185
186
187
  
        expect(picker.type).to.equal('month');
        expect(picker.selectionMode).to.equal('month');
  
        vm.dateType = 'year';
        promissedTick(picker)
3ed12b4e   Sergio Crisostomo   Correct month cal...
188
          .then(() => {
5426dcf9   Sergio Crisostomo   fix specs, fix me...
189
190
191
192
193
            const yearPanel = panel.querySelector('.ivu-date-picker-cells-year');
            const monthPanel = panel.querySelector('.ivu-date-picker-cells-month');
            expect(yearPanel.style.display).to.equal('');
            expect(monthPanel).to.equal(null);
  
3ed12b4e   Sergio Crisostomo   Correct month cal...
194
195
196
197
198
199
200
201
            expect(picker.type).to.equal('year');
            expect(picker.selectionMode).to.equal('year');
  
            vm.dateType = 'date';
            return promissedTick(picker);
          })
          .then(() => {
            expect(picker.type).to.equal('date');
5426dcf9   Sergio Crisostomo   fix specs, fix me...
202
            expect(picker.selectionMode).to.equal('date');
3ed12b4e   Sergio Crisostomo   Correct month cal...
203
204
  
            done();
5426dcf9   Sergio Crisostomo   fix specs, fix me...
205
          }).catch(err => console.log(err));
9ffdd6da   Sergio Crisostomo   Propagate type ch...
206
207
208
      });
    });
  
b1d124f6   Sergio Crisostomo   add test for #2215
209
210
    it('should fire `on-change` when reseting value', done => {
      const now = new Date();
57f0582b   Sergio Crisostomo   Pass correct argu...
211
      const nowDate = dateToString(now);
7f8d334b   Sergio Crisostomo   fix date picker c...
212
      let onChangeCalled = false;
b1d124f6   Sergio Crisostomo   add test for #2215
213
      vm = createVue({
7f8d334b   Sergio Crisostomo   fix date picker c...
214
        template: '<date-picker :value="date" type="date" @on-change="onChange"></date-picker>',
b1d124f6   Sergio Crisostomo   add test for #2215
215
216
217
218
        data(){
          return { date: now };
        },
        methods: {
7f8d334b   Sergio Crisostomo   fix date picker c...
219
220
          onChange() {
            onChangeCalled = true;
b1d124f6   Sergio Crisostomo   add test for #2215
221
222
223
224
225
226
227
228
229
230
231
232
          }
        }
      });
  
      vm.$nextTick(() => {
        const picker = vm.$children[0];
        const displayField = vm.$el.querySelector('.ivu-input');
        expect(displayField.value).to.equal(nowDate);
  
        picker.showClose = true; // to simulate mouseenter in the Input
        picker.handleIconClick(); // reset the input value
        vm.$nextTick(() => {
7f8d334b   Sergio Crisostomo   fix date picker c...
233
          expect(onChangeCalled).to.equal(true);
b1d124f6   Sergio Crisostomo   add test for #2215
234
235
236
237
238
239
          expect(displayField.value).to.equal('');
          done();
        });
      });
    });
  
db1b716f   Sergio Crisostomo   Add more DatePick...
240
241
242
243
244
245
    it('should have same behavior after a reset as before the reset', done => {
      vm = createVue(`
        <Date-picker type="datetimerange"></Date-picker>
      `);
  
      const picker = vm.$children[0];
75cb2998   Sergio Crisostomo   Add keyboard navi...
246
      picker.handleFocus({type: 'focus'});
db1b716f   Sergio Crisostomo   Add more DatePick...
247
248
249
250
251
252
253
254
255
256
257
258
259
      vm.$nextTick(() => {
        const displayField = vm.$el.querySelector('.ivu-input');
        const clickableCells = vm.$el.querySelectorAll('.ivu-date-picker-cells-cell');
        const lastMonthClass = 'ivu-date-picker-cells-cell-prev-month';
        const firstDayInMonthIndex = [...clickableCells].findIndex(cell => !cell.classList.contains(lastMonthClass));
  
        // choose first date
        clickableCells[firstDayInMonthIndex].firstElementChild.click();
        vm.$nextTick(() => {
          // choose second date
          clickableCells[firstDayInMonthIndex + 4].firstElementChild.click();
          vm.$nextTick(() => {
            // cache first values
5426dcf9   Sergio Crisostomo   fix specs, fix me...
260
            const [startInternalValue, endInternalValue] = picker.internalValue; // Date Objects
db1b716f   Sergio Crisostomo   Add more DatePick...
261
262
263
264
265
266
267
268
            const [startDisplayValue, endDisplayValue] = displayField.value.split(' - ').map(stringToDate); // Date Objects
  
            // clear picker
            picker.handleClear();
            vm.$nextTick(() => {
              // it should be closed by now
              expect(picker.visible).to.equal(false);
              // open picker again
75cb2998   Sergio Crisostomo   Add keyboard navi...
269
270
                picker.handleFocus({type: 'focus'});
                picker.visible = true;
db1b716f   Sergio Crisostomo   Add more DatePick...
271
  
75cb2998   Sergio Crisostomo   Add keyboard navi...
272
273
  
                vm.$nextTick(() => {
db1b716f   Sergio Crisostomo   Add more DatePick...
274
                expect(picker.visible).to.equal(true);
5426dcf9   Sergio Crisostomo   fix specs, fix me...
275
                expect(JSON.stringify(picker.internalValue)).to.equal('[null,null]');
db1b716f   Sergio Crisostomo   Add more DatePick...
276
277
278
279
280
281
282
                expect(displayField.value).to.equal('');
  
                clickableCells[firstDayInMonthIndex].firstElementChild.click();
                vm.$nextTick(() => {
                  clickableCells[firstDayInMonthIndex + 4].firstElementChild.click();
                  vm.$nextTick(() => {
                    // recheck internal values
5426dcf9   Sergio Crisostomo   fix specs, fix me...
283
284
                    expect(Math.abs(picker.internalValue[0] - startInternalValue)).to.equal(0);
                    expect(Math.abs(picker.internalValue[1] - endInternalValue)).to.equal(0);
db1b716f   Sergio Crisostomo   Add more DatePick...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
                    // recheck display value
                    const [_startDisplayValue, _endDisplayValue] = displayField.value.split(' - ').map(stringToDate); // Date Objects
                    expect(Math.abs(_startDisplayValue - startDisplayValue)).to.equal(0);
                    expect(Math.abs(_endDisplayValue - endDisplayValue)).to.equal(0);
  
                    done();
                  });
                });
              });
            });
          });
        });
      });
    });
5d1c24c7   Sergio Crisostomo   correct behaviour...
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
  
    it('should accept a empty string as input v-model value', done => {
      vm = createVue({
        template: '<date-picker v-model="value" type="date"></date-picker>',
        data(){
          return {value: ''};
        }
      });
  
      vm.$nextTick(() => {
        expect(vm.value).to.equal('');
        done();
      });
    });
  
65255c96   Sergio Crisostomo   fix date formatti...
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
    it('should convert strings to Date objects', done => {
      vm = createVue({
        template: `
          <div>
            <date-picker v-model="value1" type="daterange" style="width: 200px"></date-picker>
            <date-picker v-model="value2" type="daterange" placement="bottom-end" style="width: 200px"></date-picker>
            <date-picker v-model="value3" type="datetime" placement="bottom-end" style="width: 200px"></date-picker>
            <date-picker v-model="value4" type="datetimerange" placement="bottom-end" style="width: 200px"></date-picker>
          </div>
        `,
        data() {
          return {
            value1: ['2017-10-10', '2017-10-20'],
            value2: [new Date(), new Date()],
            value3: '2017-10-10 10:00:00',
            value4: ['2027-10-10 10:00:00', '2027-10-20 10:00:00']
          };
        }
      });
  
      vm.$nextTick(() => {
        const {value1, value2, value3, value4} = vm;
  
        expect(value1[0] instanceof Date).to.equal(true);
        expect(value1[1] instanceof Date).to.equal(true);
        expect(value1.map(dateToString).join('|')).to.equal('2017-10-10|2017-10-20');
  
        expect(value2[0] instanceof Date).to.equal(true);
        expect(value2[1] instanceof Date).to.equal(true);
        expect(value2.map(dateToString).join('|')).to.equal([new Date(), new Date()].map(dateToString).join('|'));
  
b42322fe   Sergio Crisostomo   Pass Strings to @...
345
        expect(dateToString(value3)).to.equal('2017-10-10');
65255c96   Sergio Crisostomo   fix date formatti...
346
347
348
349
350
351
352
353
  
        expect(value4[0] instanceof Date).to.equal(true);
        expect(value4[1] instanceof Date).to.equal(true);
        expect(value4.map(dateToString).join('|')).to.equal('2027-10-10|2027-10-20');
        done();
      });
    });
  
3ed12b4e   Sergio Crisostomo   Correct month cal...
354
355
356
357
358
359
    it('should render date-picker label correctly in zh-CN', done => {
      vm = createVue(`
        <Date-picker type="date"></Date-picker>
      `);
  
      const picker = vm.$children[0];
75cb2998   Sergio Crisostomo   Add keyboard navi...
360
      picker.handleFocus({type: 'focus'});
3ed12b4e   Sergio Crisostomo   Correct month cal...
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
      vm.$nextTick(() => {
        const now = new Date();
        const labels = vm.$el.querySelectorAll('.ivu-picker-panel-body .ivu-date-picker-header-label');
        const labelText = [...labels].map(el => el.textContent).join(' ');
        expect(labelText).to.equal([now.getFullYear() + '年', now.getMonth() + 1 + '月'].join(' '));
        done();
      });
    });
  
    it('Should format labels correctly', done => {
      const formater = require('../../../src/components/date-picker/util').formatDateLabels;
      const expectedResults = require('./assets/locale-expects.js').default;
      const locales = [
        'de-DE', 'en-US', 'es-ES', 'fr-FR', 'id-ID', 'ja-JP', 'ko-KR', 'pt-BR',
        'pt-PT', 'ru-RU', 'sv-SE', 'tr-TR', 'vi-VN', 'zh-CN', 'zh-TW'
      ].reduce((obj, locale) => {
        obj[locale] = require('../../../src/locale/lang/' + locale).default;
        return obj;
      }, {});
      const testDate = new Date(2030, 9); // October 2030
  
      Object.keys(locales).forEach(locale => {
        const format = locales[locale].i.datepicker.datePanelLabel;
        const f = formater(locale, format, testDate);
        const labelText = f.labels.map(obj => obj.label).join(f.separator);
        expect(labelText).to.equal(expectedResults[locale]);
      });
      expect(Object.keys(locales).length > 0).to.equal(true);
      done();
    });
fd05bc44   Sergio Crisostomo   add basic DatePic...
391
  });