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

Successfully added unit test cases for usePathsInternal #244

Merged
merged 1 commit into from
Oct 26, 2024
Merged
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
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();
});
});