Download react-use/tests/createGlobalState.test.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/tests/createGlobalState.test.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/tests/createGlobalState.test.ts
-
curl -L -o createGlobalState.test.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/tests/createGlobalState.test.ts
3.23 kB
| 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); | |
| }); | |
| }); | |