Skip to content

Commit

Permalink
fix(selectItemAtIndex): allow selecting empty string (#242)
Browse files Browse the repository at this point in the history
* fix(selectItemAtIndex): allow selecting empty string

* fix(test): add items before testing clearItems()
  • Loading branch information
tobiasandersen authored and Kent C. Dodds committed Oct 26, 2017
1 parent f1460bf commit 38fc09c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/__tests__/downshift.misc.js
Expand Up @@ -32,6 +32,24 @@ test('selectItemAtIndex does nothing if there is no item at that index', () => {
expect(childSpy).not.toHaveBeenCalled()
})

test('selectItemAtIndex can select item that is an empty string', () => {
const items = ['Chess', '']
const children = ({getItemProps}) => (
<div>
{items.map((item, index) => (
<div key={index} {...getItemProps({item})}>
{item}
</div>
))}
</div>
)
const {selectItemAtIndex, childSpy} = setup({children})
selectItemAtIndex(1)
expect(childSpy).toHaveBeenLastCalledWith(
expect.objectContaining({selectedItem: ''}),
)
})

test('clearSelection with an input node focuses the input node', () => {
const children = ({getInputProps}) => (
<div>
Expand All @@ -57,8 +75,15 @@ test('toggleMenu can take no arguments at all', () => {
})

test('clearItems clears the all items', () => {
const items = ['Chess']
const {wrapper, clearItems} = setup({items})
const item = 'Chess'
const children = ({getItemProps}) => (
<div>
<div key={item} {...getItemProps({item})}>
{item}
</div>
</div>
)
const {wrapper, clearItems} = setup({children})
clearItems()
expect(wrapper.instance().items).toEqual([])
})
Expand Down
2 changes: 1 addition & 1 deletion src/downshift.js
Expand Up @@ -247,7 +247,7 @@ class Downshift extends Component {

selectItemAtIndex = (itemIndex, otherStateToSet, cb) => {
const item = this.items[itemIndex]
if (!item) {
if (item == null) {
return
}
this.selectItem(item, otherStateToSet, cb)
Expand Down

0 comments on commit 38fc09c

Please sign in to comment.