-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebdb0a2
commit 0925117
Showing
3 changed files
with
55 additions
and
49 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,32 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import type { MemoryRouterProps } from 'react-router'; | ||
import { MemoryRouter } from 'react-router'; | ||
import useUrlState from '..'; | ||
import type { Options } from '..'; | ||
|
||
export const setup = ( | ||
initialEntries: MemoryRouterProps['initialEntries'], | ||
initialState: any = {}, | ||
options?: Options, | ||
) => { | ||
const res = {} as any; | ||
|
||
const Component = () => { | ||
const [state, setState] = useUrlState(initialState, options); | ||
Object.assign(res, { state, setState }); | ||
return null; | ||
}; | ||
|
||
render( | ||
<MemoryRouter initialEntries={initialEntries}> | ||
<Component /> | ||
</MemoryRouter>, | ||
); | ||
|
||
return res; | ||
}; | ||
|
||
it('useUrlState should be defined', () => { | ||
expect(useUrlState).toBeDefined(); | ||
}); |
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 { act } from '@testing-library/react-hooks/dom'; | ||
import { setup } from '.'; | ||
|
||
const navigate = jest.fn(); | ||
jest.mock('react-router', () => { | ||
return { | ||
...jest.requireActual('react-router'), | ||
useNavigate: () => navigate, | ||
}; | ||
}); | ||
|
||
describe('React Router V6', () => { | ||
it('useUrlState should be work', () => { | ||
const res = setup(['/index']); | ||
act(() => { | ||
res.setState({ count: 1 }); | ||
}); | ||
|
||
expect(res.state).toMatchObject({ count: '1' }); | ||
expect(navigate).toBeCalledWith({ hash: '', search: 'count=1' }, { replace: false }); | ||
}); | ||
}); |