Skip to content

Commit

Permalink
test: Add tests for typing indicator (#15986)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc authored Oct 12, 2023
1 parent 7b12d31 commit a4134e5
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/script/components/InputBar/hooks/useTypingIndicator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,40 @@ 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);
});

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);
});
});

0 comments on commit a4134e5

Please sign in to comment.