-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reflect search in URL [#16287]
- Loading branch information
Showing
2 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
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,71 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { ThemeProvider, themes, convert } from '@storybook/theming'; | ||
|
||
import { FILTER_KEY } from './Search'; | ||
import { Simple as Search } from './Search.stories'; | ||
|
||
const TEST_URL = 'http://localhost'; | ||
const FILTER_VALUE = 'filter'; | ||
const PLACEHOLDER = 'Find components'; | ||
const DEFAULT_SEARCH = '?path=story'; | ||
|
||
beforeEach(() => { | ||
const { history, location } = global.window; | ||
delete global.window.location; | ||
global.window.location = { ...location }; | ||
delete global.window.history; | ||
global.window.history = { ...history }; | ||
global.window.history.replaceState = (state, title, url) => { | ||
global.window.location.href = url; | ||
global.window.location.search = url.indexOf('?') !== -1 ? url.slice(url.indexOf('?')) : ''; | ||
}; | ||
}); | ||
|
||
const setLocation = (url = TEST_URL, search = DEFAULT_SEARCH) => { | ||
global.window.location.href = `${url}${search}`; | ||
global.window.location.search = search; | ||
}; | ||
|
||
const renderSearch = () => | ||
render( | ||
<ThemeProvider theme={convert(themes.light)}> | ||
<Search /> | ||
</ThemeProvider> | ||
); | ||
|
||
describe('Search - reflect search in URL', () => { | ||
it('renders OK', async () => { | ||
setLocation(); | ||
renderSearch(TEST_URL, DEFAULT_SEARCH); | ||
const INPUT = await screen.getByPlaceholderText(PLACEHOLDER); | ||
expect(INPUT.value).toBe(''); | ||
}); | ||
it('prefills input with search params', async () => { | ||
setLocation(TEST_URL, `${DEFAULT_SEARCH}&${FILTER_KEY}=${FILTER_VALUE}`); | ||
renderSearch(); | ||
const INPUT = await screen.getByPlaceholderText(PLACEHOLDER); | ||
expect(INPUT.value).toBe(FILTER_VALUE); | ||
}); | ||
it('updates location on input update with current query', async () => { | ||
setLocation(); | ||
renderSearch(); | ||
const INPUT = await screen.getByPlaceholderText(PLACEHOLDER); | ||
userEvent.clear(INPUT); | ||
userEvent.type(INPUT, FILTER_VALUE); | ||
expect(global.window.location.href).toBe( | ||
`${TEST_URL}${DEFAULT_SEARCH}&${FILTER_KEY}=${FILTER_VALUE}` | ||
); | ||
expect(global.window.location.search).toBe(`${DEFAULT_SEARCH}&${FILTER_KEY}=${FILTER_VALUE}`); | ||
}); | ||
it('updates location on input update without current query', async () => { | ||
setLocation(TEST_URL, ''); | ||
renderSearch(); | ||
const INPUT = await screen.getByPlaceholderText(PLACEHOLDER); | ||
userEvent.clear(INPUT); | ||
userEvent.type(INPUT, FILTER_VALUE); | ||
expect(global.window.location.href).toBe(`${TEST_URL}?${FILTER_KEY}=${FILTER_VALUE}`); | ||
expect(global.window.location.search).toBe(`?${FILTER_KEY}=${FILTER_VALUE}`); | ||
}); | ||
}); |
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