From 95ca1b5646035130e553444fcdafdac1120202ff Mon Sep 17 00:00:00 2001 From: magicdawn Date: Thu, 24 Oct 2024 15:06:29 +0800 Subject: [PATCH] chore(tests): add test for proxySet .has with add/delete/clear --- tests/proxySet.test.tsx | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/proxySet.test.tsx b/tests/proxySet.test.tsx index 8ff894a9..8d28f95f 100644 --- a/tests/proxySet.test.tsx +++ b/tests/proxySet.test.tsx @@ -285,10 +285,12 @@ describe('delete', () => { const TestComponent = () => { const snap = useSnapshot(state) + const has = snap.set.has(valueToDelete) return ( <>
size: {snap.set.size}
+
has: {has.toString()}
@@ -303,6 +305,7 @@ describe('delete', () => { ) getByText(`size: ${state.set.size}`) + getByText('has: true') const expectedSizeAfterDelete = state.set.size > 1 ? state.set.size - 1 : 0 @@ -310,6 +313,7 @@ describe('delete', () => { fireEvent.click(getByText('button')) await waitFor(() => { getByText(`size: ${expectedSizeAfterDelete}`) + getByText('has: false') }) }) }) @@ -323,6 +327,51 @@ describe('delete', () => { }) }) +describe('has', () => { + it('should support has with add/delete/clear', async () => { + const state = proxy({ set: proxySet() }) + const val = 'hello' + + const TestComponent = () => { + const set = useSnapshot(state.set) + // size accessor has special logic, do not access `.size` here + // const size = set.size + const has = set.has(val).toString() + return ( + <> +
has: {has}
+ + + + + ) + } + + const { getByText } = render( + + + , + ) + + const clickAdd = () => fireEvent.click(getByText('add')) + const clickDelete = () => fireEvent.click(getByText('delete')) + const clickClear = () => fireEvent.click(getByText('clear')) + const expectHasToBe = (v: boolean) => getByText(`has: ${v.toString()}`) + + expectHasToBe(false) + + clickAdd() + await waitFor(() => expectHasToBe(true)) + clickDelete() + await waitFor(() => expectHasToBe(false)) + + clickAdd() + await waitFor(() => expectHasToBe(true)) + clickClear() + await waitFor(() => expectHasToBe(false)) + }) +}) + describe('proxySet internal', () => { it('should be sealed', () => { expect(Object.isSealed(proxySet())).toBe(true)