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

187 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-01-12 11:33:27 +08:00
import { mount } from '@vue/test-utils';
import { asyncExpect } from '@/tests/utils';
import moment from 'moment';
import MockDate from 'mockdate';
import DatePicker from '..';
2018-06-09 13:14:14 +08:00
import {
selectDateFromBody,
openPanel,
clearInput,
nextYear,
nextMonth,
hasSelected,
$$,
2019-01-12 11:33:27 +08:00
} from './utils';
import focusTest from '../../../tests/shared/focusTest';
2018-06-09 13:14:14 +08:00
describe('DatePicker', () => {
2019-01-12 11:33:27 +08:00
focusTest(DatePicker);
2018-06-09 13:14:14 +08:00
beforeEach(() => {
2019-01-12 11:33:27 +08:00
document.body.outerHTML = '';
MockDate.set(moment('2016-11-22'));
});
2018-06-09 13:14:14 +08:00
afterEach(() => {
2019-01-12 11:33:27 +08:00
MockDate.reset();
});
2018-06-09 13:14:14 +08:00
it('prop locale should works', async () => {
const locale = {
lang: {
placeholder: 'Избери дата',
2019-01-12 11:33:27 +08:00
rangePlaceholder: ['Начална дата', 'Крайна дата'],
2018-06-09 13:14:14 +08:00
today: 'Днес',
now: 'Сега',
backToToday: 'Към днес',
ok: 'Добре',
clear: 'Изчистване',
month: 'Месец',
year: 'Година',
timeSelect: 'Избор на час',
dateSelect: 'Избор на дата',
monthSelect: 'Избор на месец',
yearSelect: 'Избор на година',
decadeSelect: 'Десетилетие',
previousMonth: 'Предишен месец (PageUp)',
nextMonth: 'Следващ месец (PageDown)',
previousYear: 'Последна година (Control + left)',
nextYear: 'Следваща година (Control + right)',
previousDecade: 'Предишно десетилетие',
nextDecade: 'Следващо десетилетие',
previousCentury: 'Последен век',
nextCentury: 'Следващ век',
yearFormat: 'YYYY',
dateFormat: 'D M YYYY',
dayFormat: 'D',
dateTimeFormat: 'D M YYYY HH:mm:ss',
monthBeforeYear: true,
},
timePickerLocale: {
placeholder: 'Избор на час',
},
2019-01-12 11:33:27 +08:00
};
const birthday = moment('2000-01-01', 'YYYY-MM-DD');
2018-06-09 13:14:14 +08:00
const wrapper = mount({
2019-01-12 11:33:27 +08:00
render() {
return <DatePicker open locale={locale} value={birthday} />;
2018-06-09 13:14:14 +08:00
},
2019-01-12 11:33:27 +08:00
});
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(wrapper.html()).toMatchSnapshot();
});
});
2018-06-09 13:14:14 +08:00
// Fix https://github.com/ant-design/ant-design/issues/8885
it('control value after panel closed', async () => {
const Test = {
2019-01-12 11:33:27 +08:00
data() {
2018-06-09 13:14:14 +08:00
return {
cleared: false,
value: moment(),
2019-01-12 11:33:27 +08:00
};
2018-06-09 13:14:14 +08:00
},
methods: {
2019-01-12 11:33:27 +08:00
onChange(value) {
let cleared = this.cleared;
2018-06-09 13:14:14 +08:00
if (cleared) {
2019-01-12 11:33:27 +08:00
value = moment(moment(value).format('YYYY-MM-DD 12:12:12'));
cleared = false;
2018-06-09 13:14:14 +08:00
}
if (!value) {
2019-01-12 11:33:27 +08:00
cleared = true;
2018-06-09 13:14:14 +08:00
}
2019-01-12 11:33:27 +08:00
this.value = value;
this.cleared = cleared;
2018-06-09 13:14:14 +08:00
},
},
2019-01-12 11:33:27 +08:00
render() {
2018-06-09 13:14:14 +08:00
return (
<DatePicker
showTime
value={this.value}
2019-01-12 11:33:27 +08:00
format="YYYY-MM-DD HH:mm:ss"
2018-06-09 13:14:14 +08:00
onChange={this.onChange}
/>
2019-01-12 11:33:27 +08:00
);
2018-06-09 13:14:14 +08:00
},
2019-01-12 11:33:27 +08:00
};
2018-06-09 13:14:14 +08:00
2019-01-12 11:33:27 +08:00
const wrapper = mount(Test, { sync: false, attachToDocument: true });
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
// clear input
2019-01-12 11:33:27 +08:00
clearInput(wrapper);
});
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
openPanel(wrapper);
});
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
selectDateFromBody(moment('2016-11-13'));
}, 0);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect($$('.ant-calendar-input')[0].value).toBe('2016-11-13 12:12:12');
});
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
selectDateFromBody(moment('2016-11-14'));
});
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect($$('.ant-calendar-input')[0].value).toBe('2016-11-14 12:12:12');
});
await asyncExpect(() => {});
});
2018-06-09 13:14:14 +08:00
it('triggers onChange only when date was selected', async () => {
2019-01-12 11:33:27 +08:00
const handleChange = jest.fn();
const wrapper = mount(
{
render() {
return <DatePicker onChange={handleChange} />;
},
2018-06-09 13:14:14 +08:00
},
2019-01-12 11:33:27 +08:00
{ sync: false, attachToDocument: true },
);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
openPanel(wrapper);
}, 0);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
nextYear();
}, 1000);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(handleChange).not.toBeCalled();
}, 0);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
nextMonth();
}, 0);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(handleChange).not.toBeCalled();
});
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
selectDateFromBody(moment('2017-12-22'));
}, 1000);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(handleChange).toBeCalled();
}, 0);
});
2018-06-09 13:14:14 +08:00
it('clear input', async () => {
2019-01-12 11:33:27 +08:00
const wrapper = mount(DatePicker, { sync: false, attachToDocument: true });
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
openPanel(wrapper);
}, 0);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
selectDateFromBody(moment('2016-11-23'));
}, 100);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
clearInput(wrapper);
}, 1000);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
openPanel(wrapper);
}, 0);
2018-06-09 13:14:14 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(hasSelected(wrapper, moment('2016-11-22'))).toBe(true);
}, 0);
});
});