Skip to content

Commit

Permalink
test: boost coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Sep 27, 2024
1 parent c6b8efd commit 4b3efb0
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions packages/react/src/hooks/useSyncExternalStoreWithTracked.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { afterEach, expect, test } from 'vitest'

import { useSyncExternalStoreWithTracked } from './useSyncExternalStoreWithTracked.js'

function createExternalStore<State>(initialState: State) {
function createExternalStore<state>(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) {
Expand Down Expand Up @@ -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",
],
]
`)
})

0 comments on commit 4b3efb0

Please sign in to comment.