From 426975cdd5228794c6b1b8bae1f392ddd4cf3cf0 Mon Sep 17 00:00:00 2001 From: Dor Shtaif Date: Wed, 8 Jan 2025 00:56:24 +0200 Subject: [PATCH] test touchups and one added case --- spec/tests/useAsyncIterMulti.spec.ts | 29 +++++++++++++++++++++++++--- spec/utils/asyncIterOf.ts | 4 +++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/spec/tests/useAsyncIterMulti.spec.ts b/spec/tests/useAsyncIterMulti.spec.ts index d5b5c49..7c424ba 100644 --- a/spec/tests/useAsyncIterMulti.spec.ts +++ b/spec/tests/useAsyncIterMulti.spec.ts @@ -163,6 +163,29 @@ describe('`useAsyncIterMulti` hook', () => { ]); }); + it( + gray("When given multiple iterables, some empty, reflects each's states correctly"), + async () => { + let timesRerendered = 0; + + const iter = asyncIterOf('a'); + const emptyIter = asyncIterOf(); + + const renderedHook = await act(() => + renderHook(() => { + timesRerendered++; + return useAsyncIterMulti([iter, emptyIter]); + }) + ); + + expect(timesRerendered).toStrictEqual(2); + expect(renderedHook.result.current).toStrictEqual([ + { value: 'a', pendingFirst: false, done: true, error: undefined }, + { value: undefined, pendingFirst: false, done: true, error: undefined }, + ]); + } + ); + it( gray( "When given multiple iterables with corresponding initial values, reflects each's states correctly, starting with its corresponding initial value" @@ -204,7 +227,7 @@ describe('`useAsyncIterMulti` hook', () => { it( gray( - "When given multiple iterables with corresponding initial values for some, reflects each's states correctly, possibly starting with a corresponding initial value if present" + "When given multiple iterables with corresponding initial values for only some, reflects each's states correctly, possibly starting with a corresponding initial value if present" ), async () => { const channels = [ @@ -421,7 +444,7 @@ describe('`useAsyncIterMulti` hook', () => { } ); - it(gray('When unmounted will close all the last held active iterators'), async () => { + it(gray('When unmounted will close all active iterators it has been holding'), async () => { const channel1 = new IteratorChannelTestHelper<'a' | 'b' | 'c'>(); const channel2 = new IteratorChannelTestHelper<'a' | 'b' | 'c'>(); @@ -459,7 +482,7 @@ describe('`useAsyncIterMulti` hook', () => { it( gray( - 'When adding / removing / swapping positions of iterables, their ongoing states are maintained every step regardless of position and are closed only when they disappear altogether from passed array' + 'When adding / removing / swapping positions of iterables, their ongoing states are maintained at every step regardless of position and get closed only when they disappear altogether from passed array' ), async () => { const values: (string | IteratorChannelTestHelper)[] = []; diff --git a/spec/utils/asyncIterOf.ts b/spec/utils/asyncIterOf.ts index c1a733f..b929c24 100644 --- a/spec/utils/asyncIterOf.ts +++ b/spec/utils/asyncIterOf.ts @@ -1,6 +1,8 @@ export { asyncIterOf }; -function asyncIterOf(...values: T[]) { +function asyncIterOf(...values: []): AsyncIterable; +function asyncIterOf(...values: T[]): AsyncIterable; +function asyncIterOf(...values: T[]): AsyncIterable { return { async *[Symbol.asyncIterator]() { yield* values;