forked from sanjitkverma/WolfJobs
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from SiddhiKhairee/sid-testcases
Sid testcases
- Loading branch information
Showing
6 changed files
with
293 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import ChatList from '../../../src/components/Chat/ChatList'; // Adjust the path according to your project structure | ||
|
||
describe('ChatList component', () => { | ||
const mockChats = [ | ||
{ | ||
applicationId: '123', | ||
applicantName: 'John Doe', | ||
lastMessage: { | ||
message: 'Hello!', | ||
createdAt: new Date('2023-01-01T12:00:00Z'), | ||
}, | ||
}, | ||
{ | ||
applicationId: '456', | ||
applicantName: 'Jane Smith', | ||
lastMessage: { | ||
message: 'Hi there!', | ||
createdAt: new Date('2023-01-01T13:30:00Z'), | ||
}, | ||
}, | ||
]; | ||
|
||
const mockOnSelectChat = () => {}; | ||
|
||
it('renders the correct number of chat items', () => { | ||
render(<ChatList chats={mockChats} onSelectChat={mockOnSelectChat} />); | ||
|
||
const listItems = screen.getAllByRole('listitem'); | ||
expect(listItems.length).toBe(mockChats.length); | ||
}); | ||
|
||
it('displays the applicant name for each chat', () => { | ||
render(<ChatList chats={mockChats} onSelectChat={mockOnSelectChat} />); | ||
|
||
mockChats.forEach((chat) => { | ||
const applicantName = screen.getByText(chat.applicantName); | ||
expect(applicantName).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('renders a Divider at the end of the list', () => { | ||
render(<ChatList chats={mockChats} onSelectChat={mockOnSelectChat} />); | ||
|
||
const divider = screen.getByRole('separator'); | ||
expect(divider).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
import ChatView from '../../../src/components/Chat/ChatView'; | ||
|
||
describe("ChatView Component", () => { | ||
const selectedChatMock = { | ||
applicantId: "applicant123", | ||
applicationId: "application456", | ||
applicantName: "John Doe", | ||
messages: [ | ||
{ | ||
_id: "message1", | ||
message: "Hello", | ||
fromUser: "applicant123", | ||
createdAt: new Date().toISOString(), | ||
}, | ||
], | ||
}; | ||
|
||
const refreshChatsMock = vi.fn(); | ||
|
||
it("renders component with messages and allows sending new messages", async () => { | ||
render(<ChatView selectedChat={selectedChatMock} refreshChats={refreshChatsMock} />); | ||
|
||
// Check if message list renders | ||
expect(screen.getByText("Hello")).toBeInTheDocument(); | ||
expect(screen.getByText("John Doe")).toBeInTheDocument(); | ||
|
||
// Find and interact with input field | ||
const inputField = screen.getByLabelText("Type a message...") as HTMLInputElement; | ||
fireEvent.change(inputField, { target: { value: "Hi there!" } }); | ||
expect(inputField.value).toBe("Hi there!"); | ||
|
||
// Submit the form | ||
const submitButton = screen.getByRole("button", { name: /send/i }); | ||
fireEvent.click(submitButton); | ||
|
||
// Ensure input clears after sending | ||
await waitFor(() => expect(inputField.value).toBe("")); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
import ChatWindow from "../../../src/components/Chat/ChatWindow"; | ||
|
||
describe("ChatWindow Component", () => { | ||
const onCloseMock = vi.fn(); | ||
const sendMessageMock = vi.fn(); | ||
const refreshChatsMock = vi.fn(); | ||
|
||
const chatsMock = [ | ||
{ fromUser: "user123", message: "Hello", createdAt: new Date().toISOString() }, | ||
{ fromUser: "applicant123", message: "Hi", createdAt: new Date().toISOString() }, | ||
]; | ||
|
||
const applicationMock = { _id: "app123", applicantid: "applicant123" }; | ||
|
||
beforeEach(() => { | ||
localStorage.setItem("userId", "user123"); | ||
}); | ||
|
||
it("renders chat messages correctly", () => { | ||
render( | ||
<ChatWindow | ||
isOpen={true} | ||
chats={chatsMock} | ||
onClose={onCloseMock} | ||
sendMessage={sendMessageMock} | ||
refreshChats={refreshChatsMock} | ||
application={applicationMock} | ||
/> | ||
); | ||
|
||
expect(screen.getByText("Hello")).toBeInTheDocument(); | ||
expect(screen.getByText("Hi")).toBeInTheDocument(); | ||
expect(screen.getByText("You")).toBeInTheDocument(); | ||
expect(screen.getByText("Applicant")).toBeInTheDocument(); | ||
}); | ||
|
||
it("sends a message and clears the input field on submit", async () => { | ||
render( | ||
<ChatWindow | ||
isOpen={true} | ||
chats={chatsMock} | ||
onClose={onCloseMock} | ||
sendMessage={sendMessageMock} | ||
refreshChats={refreshChatsMock} | ||
application={applicationMock} | ||
/> | ||
); | ||
|
||
const inputField = screen.getByLabelText("Send a message...") as HTMLInputElement; | ||
fireEvent.change(inputField, { target: { value: "New message" } }); | ||
expect(inputField.value).toBe("New message"); | ||
|
||
const sendButton = screen.getByRole("button", { name: /send/i }); | ||
fireEvent.click(sendButton); | ||
|
||
await waitFor(() => { | ||
expect(sendMessageMock).toHaveBeenCalledWith("New message"); | ||
expect(inputField.value).toBe(""); | ||
}); | ||
}); | ||
}); |