Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
overthemike committed Nov 5, 2024
1 parent 0dfc656 commit 6d7f04e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/vanilla/utils/proxyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
get(key: K) {
const map = getMapForThis(this)
const index = map.get(key)
this.epoch // touch property for tracking
if (index === undefined) {
this.epoch // touch property for tracking
return undefined
}
return this.data[index]
Expand Down
57 changes: 56 additions & 1 deletion tests/proxyMap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ describe('ui updates - useSnapshot', async () => {
})
})

it('should update ui when calling has before and after settiing multile keys and deleting a single one (first item)', async () => {
it('should update ui when calling has/get before and after settiing multile keys and deleting a single one multiple times', async () => {
const state = proxyMap()
const TestComponent = () => {
const snap = useSnapshot(state)
Expand Down Expand Up @@ -627,6 +627,61 @@ describe('ui updates - useSnapshot', async () => {
})
})

it('should update ui when calling only one get with absent key added later', async () => {
const state = proxyMap()
const TestComponent = () => {
const snap = useSnapshot(state)

return (
<>
<button
onClick={() => {
state.set('key', 'value')
}}
>
set key
</button>
<button
onClick={() => {
state.delete('key')
}}
>
delete key
</button>
</>
)
}

const SeparateComponent = () => {
const snap = useSnapshot(state)

return (
<>
<p>value: {`${snap.get('key')}`}</p>
</>
)
}

render(
<StrictMode>
<TestComponent />
<SeparateComponent />
</StrictMode>,
)

screen.getByText('value: undefined')

fireEvent.click(screen.getByText('set key'))
await waitFor(() => {
screen.getByText('value: value')
})

fireEvent.click(screen.getByText('delete key'))
await waitFor(() => {
screen.getByText('value: undefined')
})
})

it('should update ui when clearing the map', async () => {
const state = proxyMap()
const TestComponent = () => {
Expand Down

0 comments on commit 6d7f04e

Please sign in to comment.