From a2a0bef29e0b9ca775cb7a21b2900ea83e994f73 Mon Sep 17 00:00:00 2001 From: Thomas Belin Date: Thu, 12 Oct 2023 09:39:03 +0200 Subject: [PATCH 1/2] test: Add tests for typing indicator --- .../InputBar/hooks/useTypingIndicator.test.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/script/components/InputBar/hooks/useTypingIndicator.test.ts b/src/script/components/InputBar/hooks/useTypingIndicator.test.ts index b2e10ad00a5..1fab416f06a 100644 --- a/src/script/components/InputBar/hooks/useTypingIndicator.test.ts +++ b/src/script/components/InputBar/hooks/useTypingIndicator.test.ts @@ -125,4 +125,30 @@ describe('useTypingIndicator', () => { unmount(); expect(onTypingChange).toHaveBeenCalledWith(false); }); + + it('calls the callback only once as user types', () => { + const onTypingChange = jest.fn(); + const {rerender} = renderHook(useTypingIndicator, { + initialProps: {text: '', isEnabled: true, onTypingChange}, + }); + + expect(onTypingChange).not.toHaveBeenCalled(); + + fireEvent.keyDown(document); + + rerender({text: 'a', isEnabled: true, onTypingChange}); + expect(onTypingChange).toHaveBeenCalledWith(true); + + rerender({text: 'ab', isEnabled: true, onTypingChange}); + expect(onTypingChange).toHaveBeenCalledTimes(1); + + rerender({text: 'abc', isEnabled: true, onTypingChange}); + expect(onTypingChange).toHaveBeenCalledTimes(1); + + jest.advanceTimersByTime(TYPING_TIMEOUT + 1); + expect(onTypingChange).toHaveBeenCalledWith(false); + + rerender({text: 'abcd', isEnabled: true, onTypingChange}); + expect(onTypingChange).toHaveBeenCalledTimes(3); + }); }); From 1bae169c23f37c708d2bfbe70e7726b3ea49d532 Mon Sep 17 00:00:00 2001 From: Thomas Belin Date: Thu, 12 Oct 2023 09:43:31 +0200 Subject: [PATCH 2/2] add tests for unmount --- .../InputBar/hooks/useTypingIndicator.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/script/components/InputBar/hooks/useTypingIndicator.test.ts b/src/script/components/InputBar/hooks/useTypingIndicator.test.ts index 1fab416f06a..0d77d059543 100644 --- a/src/script/components/InputBar/hooks/useTypingIndicator.test.ts +++ b/src/script/components/InputBar/hooks/useTypingIndicator.test.ts @@ -151,4 +151,14 @@ describe('useTypingIndicator', () => { rerender({text: 'abcd', isEnabled: true, onTypingChange}); expect(onTypingChange).toHaveBeenCalledTimes(3); }); + + it('does not call the callback with false on unmount when the user has not typed', () => { + const onTypingChange = jest.fn(); + const {unmount} = renderHook(useTypingIndicator, { + initialProps: {text: '', isEnabled: true, onTypingChange}, + }); + + unmount(); + expect(onTypingChange).not.toHaveBeenCalledWith(false); + }); });