From f681a39a14f5f3e8edd42e0c94728e24862ff451 Mon Sep 17 00:00:00 2001 From: gioboa Date: Tue, 5 Mar 2024 23:12:24 +0100 Subject: [PATCH] test: add test for #5662 issue --- packages/qwik/src/core/v2/use-store.unit.tsx | 108 ++++++++++++++----- 1 file changed, 83 insertions(+), 25 deletions(-) diff --git a/packages/qwik/src/core/v2/use-store.unit.tsx b/packages/qwik/src/core/v2/use-store.unit.tsx index 504c4371d8d..f78fe5ab60a 100644 --- a/packages/qwik/src/core/v2/use-store.unit.tsx +++ b/packages/qwik/src/core/v2/use-store.unit.tsx @@ -26,17 +26,18 @@ Error.stackTraceLimit = 100; it('should render value', async () => { const Cmp = component$(() => { const store = useStore({ items: [{ num: 0 }] }); - return (<> - {store.items.map((item, key) => ( -
{item.num}
- ))} - + return ( + <> + {store.items.map((item, key) => ( +
{item.num}
+ ))} + ); }); const { vNode } = await render(, { debug }); expect(vNode).toMatchVDOM( - +
0
@@ -310,12 +311,16 @@ Error.stackTraceLimit = 100; return ( <> @@ -362,23 +367,28 @@ Error.stackTraceLimit = 100; const Cmp = component$(() => { const count = useSignal(0); const store = useStore({ items: [{ num: 0 }] }); - useTaskQrl(inlinedQrl(({ cleanup }) => { - const [count, store] = useLexicalScope(); + useTaskQrl( + inlinedQrl( + ({ cleanup }) => { + const [count, store] = useLexicalScope(); - const intervalId = setInterval(() => { - count.value++; - store.items = store.items.map((i: { num: number }) => ({ num: i.num + 1 })); - }, 500); + const intervalId = setInterval(() => { + count.value++; + store.items = store.items.map((i: { num: number }) => ({ num: i.num + 1 })); + }, 500); - cleanup(() => clearInterval(intervalId)); - }, 's_useTask', [count, store]), { - eagerness: 'visible', - }); + cleanup(() => clearInterval(intervalId)); + }, + 's_useTask', + [count, store] + ), + { + eagerness: 'visible', + } + ); return ( <> -
- Count: {count.value}! -
+
Count: {count.value}!
{store.items.map((item, key) => (
{item.num}
))} @@ -427,5 +437,53 @@ Error.stackTraceLimit = 100; ); vi.useRealTimers(); }); + + it('#5662 - should update value in the list', async () => { + const Cmp = component$(() => { + const store = useStore<{ users: { name: string }[] }>({ users: [{ name: 'Giorgio' }] }); + + return ( +
+ {store.users.map((user, key) => ( + { + const [store] = useLexicalScope(); + store.users = store.users.map(({ name }: { name: string }) => ({ + name: name === user.name ? name + '!' : name, + })); + }, + 's_onClick', + [store] + )} + > + {user.name} + + ))} +
+ ); + }); + const { vNode, container } = await render(, { debug }); + expect(vNode).toMatchVDOM( + +
+ {'Giorgio'} +
+
+ ); + await trigger(container.element, 'span:first-of-type', 'click'); + await trigger(container.element, 'span:first-of-type', 'click'); + await trigger(container.element, 'span:first-of-type', 'click'); + await trigger(container.element, 'span:first-of-type', 'click'); + await trigger(container.element, 'span:first-of-type', 'click'); + expect(vNode).toMatchVDOM( + +
+ {'Giorgio!!!!!'} +
+
+ ); + }); }); });