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

235 lines
6.2 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 '../';
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', () => {
it('should trigger onChange when select value', async () => {
2019-01-12 03:33:27 +00:00
const onChangeFn = jest.fn();
const onOpenChangeFn = jest.fn();
const wrapper = mount(
{
render() {
return <DatePicker 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
2019-01-12 03:33:27 +00:00
const calendarWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
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(() => {
2019-01-12 03:33:27 +00:00
calendarWrapper
.findAll('.ant-calendar-date')
.at(0)
.trigger('click');
});
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();
const wrapper = mount(
{
render() {
return (
<DatePicker
showTime
open
onChange={onChangeFn}
onOk={onOkFn}
onOpenChange={onOpenChangeFn}
defaultValue={moment()}
/>
);
},
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
2019-01-12 03:33:27 +00:00
const calendarWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
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(() => {
2019-01-12 03:33:27 +00:00
calendarWrapper.find('.ant-calendar-ok-btn').trigger('click');
});
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();
const wrapper = mount(
{
render() {
return <DatePicker 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
2019-01-12 03:33:27 +00:00
const calendarWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
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(() => {
2019-01-12 03:33:27 +00:00
calendarWrapper.find('.ant-calendar-today-btn').trigger('click');
});
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 () => {
const wrapper = mount(
{
2019-01-12 03:33:27 +00:00
render() {
return <DatePicker showTime={{ use12Hours: true }} open />;
2018-06-09 05:14:14 +00:00
},
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
const calendarWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
},
{ sync: false },
);
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(calendarWrapper.findAll('.ant-calendar-time-picker-column-4').length).toBe(0);
});
calendarWrapper
.findAll('.ant-calendar-time-picker-btn')
.at(0)
.trigger('click');
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(calendarWrapper.findAll('.ant-calendar-time-picker-column-4').length).toBe(1);
});
});
});
2018-06-09 05:14:14 +00:00
describe('RangePicker with showTime', () => {
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();
2018-06-09 05:14:14 +00:00
const wrapper = mount(
{
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
2019-01-12 03:33:27 +00:00
const calendarWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
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(() => {
2019-01-12 03:33:27 +00:00
expect(calendarWrapper.find('.ant-calendar-time-picker-btn').classes()).toContain(
'ant-calendar-time-picker-btn-disabled',
);
expect(calendarWrapper.find('.ant-calendar-ok-btn').classes()).toContain(
'ant-calendar-ok-btn-disabled',
);
});
calendarWrapper
.findAll('.ant-calendar-date')
.at(10)
.trigger('click');
calendarWrapper
.findAll('.ant-calendar-date')
.at(11)
.trigger('click');
2018-06-09 05:14:14 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(calendarWrapper.find('.ant-calendar-time-picker-btn').classes()).not.toContain(
'ant-calendar-time-picker-btn-disabled',
);
expect(calendarWrapper.find('.ant-calendar-ok-btn').classes()).not.toContain(
'ant-calendar-ok-btn-disabled',
);
});
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();
const wrapper = mount(
{
render() {
return (
<RangePicker
showTime
open
onOk={onOkFn}
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
2019-01-12 03:33:27 +00:00
const calendarWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
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(() => {
2019-01-12 03:33:27 +00:00
calendarWrapper
.findAll('.ant-calendar-date')
.at(10)
.trigger('click');
calendarWrapper
.findAll('.ant-calendar-date')
.at(11)
.trigger('click');
});
onChangeFn.mockClear();
calendarWrapper.find('.ant-calendar-ok-btn').trigger('click');
expect(onOkFn).toHaveBeenCalled();
expect(onOpenChangeFn).toHaveBeenCalledWith(false);
expect(onChangeFn).not.toHaveBeenCalled();
});
});