Skip to content
This repository has been archived by the owner on Dec 23, 2022. It is now read-only.

Commit

Permalink
Add tests for the focus and keyboard handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
leMaik committed Dec 3, 2017
1 parent 15837fb commit df48516
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 5 deletions.
3 changes: 0 additions & 3 deletions src/ChipInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ class ChipInput extends React.Component {
}

handleInputFocus = (event) => {
if (this.props.disabled) {
return
}
this.setState({ isFocused: true })
if (this.props.onFocus) {
this.props.onFocus(event)
Expand Down
104 changes: 102 additions & 2 deletions src/ChipInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('uncontrolled mode', () => {
)

tree.find('input').simulate('keyDown', { keyCode: 8 }) // backspace (to focus the chip)
tree.find('input').simulate('keyDown', { keyCode: 46 }) // backspace (to remove the chip)
tree.find('input').simulate('keyDown', { keyCode: 8 }) // backspace (to remove the chip)
expect(handleChange).toBeCalledWith(['foo'])
})

Expand All @@ -60,7 +60,7 @@ describe('uncontrolled mode', () => {
)

tree.find('input').simulate('keyDown', { keyCode: 8 }) // backspace (to focus the chip)
tree.find('input').simulate('keyDown', { keyCode: 8 }) // delete (to remove the chip)
tree.find('input').simulate('keyDown', { keyCode: 46 }) // del (to remove the chip)
expect(handleChange).toBeCalledWith(['foo'])
})

Expand All @@ -74,6 +74,106 @@ describe('uncontrolled mode', () => {
})
})

describe('chip focusing', () => {
function getFocusedChip (tree) {
return tree.find('Chip').filterWhere((chip) => chip.getDOMNode().style.backgroundColor !== '')
}

function focusChip (tree, name) {
tree.find('Chip').filterWhere((chip) => chip.text() === name).simulate('click')
}

it('focuses a chip on click', () => {
const tree = mount(
<ChipInput defaultValue={['foo', 'bar']} />
)
tree.find('Chip').at(1).simulate('click')
expect(getFocusedChip(tree).length).toBe(1)
expect(getFocusedChip(tree).text()).toBe('bar')
})

it('focuses the last chip when pressing backspace', () => {
const tree = mount(
<ChipInput defaultValue={['a', 'b', 'c']} />
)
tree.find('input').simulate('keyDown', { keyCode: 8 }) // backspace
expect(getFocusedChip(tree).text()).toBe('c')
})

it('unfocuses the focused chip while adding a new chip', () => {
const tree = mount(
<ChipInput defaultValue={['foo', 'bar']} />
)
focusChip(tree, 'foo')
tree.find('input').simulate('keyDown')
expect(getFocusedChip(tree).length).toBe(0)
})

it('unfocuses the focused chip on blur', () => {
const tree = mount(
<ChipInput defaultValue={['foo', 'bar']} />
)
focusChip(tree, 'foo')
tree.find('input').simulate('blur')
expect(getFocusedChip(tree).length).toBe(0)
})

it('unfocuses the focused chip when switching to disabled state', () => {
const tree = mount(
<ChipInput defaultValue={['foo', 'bar']} />
)
focusChip(tree, 'foo')
tree.setProps({ disabled: true })
expect(getFocusedChip(tree).length).toBe(0)
})

it('moves the focus to the left when pressing the left arrow key', () => {
const tree = mount(
<ChipInput defaultValue={['a', 'b', 'c']} />
)
focusChip(tree, 'b')
tree.find('input').simulate('keyDown', { keyCode: 37 }) // arrow left
expect(getFocusedChip(tree).text()).toBe('a')

// keep the first element focused when pressing left if the first element is already focused
tree.find('input').simulate('keyDown', { keyCode: 37 }) // arrow left
expect(getFocusedChip(tree).text()).toBe('a')
})

it('moves the focus to the right when pressing the right arrow key', () => {
const tree = mount(
<ChipInput defaultValue={['a', 'b', 'c']} />
)
focusChip(tree, 'b')
tree.find('input').simulate('keyDown', { keyCode: 39 }) // arrow right
expect(getFocusedChip(tree).text()).toBe('c')

// onfocus all chips if the right arrow key is pressed when focusing the last chip
tree.find('input').simulate('keyDown', { keyCode: 39 }) // arrow right
expect(getFocusedChip(tree).length).toBe(0)
})

it('focuses the chip to the left when removing a chip by pressing backspace', () => {
const tree = mount(
<ChipInput defaultValue={['a', 'b', 'c']} />
)
focusChip(tree, 'b')

tree.find('input').simulate('keyDown', { keyCode: 8 }) // backspace
expect(getFocusedChip(tree).text()).toBe('a')
})

it('focuses the chip at the previously focused position when removing a chip by pressing delete', () => {
const tree = mount(
<ChipInput defaultValue={['a', 'b', 'c']} />
)
focusChip(tree, 'b')

tree.find('input').simulate('keyDown', { keyCode: 46 }) // delete
expect(getFocusedChip(tree).text()).toBe('c')
})
})

describe('placeholder', () => {
it('displays a placeholder', () => {
const tree = mount(
Expand Down

0 comments on commit df48516

Please sign in to comment.