Commit 4b59f66e by Ari Rizzitano

more dropdown tests

parent 5e8bc736
...@@ -13,5 +13,11 @@ ...@@ -13,5 +13,11 @@
}, },
"env": { "env": {
"jest": true "jest": true
},
"overrides": {
"files": ["*.stories.jsx", "*.test.jsx"],
"rules": {
"import/no-extraneous-dependencies": "off" // storybook & enzyme should stay devDependencies
}
} }
} }
/* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable import/no-extraneous-dependencies */
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow, mount } from 'enzyme';
import Dropdown from './index'; import Dropdown, { triggerKeys } from './index';
const props = { const props = {
title: 'Example', title: 'Example',
...@@ -12,48 +12,55 @@ const props = { ...@@ -12,48 +12,55 @@ const props = {
], ],
}; };
const menuOpen = (isOpen, wrapper) => {
expect(wrapper.hasClass('show')).toEqual(isOpen);
expect(wrapper.find('[type="button"]').prop('aria-expanded')).toEqual(isOpen);
expect(wrapper.find('[aria-hidden=false]').exists()).toEqual(isOpen);
};
describe('<Dropdown />', () => { describe('<Dropdown />', () => {
it('renders correctly', () => { describe('renders', () => {
const wrapper = shallow( const wrapper = shallow(
<Dropdown <Dropdown
{...props} {...props}
/>, />,
); );
const menu = wrapper.find('ul'); const menu = wrapper.find('ul');
const button = wrapper.find('[type="button"]'); const button = wrapper.find('[type="button"]');
expect(button.exists()).toEqual(true); it('with menu and toggle', () => {
expect(button.prop('aria-expanded')).toEqual(false); expect(button.exists()).toEqual(true);
expect(menu.prop('aria-label')).toEqual(props.title);
expect(menu.exists()).toEqual(true); expect(menu.exists()).toEqual(true);
expect(menu.find('li')).toHaveLength(2); expect(menu.find('li')).toHaveLength(2);
expect(menu.prop('aria-label')).toEqual(props.title); });
expect(menu.prop('aria-hidden')).toEqual(true);
});
it('renders correctly', () => {
const wrapper = shallow(
<Dropdown
{...props}
/>,
);
expect(wrapper.find('[type="button"]').exists()).toEqual(true); it('with menu closed', () => {
expect(wrapper.find('li')).toHaveLength(2); menuOpen(false, wrapper);
expect(wrapper.find('[aria-expanded=false]').exists()).toEqual(true); });
}); });
it('opens on click', () => { describe('opens', () => {
const wrapper = shallow( it('on click', () => {
<Dropdown const wrapper = mount(
{...props} <Dropdown
/>, {...props}
); />,
);
const button = wrapper.find('[type="button"]'); wrapper.find('[type="button"]').simulate('click');
menuOpen(true, wrapper);
});
button.simulate('click'); triggerKeys.OPEN_MENU.forEach((key) => {
expect(wrapper.find('[aria-hidden=false]').exists()).toEqual(true); it(`on ${key}`, () => {
const wrapper = mount(
<Dropdown
{...props}
/>,
);
wrapper.find('[type="button"]').simulate('keyDown', { key });
menuOpen(true, wrapper);
});
});
}); });
}); });
...@@ -7,7 +7,7 @@ import borders from 'bootstrap/scss/utilities/_borders.scss'; ...@@ -7,7 +7,7 @@ import borders from 'bootstrap/scss/utilities/_borders.scss';
import pc from '../utils/base-styles.scss'; import pc from '../utils/base-styles.scss';
import Button from '../Button'; import Button from '../Button';
const triggerKeys = { export const triggerKeys = {
OPEN_MENU: ['ArrowDown', 'Space'], OPEN_MENU: ['ArrowDown', 'Space'],
CLOSE_MENU: ['Escape'], CLOSE_MENU: ['Escape'],
NAVIGATE_DOWN: ['ArrowDown', 'Tab'], NAVIGATE_DOWN: ['ArrowDown', 'Tab'],
...@@ -45,7 +45,7 @@ class Dropdown extends React.Component { ...@@ -45,7 +45,7 @@ class Dropdown extends React.Component {
} }
componentDidUpdate() { componentDidUpdate() {
if (this.state.open && this.menuItems.length > 0) { if (this.state.open) {
this.menuItems[this.state.focusIndex].focus(); this.menuItems[this.state.focusIndex].focus();
} else if (this.toggleElem) { } else if (this.toggleElem) {
this.toggleElem.focus(); this.toggleElem.focus();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment