Skip to content

Commit

Permalink
Merge pull request #7 from JMarques1196/dev
Browse files Browse the repository at this point in the history
Feat: Progess on unit testing
  • Loading branch information
JMarques1196 authored Feb 13, 2025
2 parents 3cf79be + 5b26e60 commit 02d746b
Showing 1 changed file with 68 additions and 2 deletions.
70 changes: 68 additions & 2 deletions src/components/Menu/activity-menu.test.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,72 @@
import { render, screen } from "@testing-library/react";
import { render, screen, waitFor } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import React from "react";
import Menu from "./activity-menu";
import { collection, getDocs } from "firebase/firestore";

// Mocks
// ResizeObserver
global.ResizeObserver = vi.fn().mockImplementation(() => {
return {
observe: vi.fn(),
disconnect: vi.fn(),
unobserve: vi.fn(),
};
});
// Firebase
vi.mock("firebase/firestore", () => ({
collection: vi.fn(),
getDocs: vi.fn(),
QuerySnapshot: vi.fn(),
}));

vi.mock("src/firebase.js", () => ({
db: vi.fn(),
}));

describe("Menu", () => {
it("Menu renders displaying the selection buttons and dropdown", () => {
// Mock Data
interface MockData {
id: string;
heartRate: number[];
altitude: number[];
}

const mockRunData: MockData[] = [
{
id: "2024-01-01",
heartRate: [70, 75, 80],
altitude: [100, 150, 200],
},
{
id: "2024-01-02",
heartRate: [72, 77, 82],
altitude: [110, 160, 210],
},
];

/* const mockBikingData: MockData[] = [
{
id: '2024-01-03',
heartRate: [75, 80, 85],
altitude: [200, 250, 300],
},
]; */

beforeEach(() => {
vi.clearAllMocks();
// Setup default mock implementation for getDocs
(getDocs as jest.Mock).mockImplementation(() =>
Promise.resolve({
docs: mockRunData.map((data) => ({
data: () => ({ heartRate: data.heartRate, altitude: data.altitude }),
id: data.id,
})),
})
);
});

it("Renders the inital state correctly", () => {
render(<Menu />);
expect(screen.getByText("Menu")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Run" })).toBeInTheDocument();
Expand All @@ -29,4 +83,16 @@ describe("Menu", () => {
).toBeInTheDocument();
screen.debug();
});
// Test Functions
it("Dsplays running data on initial render", async () => {
render(<Menu />);

await waitFor(() => {
const dateSelect = screen.getByLabelText("Choose a date");
expect(dateSelect).toHaveValue("2024-01-01"); // Displays the older activity on first run
});

expect(getDocs).toHaveBeenCalledTimes(1);
expect(collection).toHaveBeenCalledWith(expect.anything(), "run");
});
});

0 comments on commit 02d746b

Please sign in to comment.