This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The <SearchBar /> component debounces searches. Normally that would be tested using Jest's fake timers, however due to this issue jestjs/jest#3465 it's using real timers and a timeout of 500ms. A placeholder prop to be passed onto the <InputText /> was also added.
- Loading branch information
Showing
7 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/components/search_bar/__tests__/__snapshots__/search_bar.spec.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`search_bar.tsx snapshot and instance 1`] = ` | ||
<SearchBar | ||
emitDelay={0} | ||
onChange={[MockFunction]} | ||
placeholder="For example…" | ||
> | ||
<div | ||
className="search-bar" | ||
> | ||
<span | ||
className="search-bar__icon" | ||
> | ||
<MagnifierIconSmall> | ||
<svg | ||
aria-hidden="true" | ||
fill="currentColor" | ||
focusable="false" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
width="24px" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
d="M15,1.99999996 C11.1,1.99999996 7.99999996,5.09999997 7.99999996,8.99999996 C7.99999996,10.6 8.49999996,12 9.39999999,13.2 L1.99999996,20.6 L3.39999996,22 L10.8,14.6 C12,15.5 13.4,16 15,16 C18.9,16 22,12.9 22,8.99999996 C22,5.09999997 18.9,1.99999996 15,1.99999996 Z M15,14 C12.2,14 9.99999999,11.8 9.99999999,8.99999997 C9.99999999,6.19999998 12.2,3.99999997 15,3.99999997 C17.8,3.99999997 20,6.19999998 20,8.99999997 C20,11.8 17.8,14 15,14 Z" | ||
fill="currentColor" | ||
/> | ||
</svg> | ||
</MagnifierIconSmall> | ||
</span> | ||
<ForwardRef | ||
className="search-bar__input" | ||
hasError={false} | ||
onChange={[Function]} | ||
placeholder="For example…" | ||
type="text" | ||
value="" | ||
> | ||
<input | ||
className="input-text search-bar__input" | ||
onChange={[Function]} | ||
placeholder="For example…" | ||
type="text" | ||
value="" | ||
/> | ||
</ForwardRef> | ||
</div> | ||
</SearchBar> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react'; | ||
import { mount, ReactWrapper } from 'enzyme'; | ||
import { SearchBar } from '..'; | ||
|
||
// Use real timers for this one because https://github.com/facebook/jest/issues/3465 | ||
|
||
describe('search_bar.tsx', () => { | ||
let wrapper: ReactWrapper; | ||
let emitDelay: number; | ||
const query = 'hello world'; | ||
const placeholder = 'For example…'; | ||
const onChange: jest.Mock<any, any> = jest.fn(); | ||
|
||
afterEach(() => { | ||
onChange.mockReset(); | ||
wrapper.unmount(); | ||
}); | ||
|
||
test('snapshot and instance', () => { | ||
wrapper = mount(<SearchBar onChange={onChange} placeholder={placeholder} />); | ||
expect(wrapper.instance()).toBeInstanceOf(SearchBar); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
test('with "query" prop', (done) => { | ||
wrapper = mount( | ||
<SearchBar | ||
onChange={onChange} | ||
placeholder={placeholder} | ||
query={query} | ||
/>, | ||
); | ||
expect(onChange).not.toHaveBeenCalled(); | ||
setTimeout(() => { | ||
expect(onChange).toHaveBeenCalledTimes(1); | ||
expect(onChange).toHaveBeenCalledWith(query); | ||
done(); | ||
}, 0); | ||
}); | ||
|
||
test('with "emitDelay" prop', (done) => { | ||
emitDelay = 500; | ||
wrapper = mount( | ||
<SearchBar | ||
onChange={onChange} | ||
placeholder={placeholder} | ||
emitDelay={emitDelay} | ||
query={query} | ||
/>, | ||
); | ||
expect(onChange).not.toHaveBeenCalled(); | ||
setTimeout(() => { | ||
expect(onChange).toHaveBeenCalledTimes(1); | ||
expect(onChange).toHaveBeenCalledWith(query); | ||
done(); | ||
}, emitDelay); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { SearchBar } from './search_bar'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters