|
| 1 | +import { it, describe, expect, afterEach } from 'vitest'; |
| 2 | +import { gray } from 'colorette'; |
| 3 | +import { range } from 'lodash-es'; |
| 4 | +import { renderHook, cleanup as cleanupMountedReactTrees, act } from '@testing-library/react'; |
| 5 | +import { useAsyncIterState } from '../../src/index.js'; |
| 6 | +import { asyncIterToArray } from '../utils/asyncIterToArray.js'; |
| 7 | +import { asyncIterTake } from '../utils/asyncIterTake.js'; |
| 8 | +import { pipe } from '../utils/pipe.js'; |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + cleanupMountedReactTrees(); |
| 12 | +}); |
| 13 | + |
| 14 | +describe('`useAsyncIterState` hook', () => { |
| 15 | + it(gray('The returned iterable can be async-iterated upon successfully'), async () => { |
| 16 | + const [values, setValue] = renderHook(() => useAsyncIterState<string>()).result.current; |
| 17 | + |
| 18 | + const valuesToSet = ['a', 'b', 'c']; |
| 19 | + |
| 20 | + const collectPromise = pipe(values, asyncIterTake(valuesToSet.length), asyncIterToArray); |
| 21 | + |
| 22 | + for (const value of valuesToSet) { |
| 23 | + await act(() => setValue(value)); |
| 24 | + } |
| 25 | + |
| 26 | + expect(await collectPromise).toStrictEqual(['a', 'b', 'c']); |
| 27 | + }); |
| 28 | + |
| 29 | + it( |
| 30 | + gray( |
| 31 | + 'When hook is unmounted, all outstanding yieldings of the returned iterable resolve to "done"' |
| 32 | + ), |
| 33 | + async () => { |
| 34 | + const renderedHook = renderHook(() => useAsyncIterState<string>()); |
| 35 | + const [values] = renderedHook.result.current; |
| 36 | + |
| 37 | + const [collectPromise1, collectPromise2] = range(2).map(() => asyncIterToArray(values)); |
| 38 | + |
| 39 | + renderedHook.unmount(); |
| 40 | + |
| 41 | + const collections = await Promise.all([collectPromise1, collectPromise2]); |
| 42 | + expect(collections).toStrictEqual([[], []]); |
| 43 | + } |
| 44 | + ); |
| 45 | + |
| 46 | + it( |
| 47 | + gray( |
| 48 | + 'After setting some values followed by unmounting the hook, the pre-unmounting values go through while further values pulled from the returned iterable are always "done"' |
| 49 | + ), |
| 50 | + async () => { |
| 51 | + const renderedHook = renderHook(() => useAsyncIterState<string>()); |
| 52 | + const [values, setValue] = renderedHook.result.current; |
| 53 | + |
| 54 | + const [collectPromise1, collectPromise2] = range(2).map(() => asyncIterToArray(values)); |
| 55 | + |
| 56 | + await act(() => setValue('a')); |
| 57 | + |
| 58 | + renderedHook.unmount(); |
| 59 | + |
| 60 | + const collections = await Promise.all([collectPromise1, collectPromise2]); |
| 61 | + expect(collections).toStrictEqual([['a'], ['a']]); |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + it( |
| 66 | + gray( |
| 67 | + 'After the hook is unmounted, any further values pulled from the returned iterable are always "done"' |
| 68 | + ), |
| 69 | + async () => { |
| 70 | + const renderedHook = renderHook(() => useAsyncIterState<string>()); |
| 71 | + const [values] = renderedHook.result.current; |
| 72 | + |
| 73 | + renderedHook.unmount(); |
| 74 | + |
| 75 | + const collections = await Promise.all(range(2).map(() => asyncIterToArray(values))); |
| 76 | + expect(collections).toStrictEqual([[], []]); |
| 77 | + } |
| 78 | + ); |
| 79 | + |
| 80 | + it( |
| 81 | + gray( |
| 82 | + "The returned iterable's values are each shared between all its parallel consumers so that each receives all the values from the start of consumption and onwards" |
| 83 | + ), |
| 84 | + async () => { |
| 85 | + const [values, setValue] = renderHook(() => useAsyncIterState<string>()).result.current; |
| 86 | + |
| 87 | + const consumeStacks: string[][] = []; |
| 88 | + |
| 89 | + for (const [i, value] of ['a', 'b', 'c'].entries()) { |
| 90 | + consumeStacks[i] = []; |
| 91 | + (async () => { |
| 92 | + for await (const v of values) consumeStacks[i].push(v); |
| 93 | + })(); |
| 94 | + await act(() => setValue(value)); |
| 95 | + } |
| 96 | + |
| 97 | + expect(consumeStacks).toStrictEqual([['a', 'b', 'c'], ['b', 'c'], ['c']]); |
| 98 | + } |
| 99 | + ); |
| 100 | +}); |
0 commit comments