Skip to content

Commit b26360e

Browse files
committed
fix: add case test
1 parent 6d646a7 commit b26360e

File tree

1 file changed

+91
-4
lines changed

1 file changed

+91
-4
lines changed

tests/disabledTime.spec.tsx

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ import {
1212
openPicker,
1313
} from './util/commonUtil';
1414

15+
import dayjs from 'dayjs';
16+
import customParseFormat from 'dayjs/plugin/customParseFormat';
17+
import KeyCode from 'rc-util/lib/KeyCode';
18+
dayjs.extend(customParseFormat);
19+
20+
function keyDown(keyCode: number) {
21+
fireEvent.keyDown(document.querySelector('input'), {
22+
keyCode,
23+
which: keyCode,
24+
charCode: keyCode,
25+
});
26+
}
27+
1528
describe('Picker.DisabledTime', () => {
1629
it('disabledTime on TimePicker', () => {
1730
render(
@@ -153,11 +166,15 @@ describe('Picker.DisabledTime', () => {
153166
/>,
154167
);
155168

156-
expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual('1989-11-28 00:00:00');
169+
expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual(
170+
'1989-11-28 00:00:00',
171+
);
157172

158173
fireEvent.click(document.querySelectorAll('.rc-picker-cell-inner')[2]);
159174

160-
expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual('1989-10-31 05:00:00');
175+
expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual(
176+
'1989-10-31 05:00:00',
177+
);
161178
});
162179

163180
it('disabledTime should reset correctly when date changed by click for no default value', function () {
@@ -174,8 +191,12 @@ describe('Picker.DisabledTime', () => {
174191

175192
const firstDayInMonth = now.startOf('month');
176193
const firstDayInCalendar = firstDayInMonth.clone().subtract(firstDayInMonth.days(), 'days');
177-
const expected = firstDayInCalendar.clone().hour(h + 1 % 24).minute(m + 1 % 60).second(s + 1 % 60);
178-
194+
const expected = firstDayInCalendar
195+
.clone()
196+
.hour(h + (1 % 24))
197+
.minute(m + (1 % 60))
198+
.second(s + (1 % 60));
199+
179200
render(<MomentRangePicker open showTime disabledTime={disabledTime} />);
180201

181202
fireEvent.click(document.querySelectorAll('.rc-picker-cell-inner')[0]);
@@ -184,6 +205,72 @@ describe('Picker.DisabledTime', () => {
184205
expected.format('YYYY-MM-DD HH:mm:ss'),
185206
);
186207
});
208+
// https://github.com/ant-design/ant-design/issues/45489
209+
it('disabledTime should reset correctly when date time change by input enter', () => {
210+
resetWarned();
211+
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
212+
const range = (start, end) => {
213+
const result = [];
214+
for (let i = start; i < end; i++) {
215+
result.push(i);
216+
}
217+
return result;
218+
};
219+
const disabledDate = (current) => {
220+
return current && current.isBefore(Date.now());
221+
};
222+
const disabledDateTime = () => ({
223+
disabledHours: () => range(0, 24).splice(4, 20),
224+
disabledMinutes: () => range(30, 60),
225+
disabledSeconds: () => [55, 56],
226+
});
227+
228+
const expectDate = '2023-10-26 00:00:00';
229+
const changingDate = '2023-10-26 03:00:00';
230+
const nowAllowDate = '2023-10-26 06:00:00';
231+
232+
const { container } = render(
233+
<MomentPicker
234+
showTime
235+
picker="date"
236+
format="YYYY-MM-DD HH:mm:ss"
237+
disabledDate={disabledDate}
238+
disabledTime={disabledDateTime}
239+
defaultValue={moment(expectDate)}
240+
/>,
241+
);
242+
openPicker(container);
243+
const yearPanel = document.querySelector('.rc-picker-time-panel-column');
244+
expect(yearPanel.querySelectorAll('li')[4]).toHaveClass('rc-picker-time-panel-cell-disabled');
245+
closePicker(container);
246+
247+
expect(document.querySelector('.rc-picker-input input').getAttribute('value')).toEqual(
248+
expectDate,
249+
);
250+
251+
fireEvent.click(document.querySelector('.rc-picker-input > input'));
252+
fireEvent.change(document.querySelector('.rc-picker-input > input'), {
253+
target: {
254+
value: changingDate,
255+
},
256+
});
257+
keyDown(KeyCode.ENTER);
258+
expect(document.querySelector('.rc-picker-input input').getAttribute('value')).toEqual(
259+
changingDate,
260+
);
261+
fireEvent.click(document.querySelector('.rc-picker-input > input'));
262+
fireEvent.change(document.querySelector('.rc-picker-input > input'), {
263+
target: {
264+
value: nowAllowDate,
265+
},
266+
});
267+
268+
keyDown(KeyCode.ENTER);
269+
expect(document.querySelector('.rc-picker-input input').getAttribute('value')).toEqual(
270+
changingDate,
271+
);
272+
errSpy.mockRestore();
273+
});
187274

188275
describe('warning for legacy props', () => {
189276
it('single', () => {

0 commit comments

Comments
 (0)