File size: 1,362 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import { renderHook, act } from '@testing-library/react-hooks';
import { useHash } from '../src/useHash';
(global as any).window = Object.create(window);
let mockHash = '#';
const mockLocation = {};
Object.defineProperty(mockLocation, 'hash', {
get() {
return mockHash;
},
set(newHash) {
mockHash = newHash;
window.dispatchEvent(new HashChangeEvent('hashchange'));
},
});
Object.defineProperty(window, 'location', {
value: mockLocation,
});
beforeEach(() => {
window.location.hash = '#';
});
test('returns current url hash', () => {
window.location.hash = '#abc';
const { result } = renderHook(() => useHash());
const hash = result.current[0];
expect(hash).toBe('#abc');
});
test('returns latest url hash when change the hash with setHash', () => {
const { result } = renderHook(() => useHash());
const hash = result.current[0];
const setHash = result.current[1];
expect(hash).toBe('#');
act(() => {
setHash('#abc');
});
const hash2 = result.current[0];
expect(hash2).toBe('#abc');
});
it('returns latest url hash when change the hash with "hashchange" event', () => {
const { result } = renderHook(() => useHash());
const hash = result.current[0];
expect(hash).toBe('#');
act(() => {
window.location.hash = '#abc';
});
const hash2 = result.current[0];
expect(hash2).toBe('#abc');
});
|