File size: 3,232 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import { act, renderHook } from '@testing-library/react-hooks';
import createGlobalState from '../src/factory/createGlobalState';
describe('useGlobalState', () => {
it('should be defined', () => {
expect(createGlobalState).toBeDefined();
});
it('both components should be updated', () => {
const useGlobalValue = createGlobalState(0);
const { result: result1 } = renderHook(() => useGlobalValue());
const { result: result2 } = renderHook(() => useGlobalValue());
expect(result1.current[0]).toBe(0);
expect(result2.current[0]).toBe(0);
act(() => {
result1.current[1](1);
});
expect(result1.current[0]).toBe(1);
expect(result2.current[0]).toBe(1);
});
it('allows setting state with function and previous value', () => {
const useGlobalValue = createGlobalState(0);
const { result: result1 } = renderHook(() => useGlobalValue());
const { result: result2 } = renderHook(() => useGlobalValue());
expect(result1.current[0]).toBe(0);
expect(result2.current[0]).toBe(0);
act(() => {
result1.current[1]((value) => value + 1);
});
expect(result1.current[0]).toBe(1);
expect(result2.current[0]).toBe(1);
});
it('allows setting state with function and no previous value', () => {
const useGlobalValue = createGlobalState(0);
const { result: result1 } = renderHook(() => useGlobalValue());
const { result: result2 } = renderHook(() => useGlobalValue());
expect(result1.current[0]).toBe(0);
expect(result2.current[0]).toBe(0);
act(() => {
result1.current[1](() => 1);
});
expect(result1.current[0]).toBe(1);
expect(result2.current[0]).toBe(1);
});
it('initializes and updates with undefined', () => {
const useGlobalValue = createGlobalState<number>();
const { result: result1 } = renderHook(() => useGlobalValue());
const { result: result2 } = renderHook(() => useGlobalValue());
expect(result1.current[0]).toBe(undefined);
expect(result2.current[0]).toBe(undefined);
act(() => {
// this is the only case where types fail, it should guard the number
result1.current[1]((value) => value);
});
expect(result1.current[0]).toBe(undefined);
expect(result2.current[0]).toBe(undefined);
});
it('initializes with undefined and updates with different type', () => {
const useGlobalValue = createGlobalState();
const { result: result1 } = renderHook(() => useGlobalValue());
const { result: result2 } = renderHook(() => useGlobalValue());
expect(result1.current[0]).toBe(undefined);
expect(result2.current[0]).toBe(undefined);
act(() => {
// @ts-expect-error this case it checks correctly, hence the comment
result1.current[1]((value) => 1);
});
});
it('initializes with function', () => {
const useGlobalValue = createGlobalState(() => 0);
const { result: result1 } = renderHook(() => useGlobalValue());
const { result: result2 } = renderHook(() => useGlobalValue());
expect(result1.current[0]).toBe(0);
expect(result2.current[0]).toBe(0);
act(() => {
result1.current[1](1);
});
expect(result1.current[0]).toBe(1);
expect(result2.current[0]).toBe(1);
});
});
|