Skip to content

Commit

Permalink
test(useUrlState): refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
miracles1919 committed Aug 9, 2022
1 parent ebdb0a2 commit 0925117
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 49 deletions.
50 changes: 1 addition & 49 deletions packages/use-url-state/src/__tests__/browser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
import React from 'react';
import { render } from '@testing-library/react';
import { act } from '@testing-library/react-hooks/dom';
import type { MemoryRouterProps } from 'react-router';
import { MemoryRouter } from 'react-router';
import * as rc from 'react-router';
import useUrlState from '..';
import type { Options } from '..';

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;
};
import { setup } from '.';

describe('useUrlState', () => {
it('should be defined', () => {
expect(useUrlState).toBeDefined();
});

it('state should be url search params', () => {
const res = setup([
{
Expand Down Expand Up @@ -89,20 +57,4 @@ describe('useUrlState', () => {
});
expect(res.state).toMatchObject({ foo: ['4', '5', '6'] });
});

it('react router v5 should be work', () => {
const push = jest.fn();

Object.defineProperty(rc, 'useHistory', {
value: () => ({ push }),
});

const res = setup(['/index']);
act(() => {
res.setState({ count: 1 });
});

expect(res.state).toMatchObject({ count: '1' });
expect(push).toBeCalledWith({ hash: '', search: 'count=1' });
});
});
32 changes: 32 additions & 0 deletions packages/use-url-state/src/__tests__/index.tsx
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();
});
22 changes: 22 additions & 0 deletions packages/use-url-state/src/__tests__/router.test.tsx
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 });
});
});

0 comments on commit 0925117

Please sign in to comment.