-
Notifications
You must be signed in to change notification settings - Fork 0
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
b7b2128
commit 2c2a9cb
Showing
1 changed file
with
42 additions
and
10 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 |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); |