Blame view

test/unit/specs/date-picker.spec.js 6.76 KB
9ffdd6da   Sergio Crisostomo   Propagate type ch...
1
  import { createVue, destroyVM, stringToDate, promissedTick } from '../util';
fd05bc44   Sergio Crisostomo   add basic DatePic...
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
  
  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];
      picker.showPicker();
      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();
        const daysInCurrentMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0).getDate();
        expect(daysInCurrentMonth).to.equal(calendarCells.length);
        done();
      });
    });
db1b716f   Sergio Crisostomo   Add more DatePick...
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
  
    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);
      expect(Array.isArray(picker.currentValue)).to.equal(true);
      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];
      picker.handleIconClick();
      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
            const [startInternalValue, endInternalValue] = picker.currentValue; // Date Objects
            expect(Math.abs(dayOne - startInternalValue)).to.equal(0);
            expect(Math.abs(dayFive - endInternalValue)).to.equal(0);
  
            // 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...
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
    it('should change type progamatically', done => {
      vm = createVue({
        template: '<Date-picker :type="dateType"></Date-picker>',
        data() {
          return {
            dateType: 'month'
          };
        }
      });
  
      const picker = vm.$children[0];
      picker.handleIconClick();
      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');
  
        expect(dayPanel.style.display).to.equal('none');
        expect(monthPanel.style.display).to.equal('');
        expect(yearPanel.style.display).to.equal('none');
  
        expect(picker.type).to.equal('month');
        expect(picker.selectionMode).to.equal('month');
  
        vm.dateType = 'year';
        promissedTick(picker)
  		.then(() => {
            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');
            expect(picker.selectionMode).to.equal('day');
  
            done();
          });
      });
    });
  
db1b716f   Sergio Crisostomo   Add more DatePick...
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
    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];
      picker.handleIconClick();
      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
            const [startInternalValue, endInternalValue] = picker.currentValue; // Date Objects
            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
              picker.handleIconClick();
  
              vm.$nextTick(() => {
                expect(picker.visible).to.equal(true);
                expect(JSON.stringify(picker.currentValue)).to.equal('[null,null]');
                expect(displayField.value).to.equal('');
  
                clickableCells[firstDayInMonthIndex].firstElementChild.click();
                vm.$nextTick(() => {
                  clickableCells[firstDayInMonthIndex + 4].firstElementChild.click();
                  vm.$nextTick(() => {
                    // recheck internal values
                    expect(Math.abs(picker.currentValue[0] - startInternalValue)).to.equal(0);
                    expect(Math.abs(picker.currentValue[1] - endInternalValue)).to.equal(0);
                    // 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();
                  });
                });
              });
            });
          });
        });
      });
    });
fd05bc44   Sergio Crisostomo   add basic DatePic...
179
  });