From 72e2a1654cc38d67bc3ed9b83a731695e5b1b194 Mon Sep 17 00:00:00 2001 From: Maria Zadnepryanets Date: Wed, 30 Oct 2024 12:53:08 +0100 Subject: [PATCH] test: add initial useSubmitInputInternal tests --- .../internal/useSubmitInputInternal.test.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 __tests__/hooks/internal/useSubmitInputInternal.test.ts diff --git a/__tests__/hooks/internal/useSubmitInputInternal.test.ts b/__tests__/hooks/internal/useSubmitInputInternal.test.ts new file mode 100644 index 00000000..8e0969ac --- /dev/null +++ b/__tests__/hooks/internal/useSubmitInputInternal.test.ts @@ -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; + +/** + * Test for useAudioInternal hook. + */ +describe("useSubmitInputInternal Hook", () => { + let callRcbEventMock: jest.Mock; + + 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); + }); +}); \ No newline at end of file