Skip to content

Commit

Permalink
Merge branch 'main' into 987
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi authored Nov 5, 2024
2 parents 6d7f04e + bbe3c3a commit 4ab2aae
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ const createSnapshotDefault = <T extends object>(
if (refSet.has(value as object)) {
markToTrack(value as object, false) // mark not to track
} else if (proxyStateMap.has(value as object)) {
const [target] = proxyStateMap.get(value as object) as ProxyState
desc.value = createSnapshotDefault(target, version) as Snapshot<T>
const [target, ensureVersion] = proxyStateMap.get(
value as object,
) as ProxyState
desc.value = createSnapshotDefault(target, ensureVersion()) as Snapshot<T>
}
Object.defineProperty(snap, key, desc)
})
Expand Down
32 changes: 32 additions & 0 deletions tests/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,35 @@ it('respects property enumerability (#726)', async () => {
const x = proxy(Object.defineProperty({ a: 1 }, 'b', { value: 2 }))
expect(Object.keys(snapshot(x))).toEqual(Object.keys(x))
})

it('stable snapshot object (#985)', async () => {
const state = proxy({ count: 0, obj: {} })

let effectCount = 0

const TestComponent = () => {
const { count, obj } = useSnapshot(state)
useEffect(() => {
++effectCount
}, [obj])
return (
<>
<div>count: {count}</div>
<button onClick={() => ++state.count}>button</button>
</>
)
}

const { getByText, findByText } = render(<TestComponent />)

await findByText('count: 0')
expect(effectCount).toBe(1)

fireEvent.click(getByText('button'))
await findByText('count: 1')
expect(effectCount).toBe(1)

fireEvent.click(getByText('button'))
await findByText('count: 2')
expect(effectCount).toBe(1)
})
10 changes: 9 additions & 1 deletion tests/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ it('should not change snapshot with modifying the original proxy', async () => {
expect(snap2.obj2.nested.count).toBe(2)
})

describe('snapsoht typings', () => {
it('should return stable nested snapshot object', async () => {
const state = proxy({ count: 0, obj: {} })
const snap1 = snapshot(state)
state.count++
const snap2 = snapshot(state)
expect(snap2.obj).toBe(snap1.obj)
})

describe('snapshot typings', () => {
it('converts object properties to readonly', () => {
expectType<
TypeEqual<
Expand Down

0 comments on commit 4ab2aae

Please sign in to comment.