Skip to content

Commit

Permalink
added new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
overthemike committed Nov 5, 2024
1 parent 3773d8e commit 2dfdd19
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/vanilla/utils/proxyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
const map = getMapForThis(this)
const index = map.get(key)
if (index === undefined) {
this.epoch // touch property for tracking
return undefined
}
return this.data[index]
Expand Down
55 changes: 47 additions & 8 deletions tests/proxyMap.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { StrictMode } from 'react'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { describe, expect, it, vi } from 'vitest'
import { proxy, snapshot, useSnapshot } from 'valtio'
import { proxyMap, proxySet } from 'valtio/utils'
import { proxy, snapshot, useSnapshot } from '../src'

Check failure on line 4 in tests/proxyMap.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.8.4)

Module '"../src"' has no exported member 'proxy'.

Check failure on line 4 in tests/proxyMap.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.8.4)

Module '"../src"' has no exported member 'snapshot'.

Check failure on line 4 in tests/proxyMap.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.5.5)

Module '"../src"' has no exported member 'proxy'.

Check failure on line 4 in tests/proxyMap.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.5.5)

Module '"../src"' has no exported member 'snapshot'.

Check failure on line 4 in tests/proxyMap.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.7.4)

Module '"../src"' has no exported member 'proxy'.

Check failure on line 4 in tests/proxyMap.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.7.4)

Module '"../src"' has no exported member 'snapshot'.

Check failure on line 4 in tests/proxyMap.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.6.4)

Module '"../src"' has no exported member 'proxy'.

Check failure on line 4 in tests/proxyMap.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.6.4)

Module '"../src"' has no exported member 'snapshot'.

Check failure on line 4 in tests/proxyMap.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.9.5)

Module '"../src"' has no exported member 'proxy'.

Check failure on line 4 in tests/proxyMap.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.9.5)

Module '"../src"' has no exported member 'snapshot'.
import { proxyMap, proxySet } from '../src/vanilla/utils'

const initialValues = [
{
Expand Down Expand Up @@ -525,8 +525,11 @@ describe('ui updates - useSnapshot', async () => {

return (
<>
<p>has key: {`${snap.has('key')}`}</p>
<p>has key1: {`${snap.has('key')}`}</p>
<p>value1: {`${snap.get('key')}`}</p>
<p>has key2: {`${snap.has('key2')}`}</p>
<p>value2: {`${snap.get('key2')}`}</p>

<button
onClick={() => {
state.set('key', 'value')
Expand All @@ -535,12 +538,19 @@ describe('ui updates - useSnapshot', async () => {
>
set keys
</button>
<button
onClick={() => {
state.delete('key')
}}
>
delete key 1
</button>
<button
onClick={() => {
state.delete('key2')
}}
>
delete keys
delete key 2
</button>
</>
)
Expand All @@ -553,20 +563,34 @@ describe('ui updates - useSnapshot', async () => {
)

await waitFor(() => {
screen.getByText('has key: false')
screen.getByText('has key1: false')
screen.getByText('value1: undefined')
screen.getByText('has key2: false')
screen.getByText('value2: undefined')
})

fireEvent.click(screen.getByText('set keys'))
await waitFor(() => {
screen.getByText('has key: true')
screen.getByText('has key1: true')
screen.getByText('has key2: true')
screen.getByText('value1: value')
screen.getByText('value2: value')
})

fireEvent.click(screen.getByText('delete keys'))
fireEvent.click(screen.getByText('delete key 1'))
await waitFor(() => {
screen.getByText('has key: true')
screen.getByText('has key1: false')
screen.getByText('value1: undefined')
screen.getByText('has key2: true')
screen.getByText('value2: value')
})

fireEvent.click(screen.getByText('delete key 2'))
await waitFor(() => {
screen.getByText('has key1: false')
screen.getByText('value1: undefined')
screen.getByText('has key2: false')
screen.getByText('value2: undefined')
})
})

Expand All @@ -579,6 +603,8 @@ describe('ui updates - useSnapshot', async () => {
<>
<p>has key: {`${snap.has('key')}`}</p>
<p>has key2: {`${snap.has('key2')}`}</p>
<p>value1: {`${snap.get('key')}`}</p>
<p>value2: {`${snap.get('key2')}`}</p>
<button
onClick={() => {
state.set('key', 'value')
Expand Down Expand Up @@ -607,18 +633,24 @@ describe('ui updates - useSnapshot', async () => {
await waitFor(() => {
screen.getByText('has key: false')
screen.getByText('has key2: false')
screen.getByText('value1: undefined')
screen.getByText('value2: undefined')
})

fireEvent.click(screen.getByText('set keys'))
await waitFor(() => {
screen.getByText('has key: true')
screen.getByText('has key2: true')
screen.getByText('value1: value')
screen.getByText('value2: value')
})

fireEvent.click(screen.getByText('clear map'))
await waitFor(() => {
screen.getByText('has key: false')
screen.getByText('has key2: false')
screen.getByText('value1: undefined')
screen.getByText('value2: undefined')
})
})
})
Expand Down Expand Up @@ -722,6 +754,13 @@ describe('ui updates - useSnapshot - iterator methods', () => {
>
Update
</button>
<button
onClick={() => {
state.delete(1)
}}
>
Delete
</button>
<button
onClick={() => {
addItem(1)
Expand Down

0 comments on commit 2dfdd19

Please sign in to comment.