Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Antonelli committed Jul 8, 2024
1 parent b7b2128 commit 2c2a9cb
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions src/app/page.test.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,57 @@
// src/components/Home/Home.test.js
import {render, screen, fireEvent, waitFor} from "@testing-library/react";
import "@testing-library/jest-dom";
import Home from "./page";
import {describe} from "node:test";

describe("Tests api call", () => {
test("Verify if csv report downloads on button click", async () => {
const {getByText} = render(<Home />);
jest.mock("./handle-download");
import {handleDownload} from "./handle-download";

describe("File download UI feedback", () => {
beforeEach(() => {
handleDownload.mockClear();
});

// Find the button to download report
test("Checks if download button exists", () => {
const {getByText} = render(<Home />);
const button = getByText("Download Report");
expect(button).toBeInTheDocument();
});

test("Checks if the success message displays after successful download", async () => {
const {getByText} = render(<Home />);

const button = getByText("Download Report");

handleDownload.mockImplementation(() => {
const mockBlob = new Blob(["mock data"], {type: "text/csv"});
return Promise.resolve({
ok: true,
blob: () => Promise.resolve(mockBlob),
});
});

fireEvent.click(button);

await waitFor(() => {
const successMessage = screen.getByText("Download successful!");
expect(successMessage).toBeInTheDocument();
});
});

test("Checks if the error message displays after download fails", async () => {
const {getByText} = render(<Home />);

const button = getByText("Download Report");

handleDownload.mockImplementation(() => {
return Promise.reject(new Error());
});

// click the button
fireEvent.click(button);

await waitFor(() => {
const downloadLink = document.querySelector("a");
expect(downloadLink).toBeInTheDocument();
expect(downloadLink.getAttribute("href")).toBeDefined();
expect(downloadLink.getAttribute("download")).toBe("stripe_report.csv");
const errorMessage = screen.getByText("Error: Something went wrong. Please try again");
expect(errorMessage).toBeInTheDocument();
});
});
});

0 comments on commit 2c2a9cb

Please sign in to comment.