Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for hook useFirstInteractionInternal #134

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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