Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 97 additions & 19 deletions src/DatePicker/DatePicker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,25 @@ describe('<DatePicker />', () => {

test('open/close calendar', () => {
wrapper = mount(defaultDatePicker);
// check to make sure calendar is hidden
expect(wrapper.state('hidden')).toBeTruthy();

// click to show calendar
wrapper.find('input[type="text"]').simulate('click', { type: 'input' });

// check to make sure calendar is shown
expect(wrapper.state('hidden')).toBeFalsy();

// clicking on input text should keep calendar displayed
wrapper.find('input[type="text"]').simulate('click', { type: 'input' });

// check to make sure calendar is shown
expect(wrapper.state('hidden')).toBeFalsy();

wrapper.instance().componentWillMount();

let event = new MouseEvent('mousedown', {
target: document.querySelector('body')
});
document.dispatchEvent(event);

expect(wrapper.state('hidden')).toBeTruthy();

// click to show calendar
wrapper.find('input[type="text"]').simulate('click', { type: '' });

// check to make sure calendar is shown
expect(wrapper.state('hidden')).toBeFalsy();
});

Expand All @@ -62,34 +60,61 @@ describe('<DatePicker />', () => {
//open date picker calendar
expect(wrapper.state('hidden')).toBeTruthy();

// click to show calendar
wrapper.find('input[type="text"]').simulate('click', { type: 'input' });

// check to make sure calendar is shown
expect(wrapper.state('hidden')).toBeFalsy();

wrapper.instance().componentWillMount();

// click on body element
let event = new MouseEvent('mousedown', {
target: document.querySelector('body')
});
document.dispatchEvent(event);

// check to make sure calendar is hidden
expect(wrapper.state('hidden')).toBeTruthy();

// show date picker, select date range then close
wrapper.find('input[type="text"]').simulate('click', { type: '' });

// check to make sure calendar is shown
expect(wrapper.state('hidden')).toBeFalsy();
});

test('start date and end date range', () => {
wrapper = mount(rangeDatePicker);
// set dates
let startRangeDate = new Date();
let endRangeDate = new Date();
endRangeDate.setDate(endRangeDate.getDate() + 3);

let arrDates = [startRangeDate, endRangeDate];
wrapper.instance().updateDate(arrDates);

expect(wrapper.state('arrSelectedDates').length).toEqual(2);

// click on body element
let event = new MouseEvent('mousedown', {
target: document.querySelector('body')
});
document.dispatchEvent(event);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the added mousedown event, but then no additional assertions. I'm probably just missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed MouseEvent code since it was not necessary

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still seeing a couple of these in the files changed diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, missed that mouseDown event. Just added an assertion to check that calendar is hidden.


// check to make sure calendar is hidden
expect(wrapper.state('hidden')).toBeTruthy();
});

test('check start date greater than end date for range', () => {
wrapper = mount(rangeDatePicker);
// set dates
let startRangeDate = new Date();
let endRangeDate = new Date();
endRangeDate.setDate(endRangeDate.getDate() + 3);

let arrDates = [startRangeDate, endRangeDate];

// make start date bigger than end date
arrDates = [endRangeDate, startRangeDate];
wrapper.instance().updateDate(arrDates);
Expand All @@ -101,10 +126,37 @@ describe('<DatePicker />', () => {
expect(wrapper.state('formattedDate')).toEqual(switchFormattedDate);
expect(wrapper.state('arrSelectedDates').length).toEqual(2);

// click on body element to hide calendar
let event = new MouseEvent('mousedown', {
target: document.querySelector('body')
});
document.dispatchEvent(event);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the added mousedown event, but then no additional assertions. I'm probably just missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added assertion to check that calendar is hidden


// check to make sure calendar is hidden
expect(wrapper.state('hidden')).toBeTruthy();
});

test('entering invalid range dates', () => {
wrapper = mount(rangeDatePicker);
// set dates
let startRangeDate = new Date();
let endRangeDate = new Date();
endRangeDate.setDate(endRangeDate.getDate() + 3);

let startDate = `${startRangeDate.getMonth() +
1}/${startRangeDate.getDate()}/${startRangeDate.getFullYear()}`;
let endDate = `${endRangeDate.getMonth() +
1}/${endRangeDate.getDate()}/${endRangeDate.getFullYear() + 4000}`;

wrapper.find('input[type="text"]')
.simulate('change', { target: { value: `${startDate}-${endDate}` } });

wrapper.find('input[type="text"]').simulate('keypress', { key: 'Enter' });

expect(wrapper.state('arrSelectedDates')).toEqual('undefined');
});

test('update date method', () => {
test('updateDate method', () => {
// choose one day in default picker
wrapper = mount(defaultDatePicker);
const date = new Date();
Expand Down Expand Up @@ -143,7 +195,7 @@ describe('<DatePicker />', () => {
expect(wrapper.state('arrSelectedDates').length).toEqual(2);
});

test('enter date value', () => {
test('pressing enter key on date input', () => {
wrapper = mount(rangeDatePicker);

let startRangeDate = new Date();
Expand Down Expand Up @@ -175,20 +227,24 @@ describe('<DatePicker />', () => {

wrapper.find('input[type="text"]').simulate('keypress', { key: 'Enter' });

expect(wrapper.state('formattedDate')).toBe('');
expect(wrapper.state('selectedDate')).toBe('undefined');
});

test('enter an invalid date string', () => {
wrapper = mount(defaultDatePicker);
let date = new Date();
wrapper.instance().updateDate(date);
expect(wrapper.state('selectedDate')).toEqual(date);
formattedDate = `${date.getMonth() +
1}/${date.getDate()}/${date.getFullYear()}`;
expect(wrapper.state('formattedDate')).toEqual(formattedDate);
let formattedDate = `${date.getMonth() +
1}/${date.getDate()}/${date.getFullYear() + 4000}`;
wrapper.find('input[type="text"]')
.simulate('change', { target: { value: formattedDate } });

wrapper.find('input[type="text"]').simulate('keypress', { key: 'Enter' });

// press Esc key
wrapper.find('input[type="text"]').simulate('keypress', { key: 'Esc' });
expect(wrapper.state('selectedDate')).toEqual('undefined');
});

test('format date', () => {
test('formatDate method', () => {
wrapper = mount(rangeDatePicker);
let startRangeDate = new Date();
let endRangeDate = new Date();
Expand Down Expand Up @@ -225,6 +281,28 @@ describe('<DatePicker />', () => {
expect(wrapper.instance().formatDate(startRangeDate)).toEqual('');
});

test('enter text string for date', () => {
wrapper = mount(defaultDatePicker);

wrapper.find('input[type="text"]')
.simulate('change', { target: { value: 'May 14th, 2018' } });

wrapper.find('input[type="text"]').simulate('keypress', { key: 'Enter' });

expect(wrapper.state('formattedDate')).toEqual('');
});

test('enter text string for date on date range component', () => {
wrapper = mount(rangeDatePicker);

wrapper.find('input[type="text"]')
.simulate('change', { target: { value: 'May 14th, 2018-May 15th, 2018' } });

wrapper.find('input[type="text"]').simulate('keypress', { key: 'Enter' });

expect(wrapper.state('formattedDate')).toEqual('');
});

test('modify date on change', () => {
wrapper = mount(defaultDatePicker);
wrapper
Expand Down