Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('calls onChange with provided date if ENTER key is used', () => {
const { getByTestId } = render(
);
const endInput = getByTestId('end');
fireEvent.change(endInput, { target: { value: 'January 4th, 2019' } });
fireEvent.keyDown(endInput, { keyCode: KEY_CODES.ENTER });
expect(onChangeSpy).toHaveBeenCalledWith({
startValue: DEFAULT_START_VALUE,
endValue: new Date(2019, 0, 4)
});
});
it('calls onChange with provided date if ENTER key is used', () => {
const { getByTestId } = render(
);
const startInput = getByTestId('start');
fireEvent.change(startInput, { target: { value: 'January 4th, 2019' } });
fireEvent.keyDown(startInput, { keyCode: KEY_CODES.ENTER });
expect(onChangeSpy).toHaveBeenCalledWith({
startValue: new Date(2019, 0, 4),
endValue: DEFAULT_END_VALUE
});
});
it('closes datepicker when correct keys are used', () => {
const { getByTestId, queryByTestId } = render(
);
const input = getByTestId('input');
fireEvent.mouseDown(getByTestId('input'));
fireEvent.click(input);
fireEvent.keyDown(input, { keyCode: KEY_CODES.ESCAPE });
expect(queryByTestId('datepicker-menu')).toBeNull();
fireEvent.mouseDown(getByTestId('input'));
fireEvent.click(input);
fireEvent.keyDown(input, { keyCode: KEY_CODES.ENTER });
expect(queryByTestId('datepicker-menu')).toBeNull();
});
it('toggles on ENTER', () => {
const { getAllByTestId } = render();
const firstTrigger = getAllByTestId('trigger')[0];
fireEvent.keyDown(firstTrigger, { keyCode: KEY_CODES.ENTER });
expect(firstTrigger).toHaveAttribute('aria-expanded', 'true');
});
it('shows on ENTER', () => {
const { getAllByTestId } = render();
const firstTrigger = getAllByTestId('trigger')[0];
const firstPanel = getAllByTestId('panel')[0];
fireEvent.keyDown(firstTrigger, { keyCode: KEY_CODES.ENTER });
expect(firstPanel).toHaveAttribute('aria-hidden', 'false');
});
it('does not close modal when other keys are pressed', () => {
const { getByTestId } = render();
fireEvent.keyDown(getByTestId('modal'), { keyCode: KEY_CODES.ENTER });
expect(onCloseSpy).not.toHaveBeenCalled();
});
});
it('focuses first page when visibility is lost', () => {
const { container } = render();
const paginationWrapper = container.firstChild;
const previousPage = container.firstChild.children[0];
fireEvent.focus(previousPage);
fireEvent.keyDown(previousPage, { keyCode: KEY_CODES.ENTER });
expect(container.firstChild.children[1]).toHaveClass('is-focused');
expect(container.firstChild.children[2]).toHaveClass('is-current');
});
});
it('selects currently focused item', () => {
const { getAllByTestId } = render();
const [item] = getAllByTestId('item');
fireEvent.focus(item);
fireEvent.keyDown(item, { keyCode: KEY_CODES.ENTER });
expect(item).toHaveAttribute('data-selected', 'true');
});
});
onKeyDown: e => {
switch (e.keyCode) {
case KEY_CODES.ESCAPE:
case KEY_CODES.ENTER:
dispatch({ type: 'CLOSE' });
break;
case KEY_CODES.UP:
case KEY_CODES.DOWN:
case KEY_CODES.SPACE:
dispatch({ type: 'OPEN' });
break;
}
},
autoComplete: 'off',
(e: React.KeyboardEvent) => {
if (e.keyCode === KEY_CODES.ENTER) {
dispatch({ type: 'END_BLUR' });
e.preventDefault();
}
(props.children as any).props.onKeyDown && (props.children as any).props.onKeyDown(e);
},
[dispatch, props.children]