diff --git a/tests/snapshot.test.ts b/tests/snapshot.test.ts index f6ea0775..a75ceaee 100644 --- a/tests/snapshot.test.ts +++ b/tests/snapshot.test.ts @@ -1,7 +1,13 @@ import { createProxy, getUntracked } from 'proxy-compare' import { TypeEqual, expectType } from 'ts-expect' + import { describe, expect, it } from 'vitest' -import { INTERNAL_Snapshot as Snapshot, proxy, snapshot } from 'valtio' +import { + INTERNAL_Snapshot as Snapshot, + proxy, + snapshot, + subscribe, +} from 'valtio' const sleep = (ms: number) => new Promise((resolve) => { @@ -69,6 +75,41 @@ it('should create a new proxy from a snapshot', async () => { expect(state2.c).toBe(0) }) +it('should return updated property snapshot when property is updated after removed from Array and added to array again', async () => { + /** + * error occurs when + * [1]. Remove target item should be in the array + * [2]. Remove target item should be nested object + * [3]. The array should be subscribed + * [4]. The array should pass to snapshot() before the target item removed + */ + + const array = proxy([ + //[1] + { + //[2] + nested: { + value: 'before', + }, + }, + ]) + + //[4] + subscribe(array, () => {}) + //[3] + expect(snapshot(array)[0]?.nested.value).toEqual('before') + + const property = array[0] + array.splice(0, 1) + + if (property) { + property.nested.value = 'after' + array.push(property) + } + + expect(snapshot(array)[0]?.nested.value).toEqual('after') +}) + describe('snapsoht typings', () => { it('converts object properties to readonly', () => { expectType<