Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(daypicker): Open daypicker at top of calendar. Fix keyboard navi… #119

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ window.matchMedia = jest.fn().mockImplementation(query => ({
removeListener: jest.fn()
}));

window.scrollTo = jest.fn();

configure({ adapter: new Adapter() });
119 changes: 109 additions & 10 deletions src/components/DayPicker/__tests__/Day.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ jest.mock('../helpers', () => ({
getShouldSelectAsStartDate: jest.fn(),
getEndDateFromStartDate: jest.fn(),
DAY_CELL_HEIGHT_DESKTOP: 90,
DAY_CELL_HEIGHT_MOBILE: 94
DAY_CELL_HEIGHT_MOBILE: 94,
DAY_NAVIGATION_MAPPING: {
39: 1,
37: -1,
40: 7,
38: -7
}
}));

const DummyIcon = () => <div>Icon</div>;
Expand All @@ -29,7 +35,7 @@ describe('Day', () => {
month: 'January',
year: 2018,
onDayClick: noop,
onDayNavigate: noop
focusDateElement: noop
};

const props = {
Expand Down Expand Up @@ -198,13 +204,13 @@ describe('Day', () => {

describe('handleKeyDown()', () => {
const onDayClickMock = jest.fn();
const onDayNavigateMock = jest.fn();
const focusDateElementMock = jest.fn();
const preventDefaultMock = jest.fn();
const stopPropagationMock = jest.fn();

afterEach(() => {
onDayClickMock.mockReset();
onDayNavigateMock.mockReset();
focusDateElementMock.mockReset();
preventDefaultMock.mockReset();
stopPropagationMock.mockReset();
});
Expand Down Expand Up @@ -237,14 +243,107 @@ describe('Day', () => {
expect(stopPropagationMock.mock.calls.length).toBe(1);
});

it('calls this.props.onDayNavigate with correct args', () => {
it('calls focusDateElement when key is the UP key', () => {
component = shallow(
<Day {...defaultProps} {...props} onDayNavigate={onDayNavigateMock} />
<Day
{...defaultProps}
{...props}
focusDateElement={focusDateElementMock}
/>
);
component.instance().handleKeyDown({
keyCode: 38,
preventDefault: preventDefaultMock,
stopPropagation: stopPropagationMock
});
expect(focusDateElementMock.mock.calls.length).toBe(1);
expect(focusDateElementMock.mock.calls[0][0]).toEqual(
new Date(2019, 3, 13, 0, 0, 0)
);
component.instance().handleKeyDown({ keyCode: 555 });
expect(onDayNavigateMock.mock.calls.length).toBe(1);
expect(onDayNavigateMock.mock.calls[0][0]).toBe(date.getTime());
expect(onDayNavigateMock.mock.calls[0][1]).toBe(555);
expect(preventDefaultMock.mock.calls.length).toBe(1);
expect(stopPropagationMock.mock.calls.length).toBe(1);
});

it('calls focusDateElement when key is the DOWN key', () => {
component = shallow(
<Day
{...defaultProps}
{...props}
focusDateElement={focusDateElementMock}
/>
);
component.instance().handleKeyDown({
keyCode: 40,
preventDefault: preventDefaultMock,
stopPropagation: stopPropagationMock
});
expect(focusDateElementMock.mock.calls.length).toBe(1);
expect(focusDateElementMock.mock.calls[0][0]).toEqual(
new Date(2019, 3, 27, 0, 0, 0)
);
expect(preventDefaultMock.mock.calls.length).toBe(1);
expect(stopPropagationMock.mock.calls.length).toBe(1);
});

it('calls focusDateElement when key is the LEFT key', () => {
component = shallow(
<Day
{...defaultProps}
{...props}
focusDateElement={focusDateElementMock}
/>
);
component.instance().handleKeyDown({
keyCode: 37,
preventDefault: preventDefaultMock,
stopPropagation: stopPropagationMock
});
expect(focusDateElementMock.mock.calls.length).toBe(1);
expect(focusDateElementMock.mock.calls[0][0]).toEqual(
new Date(2019, 3, 19, 0, 0, 0)
);
expect(preventDefaultMock.mock.calls.length).toBe(1);
expect(stopPropagationMock.mock.calls.length).toBe(1);
});

it('calls focusDateElement when key is the RIGHT key', () => {
component = shallow(
<Day
{...defaultProps}
{...props}
focusDateElement={focusDateElementMock}
/>
);
component.instance().handleKeyDown({
keyCode: 39,
preventDefault: preventDefaultMock,
stopPropagation: stopPropagationMock
});
expect(focusDateElementMock.mock.calls.length).toBe(1);
expect(focusDateElementMock.mock.calls[0][0]).toEqual(
new Date(2019, 3, 21, 0, 0, 0)
);
expect(preventDefaultMock.mock.calls.length).toBe(1);
expect(stopPropagationMock.mock.calls.length).toBe(1);
});

it('does nothing when key code is something else', () => {
component = shallow(
<Day
{...defaultProps}
{...props}
focusDateElement={focusDateElementMock}
/>
);
component.instance().handleKeyDown({
keyCode: 555,
preventDefault: preventDefaultMock,
stopPropagation: stopPropagationMock
});
expect(focusDateElementMock.mock.calls.length).toBe(0);
expect(onDayClickMock.mock.calls.length).toBe(0);
expect(preventDefaultMock.mock.calls.length).toBe(0);
expect(stopPropagationMock.mock.calls.length).toBe(0);
});
});

Expand Down
12 changes: 0 additions & 12 deletions src/components/DayPicker/__tests__/DayPicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,6 @@ describe('DayPicker', () => {
});
});

describe('renderMonth()', () => {
it('renders correctly', () => {
component = shallow(<DayPicker {...props} />);

const renderedMonth = shallow(
component.instance().renderMonth({ index: 1, style: {} }, true)
);

expect(renderedMonth).toMatchSnapshot();
});
});

describe('renderButtonDates()', () => {
it('does not render if start date is not selected', () => {
component = shallow(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { mount } from 'enzyme';
import DisclaimerMessages from '..';
import DisclaimerMessages from '../components/DisclaimerMessages';

describe('DisclaimerMessages', () => {
it('Disclaimer Message renders correctly', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/components/DayPicker/__tests__/Header.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { shallow } from 'enzyme';
import Header from '../components/Header';
import noop from '../../../utils/noop';

const start = new Date(2019, 3, 21, 0, 0, 0, 0);
const end = new Date(2019, 3, 24, 0, 0, 0, 0);
Expand All @@ -16,6 +17,7 @@ describe('Header', () => {
firstDayOfWeek={1}
rowStyles={{}}
dayLabels={['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']}
onKeyDown={noop}
/>
);

Expand All @@ -41,6 +43,7 @@ describe('Header', () => {
endPlaceholder="return when?"
Icon={CalendarIcon}
isDateRange={false}
onKeyDown={noop}
/>
);

Expand Down
9 changes: 7 additions & 2 deletions src/components/DayPicker/__tests__/Month.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ describe('Month', () => {
let component;

const requiredProps = {
month: new Date(2019, 3, 20, 0, 0, 0),
months: [new Date(2019, 3, 20, 0, 0, 0)],
monthIndex: 0,
disabledBefore: new Date(2019, 3, 2, 0, 0, 0),
disabledAfter: new Date(2019, 3, 26, 0, 0, 0),
today: new Date(2019, 3, 15, 0, 0, 0).getTime(),
firstDayOfWeek: 1,
onDayClick: noop,
onDayNavigate: noop,
focusDateElement: noop,
days: [
{
date: new Date(2019, 3, 20, 0, 0, 0),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading