Skip to content

Commit

Permalink
Add test coverage for custom search function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pattie Reaves committed Oct 19, 2020
1 parent a2fd9e5 commit 9617003
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,9 @@ exports[`<Select/> component <Select/> renders with custom search function 1`] =
onKeyPress={[Function]}
placeholder="Select..."
readOnly={false}
size={9}
size={3}
tabIndex="-1"
value=""
value="Zer"
/>
</div>
<div
Expand Down
21 changes: 19 additions & 2 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import TestRenderer from 'react-test-renderer';
import { LIB_NAME } from '../src/constants';

import Select from '../src/index';

Expand Down Expand Up @@ -60,9 +61,25 @@ describe('<Select/> component', () => {
});

it('<Select/> renders with custom search function', () => {
const tree = selectWithProps(<Select {...props({ searchFn: () => {} })} />).toJSON();
const options = [
{ id: 0, name: 'Zero' },
{ id: 1, name: 'One' },
{ id: 2, name: 'Two' },
];

expect(tree).toMatchSnapshot();
const searchFn = ({ props, state }) => {
return props.options.filter(({ name }) => new RegExp(state.search).test(name) );
};

const component = selectWithProps(<Select {...props({ searchFn, options })} />);

const input = component.root.find(element => element.props.className === 'react-dropdown-select-input');

TestRenderer.act(() => input.props.onChange({ target: { value: 'Zer' } }));

expect(component.toTree().instance.state.search).toBe('Zer');
expect(component.toTree().instance.state.searchResults).toStrictEqual([{ id: 0, name: 'Zero'}])
expect(component.toJSON()).toMatchSnapshot();
});

it('<Select/> is disabled', () => {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ export class Select extends Component {
});

handleKeyDown = (event) => {
console.log({ event });
const args = {
event,
state: this.state,
Expand Down

0 comments on commit 9617003

Please sign in to comment.