Skip to content

Commit

Permalink
Add unit test for bug with setting object/array in state in useEffect…
Browse files Browse the repository at this point in the history
… hook
  • Loading branch information
mokkabonna committed Sep 20, 2023
1 parent 3a9adcf commit e0bc194
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/core/core.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e0bc194

Please sign in to comment.