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

test: add unit tests for useMessagesInternal hook #234

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

});