Skip to content

Commit

Permalink
test: add unit tests for useMessagesInternal hook (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
Goketech authored Oct 21, 2024
1 parent 8d8c8c7 commit 2d3053b
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions __tests__/hooks/internal/useMessagesInternal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { renderHook } from "@testing-library/react";
import { act } from "react";
import { useMessagesInternal } from "../../../src/hooks/internal/useMessagesInternal";
import { useSettingsContext } from "../../../src/context/SettingsContext";
import { useMessagesContext } from "../../../src/context/MessagesContext";
import { useBotStatesContext } from "../../../src/context/BotStatesContext";
import { useBotRefsContext } from "../../../src/context/BotRefsContext";
import { useRcbEventInternal } from "../../../src/hooks/internal/useRcbEventInternal";
import { Message } from "../../../src/types/Message";

jest.mock("../../../src/context/SettingsContext");
jest.mock("../../../src/context/MessagesContext");
jest.mock("../../../src/context/BotStatesContext");
jest.mock("../../../src/context/BotRefsContext");
jest.mock("../../../src/hooks/internal/useRcbEventInternal");
jest.mock("../../../src/services/AudioService");
jest.mock("../../../src/services/ChatHistoryService");

describe("useMessagesInternal", () => {
const mockSetMessages = jest.fn();
const mockSetIsBotTyping = jest.fn();
const mockSetUnreadCount = jest.fn();
const mockCallRcbEvent = jest.fn();
const mockStreamMessageMap = { current: new Map() };
const mockMessages: Message[] = [];

beforeEach(() => {
jest.clearAllMocks();
(useSettingsContext as jest.Mock).mockReturnValue({
settings: {
botBubble: { dangerouslySetInnerHtml: false, simStream: false },
userBubble: { dangerouslySetInnerHtml: false, simStream: false },
event: {},
},
});
(useMessagesContext as jest.Mock).mockReturnValue({
messages: mockMessages,
setMessages: mockSetMessages,
});
(useBotStatesContext as jest.Mock).mockReturnValue({
audioToggledOn: false,
isChatWindowOpen: true,
setIsBotTyping: mockSetIsBotTyping,
setUnreadCount: mockSetUnreadCount,
});
(useBotRefsContext as jest.Mock).mockReturnValue({
streamMessageMap: mockStreamMessageMap,
});
(useRcbEventInternal as jest.Mock).mockReturnValue({
callRcbEvent: mockCallRcbEvent,
});
});

it("should return expected functions and values", () => {
const { result } = renderHook(() => useMessagesInternal());

expect(result.current).toHaveProperty("endStreamMessage");
expect(result.current).toHaveProperty("injectMessage");
expect(result.current).toHaveProperty("removeMessage");
expect(result.current).toHaveProperty("streamMessage");
expect(result.current).toHaveProperty("messages");
expect(result.current).toHaveProperty("setMessages");
});

it("should inject a message correctly", async () => {
const { result } = renderHook(() => useMessagesInternal());

await act(async () => {
const messageId = await result.current.injectMessage("Test message", "bot");
expect(messageId).toBeTruthy();
});

expect(mockSetMessages).toHaveBeenCalled();
expect(mockSetUnreadCount).toHaveBeenCalledWith(expect.any(Function));
});

it("should remove a message correctly", async () => {
const mockMessageId = "test-id";
const mockMessage: Message = { id: mockMessageId, content: "Test", sender: "bot", type: "text",
timestamp: String(Date.now()) };
(useMessagesContext as jest.Mock).mockReturnValue({
messages: [mockMessage],
setMessages: mockSetMessages,
});

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

await act(async () => {
const removedId = await result.current.removeMessage(mockMessageId);
expect(removedId).toBe(mockMessageId);
});

expect(mockSetMessages).toHaveBeenCalled();
expect(mockSetUnreadCount).toHaveBeenCalledWith(expect.any(Function));
});

it("should stream a message correctly", async () => {
const { result } = renderHook(() => useMessagesInternal());

await act(async () => {
const messageId = await result.current.streamMessage("Test stream", "bot");
expect(messageId).toBeTruthy();
});

expect(mockSetMessages).toHaveBeenCalled();
expect(mockSetUnreadCount).toHaveBeenCalledWith(expect.any(Function));
expect(mockStreamMessageMap.current.has("bot")).toBeTruthy();
});

it("should end stream message correctly", async () => {
mockStreamMessageMap.current.set("bot", "test-id");
const { result } = renderHook(() => useMessagesInternal());

await act(async () => {
const success = await result.current.endStreamMessage("bot");
expect(success).toBeTruthy();
});

expect(mockStreamMessageMap.current.has("bot")).toBeFalsy();
});

});

0 comments on commit 2d3053b

Please sign in to comment.