-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathdisabledTime.spec.tsx
More file actions
300 lines (265 loc) Β· 9.09 KB
/
disabledTime.spec.tsx
File metadata and controls
300 lines (265 loc) Β· 9.09 KB
1
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
59
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
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
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
266
267
268
269
270
271
272
273
274
275
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
import { fireEvent, render } from '@testing-library/react';
import type { Moment } from 'moment';
import moment from 'moment';
import { resetWarned } from 'rc-util/lib/warning';
import React from 'react';
import {
closePicker,
getMoment,
isSame,
MomentPicker,
MomentRangePicker,
openPicker,
} from './util/commonUtil';
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import KeyCode from 'rc-util/lib/KeyCode';
dayjs.extend(customParseFormat);
function keyDown(keyCode: number) {
fireEvent.keyDown(document.querySelector('input'), {
keyCode,
which: keyCode,
charCode: keyCode,
});
}
describe('Picker.DisabledTime', () => {
it('disabledTime on TimePicker', () => {
render(
<MomentPicker
open
picker="time"
disabledTime={() => ({
disabledSeconds: () => new Array(59).fill(0).map((_, index) => index),
})}
/>,
);
expect(
document.querySelectorAll(
'ul.rc-picker-time-panel-column li.rc-picker-time-panel-cell-disabled',
),
).toHaveLength(59);
});
it('disabledTime on TimeRangePicker', () => {
const { container } = render(
<MomentRangePicker
open
picker="time"
disabledTime={(_, type) => ({
disabledHours: () => (type === 'start' ? [1, 3, 5] : [2, 4]),
})}
/>,
);
expect(
document.querySelectorAll(
'ul.rc-picker-time-panel-column li.rc-picker-time-panel-cell-disabled',
),
).toHaveLength(3);
// Click another one
fireEvent.mouseDown(container.querySelectorAll('input')[1]);
expect(
document.querySelectorAll(
'ul.rc-picker-time-panel-column li.rc-picker-time-panel-cell-disabled',
),
).toHaveLength(2);
});
it('disabledTime', () => {
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
const disabledTime = jest.fn((_: Moment | null, __: 'start' | 'end') => ({
disabledHours: () => [11],
}));
const { container } = render(
<MomentRangePicker
showTime
disabledTime={disabledTime}
defaultValue={[getMoment('1989-11-28'), getMoment('1990-09-03')]}
/>,
);
// Start
openPicker(container);
expect(
document.querySelector('.rc-picker-time-panel-column').querySelectorAll('li')[11],
).toHaveClass('rc-picker-time-panel-cell-disabled');
expect(isSame(disabledTime.mock.calls[0][0], '1989-11-28')).toBeTruthy();
expect(disabledTime.mock.calls[0][1]).toEqual('start');
closePicker(container);
// End
disabledTime.mockClear();
openPicker(container, 1);
expect(
document.querySelector('.rc-picker-time-panel-column').querySelectorAll('li')[11],
).toHaveClass('rc-picker-time-panel-cell-disabled');
expect(isSame(disabledTime.mock.calls[0][0], '1990-09-03')).toBeTruthy();
expect(disabledTime.mock.calls[0][1]).toEqual('end');
closePicker(container, 1);
});
it('dynamic disabledTime should be correct', () => {
jest.useFakeTimers().setSystemTime(getMoment('2023-09-05 22:02:00').valueOf());
render(
<MomentPicker
open
picker="time"
disabledTime={() => ({
disabledHours: () => [0, 1],
disabledMinutes: (selectedHour) => {
if (selectedHour === 2) {
return [0, 1];
} else {
return [];
}
},
disabledSeconds: (_, selectMinute) => {
if (selectMinute === 2) {
return [0, 1];
} else {
return [];
}
},
})}
/>,
);
// click hour 3
fireEvent.click(
document.querySelectorAll('.rc-picker-time-panel-column')[0].querySelectorAll('li')[2],
);
// click minute 0
fireEvent.click(
document.querySelectorAll('.rc-picker-time-panel-column')[1].querySelectorAll('li')[0],
);
// click second 0
fireEvent.click(
document.querySelectorAll('.rc-picker-time-panel-column')[2].querySelectorAll('li')[0],
);
// click hour 2
fireEvent.click(
document.querySelectorAll('.rc-picker-time-panel-column')[0].querySelectorAll('li')[1],
);
expect(document.querySelector('.rc-picker-input input').getAttribute('value')).toEqual(
'02:02:02',
);
jest.useRealTimers();
});
it('disabledTime should reset correctly when date changed by click', function () {
const disabledTime = jest.fn((_: Moment | null, __: 'start' | 'end') => ({
disabledHours: () => [0, 1, 2, 3, 4, 10],
}));
render(
<MomentRangePicker
open
showTime
disabledTime={disabledTime}
defaultValue={[getMoment('1989-11-28'), getMoment('1990-09-03')]}
/>,
);
expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual(
'1989-11-28 00:00:00',
);
fireEvent.click(document.querySelectorAll('.rc-picker-cell-inner')[2]);
expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual(
'1989-10-31 05:00:00',
);
});
it('disabledTime should reset correctly when date changed by click for no default value', function () {
const now = moment();
const h = now.hours();
const m = now.minutes();
const s = now.seconds();
const disabledTime = jest.fn((_: Moment | null, __: 'start' | 'end') => ({
disabledHours: () => [h],
disabledMinutes: () => [m],
disabledSeconds: () => [s],
}));
const firstDayInMonth = now.startOf('month');
const firstDayInCalendar = firstDayInMonth.clone().subtract(firstDayInMonth.days(), 'days');
const expected = firstDayInCalendar
.clone()
.hour(h + (1 % 24))
.minute(m + (1 % 60))
.second(s + (1 % 60));
render(<MomentRangePicker open showTime disabledTime={disabledTime} />);
fireEvent.click(document.querySelectorAll('.rc-picker-cell-inner')[0]);
expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual(
expected.format('YYYY-MM-DD HH:mm:ss'),
);
});
// https://github.com/ant-design/ant-design/issues/45489
it('disabledTime should reset correctly when date time change by input enter', () => {
resetWarned();
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const range = (start, end) => {
const result = [];
for (let i = start; i < end; i++) {
result.push(i);
}
return result;
};
const disabledDate = (current) => {
return current && current.isBefore(Date.now());
};
const disabledDateTime = () => ({
disabledHours: () => range(0, 24).splice(4, 20),
disabledMinutes: () => range(30, 60),
disabledSeconds: () => [55, 56],
});
const expectDate = '2023-10-26 00:00:00';
const changingDate = '2023-10-26 03:00:00';
const nowAllowDate = '2023-10-26 06:00:00';
const { container } = render(
<MomentPicker
showTime
picker="date"
format="YYYY-MM-DD HH:mm:ss"
disabledDate={disabledDate}
disabledTime={disabledDateTime}
defaultValue={moment(expectDate)}
/>,
);
openPicker(container);
const yearPanel = document.querySelector('.rc-picker-time-panel-column');
expect(yearPanel.querySelectorAll('li')[4]).toHaveClass('rc-picker-time-panel-cell-disabled');
closePicker(container);
expect(document.querySelector('.rc-picker-input input').getAttribute('value')).toEqual(
expectDate,
);
fireEvent.click(document.querySelector('.rc-picker-input > input'));
fireEvent.change(document.querySelector('.rc-picker-input > input'), {
target: {
value: changingDate,
},
});
keyDown(KeyCode.ENTER);
expect(document.querySelector('.rc-picker-input input').getAttribute('value')).toEqual(
changingDate,
);
fireEvent.click(document.querySelector('.rc-picker-input > input'));
fireEvent.change(document.querySelector('.rc-picker-input > input'), {
target: {
value: nowAllowDate,
},
});
keyDown(KeyCode.ENTER);
expect(document.querySelector('.rc-picker-input input').getAttribute('value')).toEqual(
changingDate,
);
errSpy.mockRestore();
});
describe('warning for legacy props', () => {
it('single', () => {
resetWarned();
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(<MomentPicker picker="time" disabledMinutes={() => []} />);
expect(errSpy).toHaveBeenCalledWith(
"Warning: 'disabledHours', 'disabledMinutes', 'disabledSeconds' will be removed in the next major version, please use 'disabledTime' instead.",
);
errSpy.mockRestore();
});
it('range', () => {
resetWarned();
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(<MomentRangePicker picker="time" disabledMinutes={() => []} />);
expect(errSpy).toHaveBeenCalledWith(
"Warning: 'disabledHours', 'disabledMinutes', 'disabledSeconds' will be removed in the next major version, please use 'disabledTime' instead.",
);
errSpy.mockRestore();
});
});
});