ant-design-vue/components/date-picker/__tests__/showTime.test.js

187 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import { mount } from '@vue/test-utils';
import { asyncExpect } from '@/tests/utils';
import moment from 'moment';
import DatePicker from '../';
import { $$ } from './utils';
import { sleep } from '../../../tests/utils';
2018-06-09 05:14:14 +00:00
2019-01-12 03:33:27 +00:00
const { RangePicker } = DatePicker;
2018-06-09 05:14:14 +00:00
describe('DatePicker with showTime', () => {
beforeEach(() => {
document.body.outerHTML = '';
});
2018-06-09 05:14:14 +00:00
it('should trigger onChange when select value', async () => {
2019-01-12 03:33:27 +00:00
const onChangeFn = jest.fn();
const onOpenChangeFn = jest.fn();
mount(
2019-01-12 03:33:27 +00:00
{
render() {
return <DatePicker showTime open onChange={onChangeFn} onOpenChange={onOpenChangeFn} />;
},
2018-06-09 05:14:14 +00:00
},
{ sync: false, attachTo: 'body' },
2019-01-12 03:33:27 +00:00
);
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
$$('.ant-calendar-date')[0].click();
2019-01-12 03:33:27 +00:00
});
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(onChangeFn).toHaveBeenCalled();
expect(onOpenChangeFn).not.toHaveBeenCalled();
});
});
2018-06-09 05:14:14 +00:00
it('should trigger onOk when press ok button', async () => {
2019-01-12 03:33:27 +00:00
const onOkFn = jest.fn();
const onOpenChangeFn = jest.fn();
const onChangeFn = jest.fn();
mount(
2019-01-12 03:33:27 +00:00
{
render() {
return (
<DatePicker
showTime
open
onChange={onChangeFn}
onOk={onOkFn}
onOpenChange={onOpenChangeFn}
defaultValue={moment()}
/>
);
},
2018-06-09 05:14:14 +00:00
},
{ sync: false, attachTo: 'body' },
2019-01-12 03:33:27 +00:00
);
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
$$('.ant-calendar-ok-btn')[0].click();
2019-01-12 03:33:27 +00:00
});
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(onOkFn).toHaveBeenCalled();
expect(onOpenChangeFn).toHaveBeenCalledWith(false);
expect(onChangeFn).not.toHaveBeenCalled();
});
});
2018-06-09 05:14:14 +00:00
it('should trigger onChange when click Now link', async () => {
2019-01-12 03:33:27 +00:00
const onOpenChangeFn = jest.fn();
const onChangeFn = jest.fn();
mount(
2019-01-12 03:33:27 +00:00
{
render() {
return <DatePicker showTime open onChange={onChangeFn} onOpenChange={onOpenChangeFn} />;
},
2018-06-09 05:14:14 +00:00
},
{ sync: false, attachTo: 'body' },
2019-01-12 03:33:27 +00:00
);
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
$$('.ant-calendar-today-btn')[0].click();
2019-01-12 03:33:27 +00:00
});
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(onOpenChangeFn).toHaveBeenCalledWith(false);
expect(onChangeFn).toHaveBeenCalled();
});
});
2018-06-09 05:14:14 +00:00
it('should have correct className when use12Hours is true', async () => {
mount(
2018-06-09 05:14:14 +00:00
{
2019-01-12 03:33:27 +00:00
render() {
return <DatePicker showTime={{ use12Hours: true }} open />;
2018-06-09 05:14:14 +00:00
},
},
{ sync: false, attachTo: 'body' },
2019-01-12 03:33:27 +00:00
);
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
expect($$('.ant-calendar-time-picker-column-4').length).toBe(0);
2019-01-12 03:33:27 +00:00
});
$$('.ant-calendar-today')[0].click();
await sleep();
$$('.ant-calendar-time-picker-btn')[0].click();
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
expect($$('.ant-calendar-time-picker-column-4').length).toBe(1);
2019-01-12 03:33:27 +00:00
});
});
});
2018-06-09 05:14:14 +00:00
describe('RangePicker with showTime', () => {
beforeEach(() => {
document.body.outerHTML = '';
});
2018-06-09 05:14:14 +00:00
it('should trigger onChange when select value', async () => {
2019-01-12 03:33:27 +00:00
const onChangeFn = jest.fn();
const onOpenChangeFn = jest.fn();
mount(
2018-06-09 05:14:14 +00:00
{
2019-01-12 03:33:27 +00:00
render() {
return <RangePicker showTime open onChange={onChangeFn} onOpenChange={onOpenChangeFn} />;
2018-06-09 05:14:14 +00:00
},
2019-01-12 03:33:27 +00:00
},
{ sync: false },
);
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
expect(
$$('.ant-calendar-time-picker-btn')[0]
.getAttribute('class')
.split(' '),
).toContain('ant-calendar-time-picker-btn-disabled');
expect(
$$('.ant-calendar-ok-btn')[0]
.getAttribute('class')
.split(' '),
).toContain('ant-calendar-ok-btn-disabled');
2019-01-12 03:33:27 +00:00
});
$$('.ant-calendar-date')[10].click();
$$('.ant-calendar-date')[11].click();
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
expect(
$$('.ant-calendar-time-picker-btn')[0]
.getAttribute('class')
.split(' '),
).not.toContain('ant-calendar-time-picker-btn-disabled');
expect(
$$('.ant-calendar-ok-btn')[0]
.getAttribute('class')
.split(' '),
).not.toContain('ant-calendar-ok-btn-disabled');
2019-01-12 03:33:27 +00:00
});
expect(onChangeFn).toHaveBeenCalled();
expect(onOpenChangeFn).not.toHaveBeenCalled();
});
2018-06-09 05:14:14 +00:00
it('hould trigger onOk when press ok button', async () => {
2019-01-12 03:33:27 +00:00
const onOkFn = jest.fn();
const onChangeFn = jest.fn();
const onOpenChangeFn = jest.fn();
mount(
2019-01-12 03:33:27 +00:00
{
render() {
return (
<RangePicker
showTime
open
onOk={onOkFn}
onChange={onChangeFn}
onOpenChange={onOpenChangeFn}
/>
);
},
2018-06-09 05:14:14 +00:00
},
{ sync: false, attachTo: 'body' },
2019-01-12 03:33:27 +00:00
);
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
$$('.ant-calendar-date')[10].click();
$$('.ant-calendar-date')[11].click();
2019-01-12 03:33:27 +00:00
});
onChangeFn.mockClear();
$$('.ant-calendar-ok-btn')[0].click();
2019-01-12 03:33:27 +00:00
expect(onOkFn).toHaveBeenCalled();
expect(onOpenChangeFn).toHaveBeenCalledWith(false);
expect(onChangeFn).not.toHaveBeenCalled();
});
});