diff --git a/tests/proxyMap.test.tsx b/tests/proxyMap.test.tsx index 842d3568..9a231298 100644 --- a/tests/proxyMap.test.tsx +++ b/tests/proxyMap.test.tsx @@ -1,7 +1,7 @@ import { StrictMode } from 'react' import { fireEvent, render, waitFor } from '@testing-library/react' import { describe, expect, it, vi } from 'vitest' -import { proxy, useSnapshot } from 'valtio' +import { proxy, snapshot, useSnapshot } from 'valtio' import { proxyMap, proxySet } from 'valtio/utils' const initialValues = [ @@ -319,3 +319,18 @@ describe('proxyMap internal', () => { ).toBe(false) }) }) + +describe('snapshot', () => { + it('should not change snapshot with modifying the original proxy', async () => { + const state = proxyMap([ + ['key1', {}], + ['key2', { nested: { count: 1 } }], + ]) + const snap1 = snapshot(state) + expect(snap1.get('key1')).toBeDefined() + state.get('key2')!.nested!.count++ + const snap2 = snapshot(state) + expect(snap1.get('key2')!.nested!.count).toBe(1) + expect(snap2.get('key2')!.nested!.count).toBe(2) + }) +}) diff --git a/tests/snapshot.test.ts b/tests/snapshot.test.ts index fe0b7f2c..992d3d47 100644 --- a/tests/snapshot.test.ts +++ b/tests/snapshot.test.ts @@ -69,6 +69,16 @@ it('should create a new proxy from a snapshot', async () => { expect(state2.c).toBe(0) }) +it('should not change snapshot with modifying the original proxy', async () => { + const state = proxy({ obj1: {}, obj2: { nested: { count: 1 } } }) + const snap1 = snapshot(state) + expect(snap1.obj1).toBeDefined() + state.obj2.nested.count++ + const snap2 = snapshot(state) + expect(snap1.obj2.nested.count).toBe(1) + expect(snap2.obj2.nested.count).toBe(2) +}) + describe('snapsoht typings', () => { it('converts object properties to readonly', () => { expectType<