Skip to content

Commit

Permalink
added new test and moved this.epoch in proxyMap
Browse files Browse the repository at this point in the history
  • Loading branch information
overthemike committed Nov 5, 2024
1 parent 2dfdd19 commit d616712
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/vanilla/utils/proxyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ 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) {
return undefined
}
Expand Down
33 changes: 33 additions & 0 deletions tests/proxyMap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,39 @@ describe('ui updates - useSnapshot', async () => {
})
})

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

return (
<>
<p>value: {`${snap.get('key')}`}</p>
<button
onClick={() => {
state.set('key', 'value')
}}
>
set key
</button>
</>
)
}

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

screen.getByText('value: undefined')

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

it('should update ui when calling has before and after settiing multile keys and deleting a single one (first item)', async () => {
const state = proxyMap()
const TestComponent = () => {
Expand Down

0 comments on commit d616712

Please sign in to comment.