-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce useParams and useSearchParams hooks
- Loading branch information
Showing
12 changed files
with
1,237 additions
and
748 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useContext } from 'react' | ||
import RouterContext from '../RouterContext' | ||
import useParams from '../useParams' | ||
|
||
jest.mock('react') | ||
useContext.mockImplementation(() => ({ | ||
get: jest.fn().mockReturnValue({ params: { foo: 'bar', baz: 'qux' } }) | ||
})) | ||
|
||
describe('useParams', () => { | ||
it('exports an object with expected route params from RouterContext', () => { | ||
const params = useParams() | ||
|
||
expect(useContext).toHaveBeenCalledTimes(1) | ||
expect(useContext).toHaveBeenCalledWith(RouterContext) | ||
expect(params).toEqual(expect.any(Object)) | ||
expect(params).toEqual({ | ||
foo: 'bar', | ||
baz: 'qux' | ||
}) | ||
}) | ||
}) |
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,108 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import '@testing-library/jest-dom/extend-expect' | ||
import React, { useContext } from 'react' | ||
import { fireEvent, render, screen, waitFor } from '@testing-library/react' | ||
|
||
import RouterContext from '../RouterContext' | ||
import useSearchParams from '../useSearchParams' | ||
|
||
jest.mock('react', () => ({ | ||
...jest.requireActual('react'), | ||
useContext: jest.fn() | ||
})) | ||
const mockHistoryPush = jest.fn() | ||
useContext.mockImplementation(() => ({ | ||
history: { | ||
location: { | ||
pathname: '/home', | ||
search: '?foo=bar&baz=qux', | ||
hash: '', | ||
state: null, | ||
key: 'siezotjq' | ||
}, | ||
push: mockHistoryPush | ||
} | ||
})) | ||
|
||
const ExampleComponent = () => { | ||
const [searchParams, setSearchParams] = useSearchParams() | ||
|
||
return ( | ||
<> | ||
<div>searchParams value: "{JSON.stringify(searchParams)}"</div> | ||
<button onClick={() => setSearchParams({ quux: 'corge' })}> | ||
replaceSearchParams | ||
</button> | ||
<button | ||
onClick={() => | ||
setSearchParams(currentParams => ({ | ||
...currentParams, | ||
quux: 'corge' | ||
})) | ||
} | ||
> | ||
mergeSearchParams | ||
</button> | ||
</> | ||
) | ||
} | ||
|
||
describe('useSearchParams', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('returns current searchParams correctly', () => { | ||
render(<ExampleComponent />) | ||
|
||
expect(screen.queryByText(/searchParams value:/)).toHaveTextContent( | ||
'searchParams value: "{"foo":"bar","baz":"qux"}"' | ||
) | ||
|
||
expect(mockHistoryPush).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('overrides searchParams correctly', () => { | ||
render(<ExampleComponent />) | ||
|
||
fireEvent.click(screen.getByRole('button', { name: 'replaceSearchParams' })) | ||
|
||
waitFor(() => | ||
expect(screen.queryByText(/searchParams value:/)).toHaveTextContent( | ||
'searchParams value: "{"quux":"corge"}"' | ||
) | ||
) | ||
|
||
expect(mockHistoryPush).toHaveBeenCalledTimes(1) | ||
expect(mockHistoryPush).toHaveBeenCalledWith( | ||
{ | ||
pathname: '/home', | ||
search: '?quux=corge' | ||
}, | ||
null | ||
) | ||
}) | ||
|
||
it('merges searchParams correctly', () => { | ||
render(<ExampleComponent />) | ||
|
||
fireEvent.click(screen.getByRole('button', { name: 'mergeSearchParams' })) | ||
|
||
waitFor(() => | ||
expect(screen.queryByText(/searchParams value:/)).toHaveTextContent( | ||
'searchParams value: "{"foo":"bar","baz":"qux","quux":"corge"}"' | ||
) | ||
) | ||
|
||
expect(mockHistoryPush).toHaveBeenCalledTimes(1) | ||
expect(mockHistoryPush).toHaveBeenCalledWith( | ||
{ | ||
pathname: '/home', | ||
search: '?baz=qux&foo=bar&quux=corge' | ||
}, | ||
null | ||
) | ||
}) | ||
}) |
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
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,11 @@ | ||
import { useContext } from 'react' | ||
import RouterContext from './RouterContext' | ||
|
||
const useParams = () => { | ||
const { get } = useContext(RouterContext) | ||
const routeEntry = get() | ||
|
||
return routeEntry.params | ||
} | ||
|
||
export default useParams |
Oops, something went wrong.