Skip to content

Commit

Permalink
Add test for hook useFirstInteractionInternal
Browse files Browse the repository at this point in the history
  • Loading branch information
anushree0809 committed Oct 4, 2024
1 parent b4e68c1 commit a7f49d1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
72 changes: 72 additions & 0 deletions __tests__/hooks/useFirstInteractionInternal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { renderHook, act } from '@testing-library/react';
import { useFirstInteractionInternal } from '../../src/hooks/internal/useFirstInteractionInternal';

jest.mock('../../src/context/BotStatesContext', () => ({
useBotStatesContext: jest.fn(() => ({
hasInteractedPage: false,
setHasInteractedPage: jest.fn(),
hasFlowStarted: false,
setHasFlowStarted: jest.fn(),
})),
}));

jest.mock('../../src/context/SettingsContext', () => ({
useSettingsContext: jest.fn(() => ({
settings: {
general: {
flowStartTrigger: 'ON_PAGE_INTERACT',
},
},
})),
}));
class MockSpeechSynthesisUtterance {
text = '';
onend: () => void = () => {};

constructor() {
setTimeout(() => {
this.onend();
}, 0);
}
}

const MockSpeechSynthesis = {
speak: jest.fn().mockImplementation((utterance: MockSpeechSynthesisUtterance) => {
utterance.onend();
}),
};

global.SpeechSynthesisUtterance = MockSpeechSynthesisUtterance as any;
global.speechSynthesis = MockSpeechSynthesis as any;

describe('useFirstInteractionInternal', () => {
let setHasInteractedPage: jest.Mock;
let setHasFlowStarted: jest.Mock;

beforeEach(() => {
setHasInteractedPage = jest.fn();
setHasFlowStarted = jest.fn();

(require('../../src/context/BotStatesContext').useBotStatesContext as jest.Mock).mockReturnValue({
hasInteractedPage: false,
setHasInteractedPage,
hasFlowStarted: false,
setHasFlowStarted,
});
});

afterEach(() => {
jest.clearAllMocks();
});

it('should call setHasInteractedPage and setHasFlowStarted on interaction', () => {
const { result } = renderHook(() => useFirstInteractionInternal());

act(() => {
result.current.handleFirstInteraction();
});

expect(setHasInteractedPage).toHaveBeenCalledWith(true);
expect(setHasFlowStarted).toHaveBeenCalledWith(true);
});
});
2 changes: 1 addition & 1 deletion src/context/BotStatesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Settings } from "../types/Settings";
/**
* Creates the useBotStatesContext() hook to manage common states.
*/
type BotStatesContextType = {
export type BotStatesContextType = {
isBotTyping: boolean;
setIsBotTyping: Dispatch<SetStateAction<boolean>>;

Expand Down

0 comments on commit a7f49d1

Please sign in to comment.