From 4b3efb082e4da054ba5d4b834b023e877af38a0e Mon Sep 17 00:00:00 2001 From: Tom Meagher Date: Fri, 27 Sep 2024 13:30:13 -0400 Subject: [PATCH] test: boost coverage --- .../useSyncExternalStoreWithTracked.test.tsx | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/packages/react/src/hooks/useSyncExternalStoreWithTracked.test.tsx b/packages/react/src/hooks/useSyncExternalStoreWithTracked.test.tsx index c806e1298a..e0b1e71a94 100644 --- a/packages/react/src/hooks/useSyncExternalStoreWithTracked.test.tsx +++ b/packages/react/src/hooks/useSyncExternalStoreWithTracked.test.tsx @@ -6,11 +6,11 @@ import { afterEach, expect, test } from 'vitest' import { useSyncExternalStoreWithTracked } from './useSyncExternalStoreWithTracked.js' -function createExternalStore(initialState: State) { +function createExternalStore(initialState: state) { const listeners = new Set<() => void>() let currentState = initialState return { - set(updater: (state: State) => State) { + set(updater: (state: state) => state) { currentState = updater(currentState) ReactDOM.unstable_batchedUpdates(() => { for (const listener of listeners) { @@ -222,3 +222,54 @@ test('store object reference is stable across rerenders', async () => { expect(childRenderCount).toBe(2) expect(renders.length).toBe(3) }) + +test('array', async () => { + const externalStore = createExternalStore(['foo']) + + const renders: any[] = [] + + renderHook(() => { + const array = useExternalStore(externalStore, (state) => { + renders.push(state) + }) + + return array + }) + + act(() => { + externalStore.set((x) => [...x, 'bar']) + }) + + expect(renders).toMatchInlineSnapshot(` + [ + [ + "foo", + ], + [ + "foo", + "bar", + ], + ] + `) + + act(() => { + externalStore.set((x) => [...x, 'baz']) + }) + + expect(renders).toMatchInlineSnapshot(` + [ + [ + "foo", + ], + [ + "foo", + "bar", + ], + [ + "foo", + "bar", + "baz", + ], + ] + `) +})