From f74e0e462ea8b193cc5319589f4e6dee877197a0 Mon Sep 17 00:00:00 2001 From: RAVEYUS Date: Sat, 2 Nov 2024 13:56:35 +0530 Subject: [PATCH] test: Added unit test for UseTextAreaInternal hook (#133) --- .../internal/useTextAreaInternal.test.ts | 225 ++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 __tests__/hooks/internal/useTextAreaInternal.test.ts diff --git a/__tests__/hooks/internal/useTextAreaInternal.test.ts b/__tests__/hooks/internal/useTextAreaInternal.test.ts new file mode 100644 index 00000000..99512d39 --- /dev/null +++ b/__tests__/hooks/internal/useTextAreaInternal.test.ts @@ -0,0 +1,225 @@ +import { renderHook, act } from "@testing-library/react"; +import { useTextAreaInternal } from "../../../src/hooks/internal/useTextAreaInternal"; +import { useRcbEventInternal } from "../../../src/hooks/internal/useRcbEventInternal"; +import { RcbEvent } from "../../../src/constants/RcbEvent"; +import { useBotRefsContext } from "../../../src/context/BotRefsContext"; +import { useSettingsContext } from "../../../src/context/SettingsContext"; +import { expect } from "@jest/globals" + +jest.mock("../../../src/hooks/internal/useRcbEventInternal"); +const mockUseRcbEventInternal = useRcbEventInternal as jest.MockedFunction; + +jest.mock("../../../src/context/BotRefsContext"); +jest.mock("../../../src/context/SettingsContext"); +jest.mock("../../../src/context/BotStatesContext"); + +describe("useTextAreaInternal Hook", () => { + let mockInputRef: any; + let mockPrevInputRef: any; + let mocksettingsRefT: any; + let mocksettingsRefF: any; + let mocksettingsChatLimit: any; + let mocksettingsChatUnLimit: any; + let mocksettingsPreventDefault: any; + beforeEach(() => { + + mockInputRef = { current: { value: "", focus: jest.fn() } }; + mockPrevInputRef = { current: "" }; + mocksettingsRefT = { chatInput: { allowNewline: true } }; + mocksettingsRefF = { chatInput: { allowNewline: false } }; + mocksettingsChatLimit = { chatInput: { characterLimit: 50 } }; + mocksettingsChatUnLimit = { chatInput: { characterLimit: -1 } }; + mocksettingsPreventDefault = { event: { rcbTextAreaChangeValue: true } }; + (require("../../../src/context/BotStatesContext").useBotStatesContext as jest.Mock).mockReturnValue({ + textAreaDisabled: false, + setTextAreaDisabled: jest.fn(), + }); + + + (useBotRefsContext as jest.Mock).mockReturnValue({ + inputRef: mockInputRef, + prevInputRef: mockPrevInputRef, + }); + jest.clearAllMocks(); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should set text area value with newline allowed", () => { + const callRcbEventMock = jest.fn().mockReturnValue({ defaultPrevented: false }); + mockUseRcbEventInternal.mockReturnValue({ + callRcbEvent: callRcbEventMock, + }); + + (useSettingsContext as jest.Mock).mockReturnValue({ + settings: mocksettingsRefT + }); + + const { result } = renderHook(() => useTextAreaInternal()); + + act(() => { + result.current.setTextAreaValue("Hello\nWorld"); + }); + + expect(result.current.getTextAreaValue()).toBe("Hello\nWorld"); + }); + + it("should remove newlines when not allowed", () => { + const callRcbEventMock = jest.fn().mockReturnValue({ defaultPrevented: false }); + mockUseRcbEventInternal.mockReturnValue({ + callRcbEvent: callRcbEventMock, + }); + + (useSettingsContext as jest.Mock).mockReturnValue({ + settings: mocksettingsRefF + }); + + const { result } = renderHook(() => useTextAreaInternal()); + + act(() => { + result.current.setTextAreaValue("Hello\nWorld"); + }); + + expect(result.current.getTextAreaValue()).toBe("Hello World"); + }); + + it("should respect character limit if set", () => { + const callRcbEventMock = jest.fn().mockReturnValue({ defaultPrevented: false }); + mockUseRcbEventInternal.mockReturnValue({ + callRcbEvent: callRcbEventMock, + }); + + (useSettingsContext as jest.Mock).mockReturnValue({ + settings: mocksettingsChatLimit + }); + + const { result } = renderHook(() => useTextAreaInternal()); + + act(() => { + result.current.setTextAreaValue("a".repeat(60)); + }); + + expect(result.current.getTextAreaValue().length).toBe(50); + }); + + it("should handle unlimited character input when characterLimit is -1", () => { + const callRcbEventMock = jest.fn().mockReturnValue({ defaultPrevented: false }); + mockUseRcbEventInternal.mockReturnValue({ + callRcbEvent: callRcbEventMock, + }); + + (useSettingsContext as jest.Mock).mockReturnValue({ + settings: mocksettingsChatUnLimit + }); + + const { result } = renderHook(() => useTextAreaInternal()); + + + act(() => { + result.current.setTextAreaValue("a".repeat(1000)); + }); + + expect(result.current.getTextAreaValue().length).toBe(1000); + }); + + it("should prevent setting value if event is defaultPrevented", () => { + const callRcbEventMock = jest.fn().mockReturnValue({ defaultPrevented: true }); + mockUseRcbEventInternal.mockReturnValue({ + callRcbEvent: callRcbEventMock, + }); + + (useSettingsContext as jest.Mock).mockReturnValue({ + settings: mocksettingsPreventDefault + }); + + const { result } = renderHook(() => useTextAreaInternal()); + + act(() => { + result.current.setTextAreaValue("Test value"); + }); + + expect(result.current.getTextAreaValue()).toBe(""); + }); + + it("should focus on text area when focusTextArea is called", () => { + const callRcbEventMock = jest.fn(); + mockUseRcbEventInternal.mockReturnValue({ + callRcbEvent: callRcbEventMock, + }); + + const { result } = renderHook(() => useTextAreaInternal()); + + act(() => { + result.current.focusTextArea(); + }); + + expect(result.current.textAreaDisabled).toBe(false); + }); + + // let initialTextAreaDisabled = false; + + /* it("should toggle textAreaDisabled state", () => { + // mocks rcb event handler + const callRcbEventMock = jest.fn().mockReturnValue({ defaultPrevented: false }); + mockUseRcbEventInternal.mockReturnValue({ + callRcbEvent: callRcbEventMock, + }); + + // renders the hook within the TestChatBotProvider + const { result } = renderHook(() => useTextAreaInternal(),{ + wrapper: TestChatBotProvider, + }) + console.log(result) + // checks initial value + expect(result.current.textAreaDisabled).toBe(initialTextAreaDisabled); + + // simulates clicking the toggle action + act(() => { + result.current.toggleTextAreaDisabled(); + }); + + + // checks if the state was updated + expect(result.current.textAreaDisabled).toBe(!initialTextAreaDisabled); + }); + */ + + /* + it("should toggle textAreaSensitiveMode state", () => { + const callRcbEventMock = jest.fn(); + mockUseRcbEventInternal.mockReturnValue({ + callRcbEvent: callRcbEventMock, + }); + + const { result } = renderHook(() => useTextAreaInternal(),{ + wrapper: TestChatBotProvider, + }); + + // Simulate toggling the sensitive mode state + act(() => { + result.current.toggleTextAreaSensitiveMode(); + }); + + // Verify the state toggles + expect(result.current.textAreaSensitiveMode).toBe(true); + }); +*/ + + it("should update focus based on visibility", () => { + const callRcbEventMock = jest.fn(); + mockUseRcbEventInternal.mockReturnValue({ + callRcbEvent: callRcbEventMock, + }); + + const { result } = renderHook(() => useTextAreaInternal()); + + act(() => { + result.current.updateTextAreaFocus("some_path"); + }); + + // Assume focus happens correctly, verify it updated focus (mocks internal behavior) + expect(callRcbEventMock).not.toHaveBeenCalledWith(RcbEvent.TEXT_AREA_CHANGE_VALUE); + }); +}); \ No newline at end of file