2017-12-17 17:11:54 +08:00
|
|
|
|
import React from 'react';
|
|
|
|
|
import { mount } from 'enzyme';
|
2017-12-19 16:06:19 +08:00
|
|
|
|
import moment from 'moment';
|
|
|
|
|
import MockDate from 'mockdate';
|
2017-11-30 09:54:31 +08:00
|
|
|
|
import DatePicker from '..';
|
2018-12-07 20:02:01 +08:00
|
|
|
|
import { selectDate, openPanel, clearInput, nextYear, nextMonth, hasSelected } from './utils';
|
2017-11-30 09:54:31 +08:00
|
|
|
|
import focusTest from '../../../tests/shared/focusTest';
|
|
|
|
|
|
|
|
|
|
describe('DatePicker', () => {
|
|
|
|
|
focusTest(DatePicker);
|
2017-12-17 17:11:54 +08:00
|
|
|
|
|
2018-02-01 16:12:42 +08:00
|
|
|
|
beforeEach(() => {
|
2018-03-21 12:27:41 +08:00
|
|
|
|
MockDate.set(moment('2016-11-22'));
|
2018-02-01 16:12:42 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
MockDate.reset();
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-25 11:28:31 +08:00
|
|
|
|
it('support name prop', () => {
|
|
|
|
|
const wrapper = mount(<DatePicker name="bamboo" />);
|
|
|
|
|
expect(wrapper.find('input').props().name).toBe('bamboo');
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-17 17:11:54 +08:00
|
|
|
|
it('prop locale should works', () => {
|
|
|
|
|
const locale = {
|
|
|
|
|
lang: {
|
|
|
|
|
placeholder: 'Избери дата',
|
2018-12-07 20:02:01 +08:00
|
|
|
|
rangePlaceholder: ['Начална дата', 'Крайна дата'],
|
2017-12-17 17:11:54 +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: 'Избор на час',
|
|
|
|
|
},
|
|
|
|
|
};
|
2017-12-19 16:06:19 +08:00
|
|
|
|
const birthday = moment('2000-01-01', 'YYYY-MM-DD');
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const wrapper = mount(<DatePicker open locale={locale} value={birthday} />);
|
2017-12-17 17:11:54 +08:00
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-02-01 16:12:42 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Fix https://github.com/ant-design/ant-design/issues/8885
|
|
|
|
|
it('control value after panel closed', () => {
|
|
|
|
|
class Test extends React.Component {
|
|
|
|
|
state = {
|
|
|
|
|
cleared: false,
|
|
|
|
|
value: moment(),
|
2018-12-07 20:02:01 +08:00
|
|
|
|
};
|
2018-02-01 16:12:42 +08:00
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
|
onChange = value => {
|
2018-06-22 21:05:13 +08:00
|
|
|
|
let { cleared } = this.state;
|
2018-02-01 16:12:42 +08:00
|
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
|
let newValue = value;
|
2018-02-01 16:12:42 +08:00
|
|
|
|
if (cleared) {
|
2018-11-28 15:00:03 +08:00
|
|
|
|
newValue = moment(moment(value).format('YYYY-MM-DD 12:12:12'));
|
2018-02-01 16:12:42 +08:00
|
|
|
|
cleared = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
|
if (!newValue) {
|
2018-02-01 16:12:42 +08:00
|
|
|
|
cleared = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
|
this.setState({ value: newValue, cleared });
|
2018-12-07 20:02:01 +08:00
|
|
|
|
};
|
2018-02-01 16:12:42 +08:00
|
|
|
|
|
|
|
|
|
render() {
|
2018-06-22 21:05:13 +08:00
|
|
|
|
const { value } = this.state;
|
2018-02-01 16:12:42 +08:00
|
|
|
|
return (
|
|
|
|
|
<DatePicker
|
|
|
|
|
showTime
|
2018-06-22 21:05:13 +08:00
|
|
|
|
value={value}
|
2018-02-01 16:12:42 +08:00
|
|
|
|
format="YYYY-MM-DD HH:mm:ss"
|
|
|
|
|
onChange={this.onChange}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const wrapper = mount(<Test />);
|
|
|
|
|
// clear input
|
|
|
|
|
clearInput(wrapper);
|
|
|
|
|
openPanel(wrapper);
|
|
|
|
|
selectDate(wrapper, moment('2016-11-13'));
|
|
|
|
|
expect(wrapper.find('.ant-calendar-input').getDOMNode().value).toBe('2016-11-13 12:12:12');
|
|
|
|
|
selectDate(wrapper, moment('2016-11-14'));
|
|
|
|
|
expect(wrapper.find('.ant-calendar-input').getDOMNode().value).toBe('2016-11-14 12:12:12');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('triggers onChange only when date was selected', () => {
|
|
|
|
|
const handleChange = jest.fn();
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const wrapper = mount(<DatePicker onChange={handleChange} />);
|
2018-02-01 16:12:42 +08:00
|
|
|
|
openPanel(wrapper);
|
|
|
|
|
nextYear(wrapper);
|
|
|
|
|
expect(handleChange).not.toBeCalled();
|
|
|
|
|
nextMonth(wrapper);
|
|
|
|
|
expect(handleChange).not.toBeCalled();
|
|
|
|
|
selectDate(wrapper, moment('2017-12-22'));
|
|
|
|
|
expect(handleChange).toBeCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('clear input', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const wrapper = mount(<DatePicker />);
|
2018-02-01 16:12:42 +08:00
|
|
|
|
openPanel(wrapper);
|
|
|
|
|
selectDate(wrapper, moment('2016-11-23'));
|
|
|
|
|
clearInput(wrapper);
|
|
|
|
|
openPanel(wrapper);
|
|
|
|
|
expect(hasSelected(wrapper, moment('2016-11-22'))).toBe(true);
|
2017-12-17 17:11:54 +08:00
|
|
|
|
});
|
2018-06-15 03:08:51 +08:00
|
|
|
|
|
|
|
|
|
it('sets data attributes on input', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const wrapper = mount(<DatePicker data-test="test-id" data-id="12345" />);
|
2018-06-15 03:08:51 +08:00
|
|
|
|
const input = wrapper.find('.ant-calendar-picker-input').getDOMNode();
|
|
|
|
|
expect(input.getAttribute('data-test')).toBe('test-id');
|
|
|
|
|
expect(input.getAttribute('data-id')).toBe('12345');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sets aria attributes on input', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const wrapper = mount(<DatePicker aria-label="some-label" aria-labelledby="label-id" />);
|
2018-06-15 03:08:51 +08:00
|
|
|
|
const input = wrapper.find('.ant-calendar-picker-input').getDOMNode();
|
|
|
|
|
expect(input.getAttribute('aria-label')).toBe('some-label');
|
|
|
|
|
expect(input.getAttribute('aria-labelledby')).toBe('label-id');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sets role attribute on input', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const wrapper = mount(<DatePicker role="search" />);
|
2018-06-15 03:08:51 +08:00
|
|
|
|
const input = wrapper.find('.ant-calendar-picker-input').getDOMNode();
|
|
|
|
|
expect(input.getAttribute('role')).toBe('search');
|
|
|
|
|
});
|
2018-07-25 16:31:18 +08:00
|
|
|
|
|
|
|
|
|
it('changes year/month when under control', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const wrapper = mount(<DatePicker value={moment('2018-07-01')} />);
|
2018-07-25 16:31:18 +08:00
|
|
|
|
openPanel(wrapper);
|
|
|
|
|
expect(wrapper.find('.ant-calendar-my-select').text()).toBe('Jul2018');
|
|
|
|
|
wrapper.find('.ant-calendar-prev-year-btn').simulate('click');
|
|
|
|
|
wrapper.find('.ant-calendar-prev-month-btn').simulate('click');
|
|
|
|
|
expect(wrapper.find('.ant-calendar-my-select').text()).toBe('Jun2017');
|
|
|
|
|
});
|
2018-11-25 02:30:54 +08:00
|
|
|
|
|
|
|
|
|
it('disabled date', () => {
|
|
|
|
|
function disabledDate(current) {
|
|
|
|
|
return current && current < moment().endOf('day');
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const wrapper = mount(<DatePicker disabledDate={disabledDate} />);
|
2018-11-25 02:30:54 +08:00
|
|
|
|
|
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
|
});
|
2018-12-24 13:28:13 +08:00
|
|
|
|
|
|
|
|
|
it('extra footer works', () => {
|
|
|
|
|
const wrapper = mount(
|
|
|
|
|
<DatePicker renderExtraFooter={mode => <span className="extra-node">{mode}</span>} />,
|
|
|
|
|
);
|
|
|
|
|
openPanel(wrapper);
|
|
|
|
|
|
|
|
|
|
let extraNode = wrapper.find('.extra-node');
|
|
|
|
|
expect(extraNode.length).toBe(1);
|
|
|
|
|
expect(extraNode.text()).toBe('date');
|
|
|
|
|
|
|
|
|
|
wrapper
|
|
|
|
|
.find('.ant-calendar-month-select')
|
|
|
|
|
.hostNodes()
|
|
|
|
|
.simulate('click');
|
|
|
|
|
extraNode = wrapper.find('.ant-calendar-month-panel .extra-node');
|
|
|
|
|
expect(extraNode.length).toBe(1);
|
|
|
|
|
expect(extraNode.text()).toBe('month');
|
|
|
|
|
|
|
|
|
|
wrapper
|
|
|
|
|
.find('.ant-calendar-year-select')
|
|
|
|
|
.hostNodes()
|
|
|
|
|
.simulate('click');
|
|
|
|
|
extraNode = wrapper.find('.ant-calendar-year-panel .extra-node');
|
|
|
|
|
expect(extraNode.length).toBe(1);
|
|
|
|
|
expect(extraNode.text()).toBe('year');
|
|
|
|
|
|
|
|
|
|
wrapper
|
|
|
|
|
.find('.ant-calendar-year-panel-decade-select')
|
|
|
|
|
.hostNodes()
|
|
|
|
|
.simulate('click');
|
|
|
|
|
extraNode = wrapper.find('.ant-calendar-decade-panel .extra-node');
|
|
|
|
|
expect(extraNode.length).toBe(1);
|
|
|
|
|
expect(extraNode.text()).toBe('decade');
|
|
|
|
|
});
|
2019-03-01 13:50:28 +08:00
|
|
|
|
|
|
|
|
|
it('supports multiple formats', () => {
|
|
|
|
|
const wrapper = mount(<DatePicker format={['DD/MM/YYYY', 'DD/MM/YY']} />);
|
|
|
|
|
openPanel(wrapper);
|
|
|
|
|
wrapper.find('.ant-calendar-input').simulate('change', { target: { value: '02/07/18' } });
|
|
|
|
|
expect(wrapper.find('.ant-calendar-picker-input').getDOMNode().value).toBe('02/07/2018');
|
|
|
|
|
});
|
2017-11-30 09:54:31 +08:00
|
|
|
|
});
|