Skip to content

Commit

Permalink
Successfully added unit test cases for usePathsInternal (tjtanjin#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
anjumnnit authored and RAVEYUS committed Nov 2, 2024
1 parent 2e35813 commit dc77bc7
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions __tests__/hooks/internal/usePathsInternal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { renderHook, act } from "@testing-library/react";
import { usePathsInternal } from "../../../src/hooks/internal/usePathsInternal";
import { useSettingsContext } from "../../../src/context/SettingsContext";
import { usePathsContext } from "../../../src/context/PathsContext";
import { useBotStatesContext } from "../../../src/context/BotStatesContext";
import { useBotRefsContext } from "../../../src/context/BotRefsContext";
import { emitRcbEvent } from "../../../src/services/RcbEventService";
import { RcbEvent } from "../../../src/constants/RcbEvent";

// Mock the necessary contexts and services
jest.mock("../../../src/context/SettingsContext");
jest.mock("../../../src/context/PathsContext");
jest.mock("../../../src/context/BotStatesContext");
jest.mock("../../../src/context/BotRefsContext");
jest.mock("../../../src/services/RcbEventService");

describe("usePathsInternal Hook", () => {
const mockSetPaths = jest.fn();
const mockSetIsBotTyping = jest.fn();
const mockSetTextAreaDisabled = jest.fn();
const mockSetTextAreaSensitiveMode = jest.fn();
const mockSetBlockAllowsAttachment = jest.fn();
const mockBotIdRef = { current: "bot-1" };
const mockPaths = ["path1", "path2"];

beforeEach(() => {
jest.clearAllMocks();
(useSettingsContext as jest.Mock).mockReturnValue({
settings: {
event: { rcbChangePath: true },
chatInput: { blockSpam: true },
},
});
(usePathsContext as jest.Mock).mockReturnValue({
paths: mockPaths,
setPaths: mockSetPaths,
});
(useBotStatesContext as jest.Mock).mockReturnValue({
setIsBotTyping: mockSetIsBotTyping,
setTextAreaDisabled: mockSetTextAreaDisabled,
setTextAreaSensitiveMode: mockSetTextAreaSensitiveMode,
blockAllowsAttachment: false,
setBlockAllowsAttachment: mockSetBlockAllowsAttachment,
});
(useBotRefsContext as jest.Mock).mockReturnValue({
botIdRef: mockBotIdRef,
});

// Mock emitRcbEvent to return an object with defaultPrevented
(emitRcbEvent as jest.Mock).mockReturnValue({
defaultPrevented: false,
});
});

it("should go to a new path and emit the rcbChangePath event", async () => {
const { result } = renderHook(() => usePathsInternal());

await act(async () => {
const success = result.current.goToPath("newPath");
expect(success).toBe(true);
});

// Check that mockSetPaths was called with a function
expect(mockSetPaths).toHaveBeenCalledWith(expect.any(Function));

// Simulate calling the function with previous state
const setPathsCallback = mockSetPaths.mock.calls[0][0]; // Get the first call's first argument
const newState = setPathsCallback(mockPaths); // Simulate previous state being passed to the function

// Check that the function returns the correct new state
expect(newState).toEqual([...mockPaths, "newPath"]);

expect(mockSetIsBotTyping).toHaveBeenCalledWith(true);
expect(mockSetTextAreaDisabled).toHaveBeenCalledWith(true);
expect(mockSetTextAreaSensitiveMode).toHaveBeenCalledWith(false);
expect(emitRcbEvent).toHaveBeenCalledWith(
RcbEvent.CHANGE_PATH,
{ botId: mockBotIdRef.current, currPath: "path2", prevPath: "path1" },
{ currPath: "path2", prevPath: "path1", nextPath: "newPath" }
);
});


it("should prevent going to a new path if the event is defaultPrevented", async () => {
// Mocking the event being prevented
(emitRcbEvent as jest.Mock).mockReturnValue({ defaultPrevented: true });

const { result } = renderHook(() => usePathsInternal());

await act(async () => {
const success = result.current.goToPath("blockedPath");
expect(success).toBe(false);
});

expect(mockSetPaths).not.toHaveBeenCalled();
});
});

0 comments on commit dc77bc7

Please sign in to comment.