Skip to content
This repository was archived by the owner on Apr 17, 2022. It is now read-only.

Commit db3a07f

Browse files
committed
test: Add test case
1 parent b4a3b80 commit db3a07f

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

tests/unit/logic.spec.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,24 @@ const mountDatepicker = async (props: any = {}): Promise<{ dp: VueWrapper<any>;
3131
describe('Logic connection', () => {
3232
it('Should properly define initial values', async () => {
3333
const date = new Date();
34-
const { menu } = await mountDatepicker({ modelValue: date });
34+
const { dp, menu } = await mountDatepicker({ modelValue: date });
3535

3636
expect(menu.vm.month(0)).toEqual(date.getMonth());
3737
expect(menu.vm.year(0)).toEqual(date.getFullYear());
3838
expect(menu.vm.hours).toEqual(date.getHours());
3939
expect(menu.vm.minutes).toEqual(date.getMinutes());
40+
dp.unmount();
4041
});
4142

4243
it('Should properly map initial values for range picker', async () => {
4344
const start = new Date();
4445
const end = addDays(start, 7);
45-
const { menu } = await mountDatepicker({ modelValue: [start, end], range: true });
46+
const { dp, menu } = await mountDatepicker({ modelValue: [start, end], range: true });
4647

4748
expect(menu.vm.hours).toHaveLength(2);
4849
expect(menu.vm.minutes).toHaveLength(2);
4950
expect(menu.vm.hours[0]).toEqual(start.getHours());
51+
dp.unmount();
5052
});
5153

5254
it('Should select date', async () => {
@@ -59,12 +61,13 @@ describe('Logic connection', () => {
5961
await calendar.vm.$nextTick();
6062

6163
expect(dp.vm.internalModelValue).toEqual(tomorrow);
64+
dp.unmount();
6265
});
6366

6467
it('Should select range', async () => {
6568
const start = setSeconds(addDays(new Date(), 1), 0);
6669
const end = setSeconds(addDays(start, 7), 0);
67-
const { menu } = await mountDatepicker({ modelValue: null, range: true });
70+
const { dp, menu } = await mountDatepicker({ modelValue: null, range: true });
6871

6972
const calendar = menu.findComponent(Calendar);
7073
calendar.vm.$emit('selectDate', { value: start, current: true });
@@ -78,12 +81,13 @@ describe('Logic connection', () => {
7881
expect(menu.vm.internalModelValue).toHaveLength(2);
7982
expect(menu.vm.internalModelValue[0]).toEqual(start);
8083
expect(menu.vm.internalModelValue[1]).toEqual(end);
84+
dp.unmount();
8185
});
8286

8387
it('Should select auto range', async () => {
8488
const start = setSeconds(new Date(), 0);
8589
const end = setSeconds(addDays(start, 7), 0);
86-
const { menu } = await mountDatepicker({ modelValue: null, range: true, autoRange: 7 });
90+
const { dp, menu } = await mountDatepicker({ modelValue: null, range: true, autoRange: 7 });
8791

8892
const calendar = menu.findComponent(Calendar);
8993

@@ -93,12 +97,13 @@ describe('Logic connection', () => {
9397
expect(menu.vm.internalModelValue).toHaveLength(2);
9498
expect(menu.vm.internalModelValue[0]).toEqual(start);
9599
expect(menu.vm.internalModelValue[1]).toEqual(end);
100+
dp.unmount();
96101
});
97102

98103
it('Should update time', async () => {
99104
const val = 15;
100105
const date = new Date();
101-
const { menu } = await mountDatepicker({ modelValue: date });
106+
const { dp, menu } = await mountDatepicker({ modelValue: date });
102107

103108
const timePicker = menu.findComponent(TimePicker);
104109
timePicker.vm.$emit('update:hours', val);
@@ -108,13 +113,14 @@ describe('Logic connection', () => {
108113

109114
expect(menu.vm.internalModelValue.getHours()).toEqual(val);
110115
expect(menu.vm.internalModelValue.getMinutes()).toEqual(val);
116+
dp.unmount();
111117
});
112118

113119
it('Should update range time', async () => {
114120
const val = 15;
115121
const start = new Date();
116122
const end = addDays(new Date(), 7);
117-
const { menu } = await mountDatepicker({ modelValue: [start, end], range: true });
123+
const { dp, menu } = await mountDatepicker({ modelValue: [start, end], range: true });
118124

119125
const timePicker = menu.findComponent(TimePicker);
120126
timePicker.vm.$emit('update:hours', [start.getHours(), val]);
@@ -124,26 +130,29 @@ describe('Logic connection', () => {
124130

125131
expect(menu.vm.internalModelValue[1].getHours()).toEqual(val);
126132
expect(menu.vm.internalModelValue[0].getMinutes()).toEqual(val);
133+
dp.unmount();
127134
});
128135

129136
it('Should set month', async () => {
130137
const month = 0;
131-
const { menu } = await mountDatepicker({ modelValue: null, range: true });
138+
const { dp, menu } = await mountDatepicker({ modelValue: null, range: true });
132139

133140
const monthYearInput = menu.findComponent(MonthYearInput);
134141
monthYearInput.vm.$emit('update:month', month);
135142

136143
expect(menu.vm.month(0)).toEqual(month);
144+
dp.unmount();
137145
});
138146

139147
it('Should set year', async () => {
140148
const year = 2022;
141-
const { menu } = await mountDatepicker({ modelValue: null, range: true });
149+
const { dp, menu } = await mountDatepicker({ modelValue: null, range: true });
142150

143151
const monthYearInput = menu.findComponent(MonthYearInput);
144152
monthYearInput.vm.$emit('update:year', year);
145153

146154
expect(menu.vm.year(0)).toEqual(year);
155+
dp.unmount();
147156
});
148157

149158
it('Should format with custom function', async () => {
@@ -158,19 +167,21 @@ describe('Logic connection', () => {
158167
await dp.vm.$nextTick();
159168

160169
expect(dp.vm.inputValue).toEqual(format(selected));
170+
dp.unmount();
161171
});
162172

163173
it('Should display preview format from function', async () => {
164174
const selected = new Date();
165175

166-
const { menu } = await mountDatepicker({ modelValue: null, previewFormat: format });
176+
const { dp, menu } = await mountDatepicker({ modelValue: null, previewFormat: format });
167177

168178
const calendar = menu.findComponent(Calendar);
169179
calendar.vm.$emit('selectDate', { value: selected, current: true });
170180
await calendar.vm.$nextTick();
171181

172182
const actionRow = menu.findComponent(ActionRow);
173183
expect(actionRow.text().includes(format(selected))).toBeTruthy();
184+
dp.unmount();
174185
});
175186

176187
it('Should format with locale', async () => {
@@ -191,6 +202,7 @@ describe('Logic connection', () => {
191202
await dp.vm.$nextTick();
192203

193204
expect(dp.vm.inputValue).toEqual('木');
205+
dp.unmount();
194206
});
195207

196208
it('Should select multiple dates', async () => {
@@ -213,5 +225,21 @@ describe('Logic connection', () => {
213225
// deselect all dates
214226
await selectDates();
215227
expect(dp.vm.internalModelValue).toBeNull();
228+
dp.unmount();
229+
});
230+
231+
it('Should preset range from preset-dates', async () => {
232+
const range = [new Date(), new Date()];
233+
const { dp, menu } = await mountDatepicker({
234+
modeValue: null,
235+
range: true,
236+
presetDates: [{ label: 'Today', range }],
237+
});
238+
239+
menu.vm.presetDateRange(range);
240+
241+
expect(dp.vm.internalModelValue).toHaveLength(2);
242+
expect(dp.vm.internalModelValue).toEqual(range);
243+
dp.unmount();
216244
});
217245
});

0 commit comments

Comments
 (0)