Skip to content

Commit

Permalink
fixed the error casuing test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhurSaluja committed Nov 28, 2024
1 parent 0e9034f commit 375ebae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 151 deletions.
46 changes: 33 additions & 13 deletions __tests__/components/ChatBotBody/ChatMessagePrompt.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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", () => ({
Expand All @@ -16,16 +17,8 @@ jest.mock("../../../src/context/BotRefsContext", () => ({
})),
}));

const mockSetIsScrolling = jest.fn();
let unreadCountMock = 0;
let isScrollingMock = false;

jest.mock("../../../src/context/BotStatesContext", () => ({
useBotStatesContext: jest.fn(() => ({
unreadCount: unreadCountMock,
isScrolling: isScrollingMock,
setIsScrolling: mockSetIsScrolling,
})),
useBotStatesContext: jest.fn(),
}));

jest.mock("../../../src/context/SettingsContext", () => ({
Expand All @@ -50,42 +43,63 @@ jest.mock("../../../src/context/StylesContext", () => ({
}));

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", () => {
unreadCountMock = 2;
isScrollingMock = true;
(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", () => {
unreadCountMock = 0;
isScrollingMock = false;
(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");

Expand All @@ -102,6 +116,12 @@ describe("ChatMessagePrompt Component", () => {
});

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");

Expand Down
138 changes: 0 additions & 138 deletions __tests__/services/VoiceService.test.ts

This file was deleted.

0 comments on commit 375ebae

Please sign in to comment.