-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wrote SearchBox tests * removing style tests
- Loading branch information
1 parent
10b70d6
commit a3e382f
Showing
1 changed file
with
105 additions
and
3 deletions.
There are no files selected for viewing
108 changes: 105 additions & 3 deletions
108
packages/react-components/react-search/src/components/SearchBox/SearchBox.test.tsx
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 |
---|---|---|
@@ -1,19 +1,121 @@ | ||
import * as React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, RenderResult, fireEvent, screen } from '@testing-library/react'; | ||
import { SearchBox } from './SearchBox'; | ||
import { isConformant } from '../../testing/isConformant'; | ||
import { resetIdsForTests } from '@fluentui/react-utilities'; | ||
import { searchBoxClassNames } from './useSearchBoxStyles.styles'; | ||
|
||
function getSearchBox(): HTMLInputElement { | ||
return screen.getByRole('searchbox') as HTMLInputElement; | ||
} | ||
|
||
describe('SearchBox', () => { | ||
beforeEach(() => { | ||
resetIdsForTests(); | ||
}); | ||
|
||
let renderedComponent: RenderResult | undefined; | ||
|
||
afterEach(() => { | ||
if (renderedComponent) { | ||
renderedComponent.unmount(); | ||
renderedComponent = undefined; | ||
} | ||
}); | ||
|
||
isConformant({ | ||
Component: SearchBox, | ||
displayName: 'SearchBox', | ||
primarySlot: 'input', | ||
testOptions: { | ||
'has-static-classnames': [ | ||
{ | ||
props: { | ||
contentBefore: 'Test ContentBefore', | ||
contentAfter: 'Test ContentAfter', | ||
dismiss: 'Test Dismiss', | ||
}, | ||
expectedClassNames: { | ||
root: searchBoxClassNames.root, | ||
contentAfter: searchBoxClassNames.contentAfter, | ||
dismiss: searchBoxClassNames.dismiss, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
// TODO add more tests here, and create visual regression tests in /apps/vr-tests | ||
|
||
it('renders a default state', () => { | ||
const result = render(<SearchBox />); | ||
expect(result.container).toMatchSnapshot(); | ||
}); | ||
|
||
// Tests from Input, added here since they were reimplemented for SearchBox | ||
it('respects value', () => { | ||
renderedComponent = render(<SearchBox value="hello" />); | ||
expect(getSearchBox().value).toEqual('hello'); | ||
}); | ||
|
||
it('respects updates to value', () => { | ||
renderedComponent = render(<SearchBox value="hello" />); | ||
expect(getSearchBox().value).toEqual('hello'); | ||
|
||
renderedComponent.rerender(<SearchBox value="world" />); | ||
expect(getSearchBox().value).toEqual('world'); | ||
}); | ||
|
||
it('respects defaultValue', () => { | ||
renderedComponent = render(<SearchBox defaultValue="hello" />); | ||
expect(getSearchBox().value).toEqual('hello'); | ||
}); | ||
|
||
it('ignores updates to defaultValue', () => { | ||
renderedComponent = render(<SearchBox defaultValue="hello" />); | ||
expect(getSearchBox().value).toEqual('hello'); | ||
|
||
renderedComponent.rerender(<SearchBox defaultValue="world" />); | ||
expect(getSearchBox().value).toEqual('hello'); | ||
}); | ||
|
||
it('prefers value over defaultValue', () => { | ||
renderedComponent = render(<SearchBox value="hello" defaultValue="world" />); | ||
expect(getSearchBox().value).toEqual('hello'); | ||
}); | ||
|
||
it('with value, calls onChange but does not update on text entry', () => { | ||
const onChange = jest.fn(); | ||
renderedComponent = render(<SearchBox value="hello" onChange={onChange} />); | ||
const searchBox = getSearchBox(); | ||
fireEvent.change(searchBox, { target: { value: 'world' } }); | ||
expect(onChange).toHaveBeenCalledTimes(1); | ||
expect(onChange.mock.calls[0][1]).toEqual({ value: 'world' }); | ||
expect(searchBox.value).toBe('hello'); | ||
}); | ||
|
||
it('with defaultValue, calls onChange and updates value on text entry', () => { | ||
const onChange = jest.fn(); | ||
renderedComponent = render(<SearchBox defaultValue="hello" onChange={onChange} />); | ||
const searchBox = getSearchBox(); | ||
fireEvent.change(searchBox, { target: { value: 'world' } }); | ||
expect(onChange).toHaveBeenCalledTimes(1); | ||
expect(onChange.mock.calls[0][1]).toEqual({ value: 'world' }); | ||
expect(searchBox.value).toBe('world'); | ||
}); | ||
|
||
it('does not call onChange when value prop updates', () => { | ||
const onChange = jest.fn(); | ||
renderedComponent = render(<SearchBox value="hello" onChange={onChange} />); | ||
renderedComponent.rerender(<SearchBox value="world" onChange={onChange} />); | ||
expect(onChange).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('clears value when dismiss is clicked', () => { | ||
const onClick = jest.fn(); | ||
renderedComponent = render(<SearchBox defaultValue="hello" dismiss={{ onClick }} />); | ||
|
||
userEvent.click(renderedComponent.getByRole('button')); | ||
expect(getSearchBox().value).toBe(''); | ||
expect(onClick).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |