2018-02-28 17:27:19 +00:00
|
|
|
import React from 'react'
|
2018-02-28 21:05:24 +00:00
|
|
|
import Dropdown from 'shared/components/Dropdown'
|
|
|
|
import DropdownMenu from 'shared/components/DropdownMenu'
|
2018-02-28 21:40:58 +00:00
|
|
|
import DropdownInput from 'shared/components/DropdownInput'
|
2018-02-28 21:05:24 +00:00
|
|
|
|
2018-02-28 17:27:19 +00:00
|
|
|
import {shallow} from 'enzyme'
|
2018-02-28 21:05:24 +00:00
|
|
|
const items = [{text: 'foo'}, {text: 'bar'}]
|
2018-02-28 17:27:19 +00:00
|
|
|
|
|
|
|
const setup = (override = {}) => {
|
|
|
|
const props = {
|
|
|
|
items: [],
|
|
|
|
selected: '',
|
|
|
|
onChoose: jest.fn(),
|
|
|
|
...override,
|
|
|
|
}
|
|
|
|
|
2018-02-28 21:05:24 +00:00
|
|
|
const dropdown = shallow(<Dropdown {...props} />).dive({
|
|
|
|
'data-test': 'dropdown-button',
|
|
|
|
})
|
2018-02-28 17:27:19 +00:00
|
|
|
|
|
|
|
return {
|
2018-02-28 21:05:24 +00:00
|
|
|
dropdown,
|
2018-02-28 17:27:19 +00:00
|
|
|
props,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Components.Shared.Dropdown', () => {
|
|
|
|
describe('rednering', () => {
|
|
|
|
describe('initial render', () => {
|
2018-02-28 21:05:24 +00:00
|
|
|
it('renders the dropdown menu button', () => {
|
|
|
|
const {dropdown} = setup()
|
2018-02-28 17:27:19 +00:00
|
|
|
|
2018-02-28 21:05:24 +00:00
|
|
|
expect(dropdown.exists()).toBe(true)
|
2018-02-28 17:27:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does not show the list', () => {
|
2018-02-28 21:05:24 +00:00
|
|
|
const {dropdown} = setup({items})
|
2018-02-28 17:27:19 +00:00
|
|
|
|
2018-02-28 21:05:24 +00:00
|
|
|
const menu = dropdown.find(DropdownMenu)
|
|
|
|
expect(menu.exists()).toBe(false)
|
2018-02-28 17:27:19 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('user interactions', () => {
|
2018-02-28 21:40:58 +00:00
|
|
|
describe('opening the <DropdownMenu/>', () => {
|
|
|
|
it('shows the menu when clicked', () => {
|
|
|
|
const {dropdown} = setup({items})
|
2018-02-28 17:27:19 +00:00
|
|
|
|
2018-02-28 21:40:58 +00:00
|
|
|
dropdown.simulate('click')
|
2018-02-28 17:27:19 +00:00
|
|
|
|
2018-02-28 21:40:58 +00:00
|
|
|
const menu = dropdown.find(DropdownMenu)
|
|
|
|
expect(dropdown.state().isOpen).toBe(true)
|
|
|
|
expect(menu.exists()).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('hides the menu when clicked twice', () => {
|
|
|
|
const {dropdown} = setup({items})
|
|
|
|
|
|
|
|
// first click
|
|
|
|
dropdown.simulate('click')
|
|
|
|
// second click
|
|
|
|
dropdown.simulate('click')
|
|
|
|
|
|
|
|
const menu = dropdown.find(DropdownMenu)
|
|
|
|
expect(dropdown.state().isOpen).toBe(false)
|
|
|
|
expect(menu.exists()).toBe(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('the <DropdownInput/>', () => {
|
|
|
|
it('does not display the input by default', () => {
|
|
|
|
const {dropdown} = setup()
|
|
|
|
const input = dropdown.find(DropdownInput)
|
|
|
|
|
|
|
|
expect(input.exists()).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('displays the input when provided useAutoCompelete is true', () => {
|
|
|
|
const {dropdown} = setup({items, useAutoComplete: true})
|
|
|
|
const input = dropdown.find(DropdownInput)
|
|
|
|
|
|
|
|
expect(input.exists()).toBe(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('instance methods', () => {
|
|
|
|
describe('applyFilter', () => {
|
|
|
|
it('filters the list by the searchTerm', () => {
|
|
|
|
const {dropdown} = setup({items, useAutoComplete: true})
|
|
|
|
|
|
|
|
dropdown.instance().applyFilter('fo')
|
|
|
|
expect(dropdown.state().filteredItems).toEqual([{text: 'foo'}])
|
|
|
|
})
|
2018-02-28 17:27:19 +00:00
|
|
|
})
|
2018-02-28 21:55:44 +00:00
|
|
|
|
|
|
|
describe('handleFilterChange', () => {
|
|
|
|
it('resets filteredList and searchTerm if the filter is empty', () => {
|
|
|
|
const {dropdown} = setup({items, useAutoComplete: true})
|
|
|
|
const event = {target: {value: ''}}
|
|
|
|
dropdown.instance().applyFilter('fo')
|
|
|
|
|
|
|
|
// assert that the list is filtered
|
|
|
|
expect(dropdown.state().filteredItems).toEqual([{text: 'foo'}])
|
|
|
|
|
|
|
|
dropdown.instance().handleFilterChange(event)
|
|
|
|
const {filteredItems, searchTerm} = dropdown.state()
|
|
|
|
|
|
|
|
expect(filteredItems).toEqual(items)
|
|
|
|
expect(searchTerm).toEqual('')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('handleClickOutside', () => {
|
|
|
|
it('sets isOpen to false', () => {
|
|
|
|
const {dropdown} = setup()
|
|
|
|
dropdown.simulate('click')
|
|
|
|
dropdown.instance().handleClickOutside()
|
|
|
|
|
|
|
|
expect(dropdown.state().isOpen).toBe(false)
|
|
|
|
})
|
|
|
|
})
|
2018-02-28 23:30:20 +00:00
|
|
|
|
|
|
|
describe('handleClick', () => {
|
|
|
|
it('fires the onClick prop', () => {
|
|
|
|
const onClick = jest.fn()
|
|
|
|
const {dropdown} = setup({onClick})
|
|
|
|
dropdown.instance().handleClick()
|
|
|
|
|
|
|
|
expect(onClick).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('toggles the isOpen', () => {
|
|
|
|
const {dropdown} = setup()
|
|
|
|
|
|
|
|
dropdown.instance().handleClick()
|
|
|
|
expect(dropdown.state().isOpen).toBe(true)
|
|
|
|
|
|
|
|
dropdown.instance().handleClick()
|
|
|
|
expect(dropdown.state().isOpen).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does nothing if disabled', () => {
|
|
|
|
const onClick = jest.fn()
|
|
|
|
const {dropdown} = setup({disabled: true, onClick})
|
|
|
|
|
|
|
|
dropdown.instance().handleClick()
|
|
|
|
|
|
|
|
expect(dropdown.state().isOpen).toBe(false)
|
|
|
|
expect(onClick).not.toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
})
|
2018-02-28 17:27:19 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|