react-use/tests/useSearchParam.test.ts
Anton Zinovyev 8de2a3ee13 chore: move tests to top level /tests folder
chore: move all the tests to the separate directory outside of sources;

chore: remove jest.config.js (config moved to the package.json);

test: unused import in test;

test: 💍 fix tests add back x and y to useMeasure

chore: 🤖 add linter to /tests folder

ci: 🎡 limit Jest worker count for CircleCI
2019-11-08 16:55:34 -05:00

52 lines
1.2 KiB
TypeScript

import { act, renderHook } from '@testing-library/react-hooks';
import useSearchParam from '../src/useSearchParam';
(global as any).window = Object.create(window);
const location = {
search: 'foo=bar&baz=quux',
};
Object.defineProperty(window, 'location', {
value: location,
});
it('returns current location.search value', () => {
location.search = 'foo=bar&baz=quux';
const { result } = renderHook(() => useSearchParam('foo'));
expect(result.current).toBe('bar');
});
it('returns null if search param not found', () => {
location.search = 'foo=bar&baz=quux';
const { result } = renderHook(() => useSearchParam('foo2'));
expect(result.current).toBe(null);
});
it('tracks the latest search param value', () => {
location.search = 'foo=bar&baz=quux';
let callback;
const window$addEventListener = window.addEventListener;
window.addEventListener = (event, cb) => {
if (event === 'pushstate') {
callback = cb;
}
};
const { result } = renderHook(() => useSearchParam('baz'));
expect(result.current).toBe('quux');
act(() => {
location.search = 'foo=1&baz=2';
callback();
});
expect(result.current).toBe('2');
window.addEventListener = window$addEventListener;
});