Skip to content

Commit

Permalink
janna leave queue gen ai test
Browse files Browse the repository at this point in the history
  • Loading branch information
jannalee0819 committed Nov 15, 2024
1 parent 2b49aee commit 7e34d70
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions test/genAIformQueueView.Lee.test.jsx
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

View workflow job for this annotation

GitHub Actions / Build and unit test

test/genAIformQueueView.Lee.test.jsx > Leave Queue Functionality > removes student from queue and navigates to home when leave button is clicked

TestingLibraryElementError: Unable to find an element with the placeholder text of: /enter your name/i Ignored nodes: comments, script, style <body> <div> <div class="min-h-screen p-4" > <div class="max-w-6xl mx-auto" > <div class="mb-2 sm:mb-6 border border-blue-500 rounded-md p-2 sm:p-5" > <div class="text-2xl text-blue-500 font-bold" > OH </div> </div> <div class="grid grid-cols-1 md:grid-cols-2 gap-6" > <div class=" overflow-y-auto bg-white shadow rounded sm:h-[calc(100vh-200px)]" > <div class="p-4 sm:h-full p-4" > <div class="h-full flex flex-col" > <h2 class="text-xl font-semibold mb-4" > Submit Your Question </h2> <form class="flex-1 flex flex-col space-y-4" > <div> <label class="block text-sm font-medium mb-1" > Your Name: </label> <input class="w-full p-2 border rounded-md" required="" type="text" value="" /> </div> <div class="flex-1" > <label class="block text-sm font-medium mb-1" > Your Question: </label> <textarea class="w-full h-[calc(100%-2rem)] p-2 border rounded-md resize-none" required="" /> </div> <div class="flex space-x-4" > <button class="flex-1 bg-blue-500 text-white py-2 px-4 rounded" type="submit" > Submit Question </button> </div> </form> </div> </div> </div> <div class="h-[calc(100vh-350px)] sm:h-[calc(100vh-200px)] bg-gray-50 rounded p-4 flex flex-col" > <h2 class="text-xl font-semibold mb-4 text-gray-600" > Current Queue </h2> <div class="flex-1 overflow-y-auto" > <div class="space-y-2" > <div class="text-center text-gray-500 mt-4" > Queue is currently empty </div> </div> </div> </div> </div> </div> </div> </div> </body> ❯ Object.getElementError node_modules/@testing-library/dom/dist/config.js:37:19 ❯ node_modules/@testing-library/dom/dist/query-helpers.js:76:38 ❯ node_modules/@testing-library/dom/dist/query-helpers.js:52:17 ❯ node_modules/@testing-library/dom/dist/query-helpers.js:95:19 ❯ test/genAIformQueueView.Lee.test.jsx:59:30
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("/");
});
});
});

0 comments on commit 7e34d70

Please sign in to comment.