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

WIP: Add unit test cases for useSubmitInputInternal hook #253

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
53 changes: 53 additions & 0 deletions __tests__/hooks/internal/useSubmitInputInternal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { renderHook, act } from "@testing-library/react";
import { expect } from "@jest/globals";

import { useSubmitInputInternal } from "../../../src/hooks/internal/useSubmitInputInternal";
import { useRcbEventInternal } from "../../../src/hooks/internal/useRcbEventInternal";
import { RcbEvent } from "../../../src/constants/RcbEvent";

import { TestChatBotProvider } from "../../__mocks__/TestChatBotContext";

// mocks internal hooks
jest.mock("../../../src/hooks/internal/useRcbEventInternal");
const mockUseRcbEventInternal = useRcbEventInternal as jest.MockedFunction<typeof useRcbEventInternal>;

/**
* Test for useAudioInternal hook.
*/
describe("useSubmitInputInternal Hook", () => {
let callRcbEventMock: jest.Mock<any, any, any>;

beforeEach(() => {
jest.clearAllMocks();

// mocks rcb event handler
callRcbEventMock = jest.fn().mockReturnValue({ defaultPrevented: false });
mockUseRcbEventInternal.mockReturnValue({
callRcbEvent: callRcbEventMock,
});
});

// Test to ensure initial values (handleSubmitText) are returned correctly from the hook
it("should return initial values from context", () => {
const { result } = renderHook(() => useSubmitInputInternal(), {
wrapper: TestChatBotProvider,
});

expect(result.current.handleSubmitText).toEqual(expect.any(Function));
});

it("should submit input text to rcb event handler", () => {
const params = { inputText: 'some test', sendInChat: true };
const { result } = renderHook(() => useSubmitInputInternal(), {
wrapper: TestChatBotProvider,
});

// simulates submitting text
act(() => {
result.current.handleSubmitText(params.inputText, params.sendInChat);
});

// checks if callRcbEvent was called with rcb-user-submit-text and correct arguments
expect(callRcbEventMock).toHaveBeenCalledWith(RcbEvent.USER_SUBMIT_TEXT, params);
});
});