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

Solved issue-148 #278

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
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
136 changes: 136 additions & 0 deletions __tests__/components/ChatBotBody/ChatMessagePrompt.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom";
import ChatMessagePrompt from "../../../src/components/ChatBotBody/ChatMessagePrompt/ChatMessagePrompt";
import { useBotStatesContext } from "../../../src/context/BotStatesContext";

// Mock contexts
jest.mock("../../../src/context/BotRefsContext", () => ({
useBotRefsContext: jest.fn(() => ({
chatBodyRef: {
current: {
scrollTop: 0,
scrollHeight: 1000,
clientHeight: 400,
},
},
})),
}));

jest.mock("../../../src/context/BotStatesContext", () => ({
useBotStatesContext: jest.fn(),
}));

jest.mock("../../../src/context/SettingsContext", () => ({
useSettingsContext: jest.fn(() => ({
settings: {
general: { primaryColor: "#000" },
chatWindow: {
showMessagePrompt: true,
messagePromptText: "Scroll to new messages",
},
},
})),
}));

jest.mock("../../../src/context/StylesContext", () => ({
useStylesContext: jest.fn(() => ({
styles: {
chatMessagePromptStyle: { color: "#fff", borderColor: "#ccc" },
chatMessagePromptHoveredStyle: { color: "#000", borderColor: "#000" },
},
})),
}));

describe("ChatMessagePrompt Component", () => {
const mockSetIsScrolling = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
});

afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
});

const renderComponent = () => render(<ChatMessagePrompt />);

it("renders with the correct message prompt text", () => {
(useBotStatesContext as jest.Mock).mockReturnValue({
unreadCount: 0,
isScrolling: false,
setIsScrolling: mockSetIsScrolling,
});

renderComponent();
const messagePrompt = screen.getByText("Scroll to new messages");
expect(messagePrompt).toBeInTheDocument();
});

it("applies visible class when conditions are met", () => {
(useBotStatesContext as jest.Mock).mockReturnValue({
unreadCount: 2,
isScrolling: true,
setIsScrolling: mockSetIsScrolling,
});

renderComponent();
const messagePrompt = screen.getByText("Scroll to new messages");
expect(messagePrompt.parentElement).toHaveClass("rcb-message-prompt-container visible");
});

it("applies hidden class when conditions are not met", () => {
(useBotStatesContext as jest.Mock).mockReturnValue({
unreadCount: 0,
isScrolling: false,
setIsScrolling: mockSetIsScrolling,
});

renderComponent();
const messagePromptContainer = screen.queryByText("Scroll to new messages")?.parentElement;
expect(messagePromptContainer).toHaveClass("rcb-message-prompt-container hidden");
});

it("applies hover styles when hovered", () => {
(useBotStatesContext as jest.Mock).mockReturnValue({
unreadCount: 2,
isScrolling: true,
setIsScrolling: mockSetIsScrolling,
});

renderComponent();
const messagePrompt = screen.getByText("Scroll to new messages");

// Before hover
expect(messagePrompt).toHaveStyle({ color: "#fff", borderColor: "#ccc" });

// Hover
fireEvent.mouseEnter(messagePrompt);
expect(messagePrompt).toHaveStyle({ color: "#000", borderColor: "#000" });

// Leave hover
fireEvent.mouseLeave(messagePrompt);
expect(messagePrompt).toHaveStyle({ color: "#fff", borderColor: "#ccc" });
});

it("scrolls to the bottom when clicked", () => {
(useBotStatesContext as jest.Mock).mockReturnValue({
unreadCount: 2,
isScrolling: true,
setIsScrolling: mockSetIsScrolling,
});

renderComponent();
const messagePrompt = screen.getByText("Scroll to new messages");

fireEvent.mouseDown(messagePrompt);

// Simulate scrolling completion
jest.advanceTimersByTime(600);

// Verify that setIsScrolling was called
expect(mockSetIsScrolling).toHaveBeenCalledWith(false);
});
});
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@testing-library/jest-dom": "^6.6.2",
"@testing-library/react": "^16.0.1",
"@types/dom-speech-recognition": "^0.0.4",
"@types/jest": "^29.5.13",
"@types/jest": "^29.5.14",
"@types/react": "^18.3.6",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^5.60.1",
Expand Down