Skip to content

Commit

Permalink
chore(test): add some cases with snapshot (#966)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi authored Oct 14, 2024
1 parent 0700ed4 commit 98ea29b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 16 additions & 1 deletion tests/proxyMap.test.tsx
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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)
})
})
10 changes: 10 additions & 0 deletions tests/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down

0 comments on commit 98ea29b

Please sign in to comment.