From e0bc194a0d1b70833f014334698f668cba0b9c90 Mon Sep 17 00:00:00 2001 From: Martin Hansen Date: Wed, 20 Sep 2023 15:43:29 +0200 Subject: [PATCH] Add unit test for bug with setting object/array in state in useEffect hook --- packages/core/core.test.mts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/core/core.test.mts b/packages/core/core.test.mts index d685315376..8314110d5e 100644 --- a/packages/core/core.test.mts +++ b/packages/core/core.test.mts @@ -83,6 +83,31 @@ describe('createPrompt()', () => { await expect(answer).resolves.toEqual('up'); }); + it('useEffect: works with setting state at once with objects', async () => { + const Prompt = (config: { message: string }, done: (value: string) => void) => { + const [value, setValue] = useState([1, 2]); + + useEffect(() => { + setValue([1, 3]); + }, []); + + useKeypress((key: KeypressEvent) => { + if (isEnterKey(key)) { + done(String(value)); + } + }); + + return String(value); + }; + + const prompt = createPrompt(Prompt); + const { answer, events } = await render(prompt, { message: 'Question' }); + events.keypress('enter'); + + const resolvedAnswer = await answer; + await expect(resolvedAnswer).toEqual('1,3'); + }); + it('useEffect: re-run only on change', async () => { const effect = vi.fn(); const effectCleanup = vi.fn();