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 calling onAdd and onDelete in controlled mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
leMaik committed Apr 23, 2019
1 parent cc2dc3c commit f30ec9e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 30 deletions.
41 changes: 11 additions & 30 deletions package-lock.json

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

63 changes: 63 additions & 0 deletions src/ChipInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,69 @@ describe('controlled mode', () => {
tree.setProps({ value: ['foo', 'bar'] })
expect(tree.find('input').getDOMNode().value).toBe('bar')
})

it('calls onAdd when adding new chips', () => {
// using object chips to test that too
const handleAdd = jest.fn()
const tree = mount(
<ChipInput
onAdd={handleAdd}
value={[{ text: 'a', value: 1 }, { text: 'b', value: 2 }]}
dataSourceConfig={{ text: 'text', value: 'value' }}
/>
)

tree.find('input').getDOMNode().value = 'foo'
tree.find('input').simulate('keyDown', { keyCode: 13 }) // press enter
expect(handleAdd).toBeCalledWith({ text: 'foo', value: 'foo' })
})

it('calls onDelete when deleting chips with backspace key', () => {
// using object chips to test that too
const handleDelete = jest.fn()
const tree = mount(
<ChipInput
onDelete={handleDelete}
value={[{ text: 'a', value: 1 }, { text: 'b', value: 2 }]}
dataSourceConfig={{ text: 'text', value: 'value' }}
/>
)

tree.find('input').simulate('keyDown', { keyCode: 8 }) // backspace (to focus the chip)
tree.find('input').simulate('keyDown', { keyCode: 8 }) // backspace (to remove the chip)
expect(handleDelete).toBeCalledWith({ text: 'b', value: 2 }, 1) // chip value (object if dataSourceConfig is used) and index
})

it('calls onChange when deleting chips with delete key', () => {
// using object chips to test that too
const handleDelete = jest.fn()
const tree = mount(
<ChipInput
onDelete={handleDelete}
value={[{ text: 'a', value: 1 }, { text: 'b', value: 2 }]}
dataSourceConfig={{ text: 'text', value: 'value' }}
/>
)

tree.find('input').simulate('keyDown', { keyCode: 8 }) // backspace (to focus the chip)
tree.find('input').simulate('keyDown', { keyCode: 46 }) // del (to remove the chip)
expect(handleDelete).toBeCalledWith({ text: 'b', value: 2 }, 1) // chip value (object if dataSourceConfig is used) and index
})

it('calls onChange when deleting chips by clicking on the remove button', () => {
// using object chips to test that too (this is a test for issue #112)
const handleDelete = jest.fn()
const tree = mount(
<ChipInput
onDelete={handleDelete}
value={[{ text: 'a', value: 1 }, { text: 'b', value: 2 }]}
dataSourceConfig={{ text: 'text', value: 'value' }}
/>
)

tree.find('Cancel').first().simulate('click')
expect(handleDelete).toBeCalledWith({ text: 'a', value: 1 }, 0) // chip value (object if dataSourceConfig is used) and index
})
})

describe('chip focusing', () => {
Expand Down

0 comments on commit f30ec9e

Please sign in to comment.