-
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.
- Loading branch information
1 parent
2b49aee
commit 7e34d70
Showing
1 changed file
with
86 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
import { vi, describe, it, expect, beforeEach } from "vitest"; | ||
import FormQueueView from "../src/pages/formQueue/formQueueView"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
import { | ||
getQueue, | ||
addQuestion, | ||
updateQuestion, | ||
deleteQuestion, | ||
getClassName, | ||
} from "../src/firebase/studentSideFunctions"; | ||
import * as router from "react-router-dom"; | ||
|
||
// Mock react-router-dom's useNavigate and useParams | ||
vi.mock("react-router-dom", async () => { | ||
const actual = await vi.importActual("react-router-dom"); | ||
return { | ||
...actual, | ||
useNavigate: vi.fn(), | ||
useParams: () => ({ code: "testCode" }), | ||
}; | ||
}); | ||
|
||
// Mock the Firebase functions | ||
vi.mock("../src/firebase/studentSideFunctions", () => ({ | ||
getQueue: vi.fn(), | ||
addQuestion: vi.fn(), | ||
updateQuestion: vi.fn(), | ||
deleteQuestion: vi.fn(), | ||
getClassName: vi.fn(), | ||
})); | ||
|
||
describe("Leave Queue Functionality", () => { | ||
const mockNavigate = vi.fn(); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
router.useNavigate.mockImplementation(() => mockNavigate); | ||
window.confirm = vi.fn(() => true); // Mock confirm to always return true | ||
|
||
// Setup basic mock implementations | ||
getQueue.mockResolvedValue([]); | ||
getClassName.mockResolvedValue("Test Class"); | ||
deleteQuestion.mockResolvedValue(); | ||
}); | ||
|
||
it("removes student from queue and navigates to home when leave button is clicked", async () => { | ||
// Setup: Mock adding a question to get into the queue first | ||
const questionId = "test-question-id"; | ||
addQuestion.mockResolvedValueOnce(questionId); | ||
|
||
render( | ||
<BrowserRouter> | ||
<FormQueueView /> | ||
</BrowserRouter> | ||
); | ||
|
||
// First submit a question to get into the queue | ||
const nameInput = screen.getByPlaceholderText(/enter your name/i); | ||
Check failure on line 59 in test/genAIformQueueView.Lee.test.jsx GitHub Actions / Build and unit testtest/genAIformQueueView.Lee.test.jsx > Leave Queue Functionality > removes student from queue and navigates to home when leave button is clicked
|
||
const questionInput = screen.getByPlaceholderText(/enter your question/i); | ||
|
||
fireEvent.change(nameInput, { target: { value: "Test Student" } }); | ||
fireEvent.change(questionInput, { target: { value: "Test Question" } }); | ||
|
||
const submitButton = screen.getByText(/submit/i); | ||
fireEvent.click(submitButton); | ||
|
||
// Wait for the question to be added | ||
await waitFor(() => { | ||
expect(addQuestion).toHaveBeenCalled(); | ||
}); | ||
|
||
// Now click the leave queue button | ||
const leaveButton = screen.getByText(/leave queue/i); | ||
fireEvent.click(leaveButton); | ||
|
||
// Verify the expected behaviors | ||
await waitFor(() => { | ||
// Verify the question was deleted from Firebase | ||
expect(deleteQuestion).toHaveBeenCalledWith(questionId.toString(), "testCode"); | ||
|
||
// Verify navigation to home page occurred | ||
expect(mockNavigate).toHaveBeenCalledWith("/"); | ||
}); | ||
}); | ||
}); |