Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add docs and unit tests for useQueryParams hook
- Loading branch information
1 parent
fc48cc2
commit a0d28b0
Showing
4 changed files
with
110 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import useQueryParams from '../src/useQueryParams'; | ||
|
||
describe('useQueryParams', () => { | ||
it('sets query params correctly', () => { | ||
const newParams = { page: '1', order: 'ASC' }; | ||
const { result } = renderHook(() => useQueryParams()); | ||
|
||
act(() => { | ||
result.current.setParams(newParams); | ||
}); | ||
const params = result.current.getParams(); | ||
expect(params).toStrictEqual(newParams); | ||
}); | ||
|
||
it('clears query params', () => { | ||
const { result } = renderHook(() => useQueryParams()); | ||
|
||
act(() => { | ||
result.current.setParams(); | ||
}); | ||
const params = result.current.getParams(); | ||
expect(params).toStrictEqual({}); | ||
}); | ||
|
||
it('read query params correctly', () => { | ||
delete window.location; | ||
window.location = new URL('https://test.com/testing?param1=test&testing=true'); | ||
|
||
const { result } = renderHook(() => useQueryParams()); | ||
let params; | ||
act(() => { | ||
params = result.current.getParams(); | ||
}); | ||
expect(params).toStrictEqual({ param1: 'test', testing: 'true' }); | ||
}); | ||
}); |
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,39 @@ | ||
# 📍 `useQueryParams` | ||
|
||
Read and manipulate window.location | ||
|
||
## Usage | ||
|
||
```js | ||
import { useQueryParams } from 'react-recipes'; | ||
|
||
function App() { | ||
const { getParams, setParams } = useQueryParams(); | ||
|
||
const params = getParams(); | ||
|
||
return ( | ||
<div> | ||
<button | ||
onClick={() => { | ||
setParams({ page: 1, order: 'ASC' }); | ||
}} | ||
> | ||
Set Params | ||
</button> | ||
<button | ||
onClick={() => { | ||
setParams({}); | ||
}} | ||
> | ||
Clear params | ||
</button> | ||
{Object.entries(params).map(([paramKey, paramValue]) => ( | ||
<p> | ||
{paramKey}: {paramValue} | ||
</p> | ||
))} | ||
</div> | ||
); | ||
} | ||
``` |
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