Skip to content

Commit 553ee1c

Browse files
Not found tag 217 (#220)
* added tests + made no file found change * formatting * edited tests --------- Co-authored-by: Arindam Kulshi <[email protected]>
1 parent 69539ce commit 553ee1c

6 files changed

+297
-177
lines changed

OCR/frontend/src/components/FilePreview.test.tsx

-136
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from "react";
2+
import { render, screen } from "@testing-library/react";
3+
import { BrowserRouter } from "react-router-dom";
4+
import ExtractDataHeader from "../ExtractDataHeader";
5+
6+
describe("ExtractDataHeader component", () => {
7+
it("renders the header with correct structure", () => {
8+
render(
9+
<ExtractDataHeader
10+
onBack={vi.fn()}
11+
onSubmit={vi.fn()}
12+
isUploadComplete={false}
13+
/>,
14+
{ wrapper: BrowserRouter }
15+
);
16+
17+
const header = screen.getByRole("banner");
18+
expect(header).toBeInTheDocument();
19+
20+
const closeButton = screen.getByTestId("close-button");
21+
expect(closeButton).toBeInTheDocument();
22+
23+
const heading = screen.getByRole("heading", { name: /extract data/i });
24+
expect(heading).toBeInTheDocument();
25+
26+
const backButton = screen.getByRole("button", { name: /back/i });
27+
expect(backButton).toBeInTheDocument();
28+
expect(backButton).toHaveStyle({
29+
height: "40px",
30+
color: "#adadad",
31+
});
32+
33+
const submitButton = screen.getByRole("button", { name: /submit/i });
34+
expect(submitButton).toBeInTheDocument();
35+
});
36+
37+
it("renders all buttons with correct text", () => {
38+
render(
39+
<ExtractDataHeader
40+
onBack={vi.fn()}
41+
onSubmit={vi.fn()}
42+
isUploadComplete={false}
43+
/>,
44+
{ wrapper: BrowserRouter }
45+
);
46+
47+
const backButton = screen.getByRole("button", { name: /back/i });
48+
const submitButton = screen.getByRole("button", { name: /submit/i });
49+
50+
expect(backButton).toBeInTheDocument();
51+
expect(backButton).toHaveTextContent("Back");
52+
53+
expect(submitButton).toBeInTheDocument();
54+
expect(submitButton).toHaveTextContent("Submit");
55+
});
56+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "react";
2+
import { render, screen } from "@testing-library/react";
3+
import { ExtractStepper } from "../ExtractStepper";
4+
import { ExtractStep } from "../../utils/constants";
5+
6+
describe("ExtractStepper component", () => {
7+
it("renders the StepIndicator component", () => {
8+
render(<ExtractStepper currentStep={ExtractStep.Upload} />);
9+
10+
const stepIndicator = screen.getByTestId("step-indicator");
11+
expect(stepIndicator).toBeInTheDocument();
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { render, screen } from "@testing-library/react";
2+
import "@testing-library/jest-dom";
3+
import { describe, it, expect } from "vitest";
4+
import { ExtractUploadFile } from "../ExtractUploadFile";
5+
import { Wrapper } from "../../utils/tests";
6+
7+
describe("ExtractUploadFile component", () => {
8+
it("renders the header with correct text", () => {
9+
render(<ExtractUploadFile onUploadComplete={vi.fn()} />, {
10+
wrapper: Wrapper,
11+
});
12+
13+
const mainHeading = screen.getByRole("heading", {
14+
name: /choose template and upload new form/i,
15+
});
16+
expect(mainHeading).toBeInTheDocument();
17+
expect(mainHeading).toHaveStyle({ margin: "0" });
18+
19+
const subHeading = screen.getByRole("heading", {
20+
name: /upload new image or pdf to extract data from/i,
21+
});
22+
expect(subHeading).toBeInTheDocument();
23+
expect(subHeading).toHaveStyle({ margin: "10px" });
24+
});
25+
26+
it("renders the upload area with correct structure", () => {
27+
render(<ExtractUploadFile onUploadComplete={vi.fn()} />, {
28+
wrapper: Wrapper,
29+
});
30+
31+
const dashedContainer = screen.getByTestId("dashed-container");
32+
expect(dashedContainer).toBeInTheDocument();
33+
34+
const uploadIcon = screen.getByTestId("upload-icon");
35+
expect(uploadIcon).toBeInTheDocument();
36+
37+
const dragDropText = screen.getByRole("heading", {
38+
name: /drag and drop file here/i,
39+
});
40+
expect(dragDropText).toBeInTheDocument();
41+
expect(dragDropText).toHaveStyle({ fontWeight: "bold" });
42+
43+
const fileInput = screen.getByTestId("file-input-input");
44+
expect(fileInput).toBeInTheDocument();
45+
});
46+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { fireEvent, render, waitFor } from "@testing-library/react";
2+
3+
import { FilePreview } from "../FilePreview";
4+
5+
export const SPACER_GIF =
6+
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
7+
8+
// Test files
9+
export const TEST_TEXT_FILE = new File(["Test File Contents"], "testFile.txt", {
10+
type: "text/plain",
11+
});
12+
13+
export const TEST_PDF_FILE = new File(["Test PDF File"], "testFile.pdf", {
14+
type: "application/pdf",
15+
});
16+
17+
export const TEST_DOC_FILE = new File(["Test doc File"], "testFile.doc", {
18+
type: "application/msword",
19+
});
20+
21+
export const TEST_XLS_FILE = new File(["Test xls File"], "testFile.xls", {
22+
type: "application/vnd.ms-excel",
23+
});
24+
25+
export const TEST_VIDEO_FILE = new File(["Test video File"], "testFile.mp4", {
26+
type: "video/mp4",
27+
});
28+
29+
export const TEST_PNG_FILE = new File(["Test PNG Image"], "testFile.png", {
30+
type: "image/png",
31+
});
32+
33+
describe("FilePreview component", () => {
34+
const testProps = {
35+
imageId: "testImageId_12345",
36+
file: TEST_TEXT_FILE,
37+
};
38+
39+
it("renders without errors", async () => {
40+
const { getByTestId } = await waitFor(() =>
41+
render(<FilePreview {...testProps} />)
42+
);
43+
expect(getByTestId("file-input-preview")).toBeInTheDocument();
44+
expect(getByTestId("file-input-preview")).toHaveClass(
45+
"usa-file-input__preview"
46+
);
47+
expect(getByTestId("file-input-preview")).toHaveAttribute(
48+
"aria-hidden",
49+
"true"
50+
);
51+
expect(getByTestId("file-input-preview")).toHaveTextContent(
52+
testProps.file.name
53+
);
54+
});
55+
56+
it("renders a preview image", async () => {
57+
const { getByTestId } = await waitFor(() =>
58+
render(<FilePreview {...testProps} />)
59+
);
60+
const imageEl = getByTestId("file-input-preview-image");
61+
expect(imageEl).toBeInstanceOf(HTMLImageElement);
62+
expect(imageEl).toHaveAttribute("id", testProps.imageId);
63+
expect(imageEl).toHaveClass("usa-file-input__preview-image");
64+
});
65+
66+
describe("while the file is loading", () => {
67+
it("renders a loading image", async () => {
68+
const spy = vi.spyOn(window.FileReader.prototype, "readAsDataURL");
69+
let fr: FileReader | undefined, blob: Blob | undefined;
70+
// Prevent loadend event from being dispatched until we're ready.
71+
// Grab the blob and FileReader instance when called.
72+
spy.mockImplementationOnce(function (this: FileReader, _blob) {
73+
blob = _blob;
74+
// eslint-disable-next-line @typescript-eslint/no-this-alias
75+
fr = this;
76+
});
77+
const { getByTestId } = await waitFor(() =>
78+
render(<FilePreview {...testProps} />)
79+
);
80+
const imageEl = getByTestId("file-input-preview-image");
81+
82+
expect(imageEl).toHaveClass("is-loading");
83+
expect(imageEl).toHaveAttribute("src", SPACER_GIF);
84+
85+
// Call real `readAsDataURL` with blob to dispatch loadend
86+
expect(fr).toBeDefined();
87+
expect(blob).toBeDefined();
88+
fr!.readAsDataURL(blob!);
89+
90+
await waitFor(() => expect(imageEl).not.toHaveClass("is-loading"));
91+
expect(imageEl).not.toHaveAttribute("src", SPACER_GIF);
92+
});
93+
});
94+
95+
describe("when the file is done loading", () => {
96+
it("renders the file preview image and removes the loading class", async () => {
97+
const { getByTestId } = await waitFor(() =>
98+
render(<FilePreview {...testProps} />)
99+
);
100+
101+
const expectedSrc = "data:text/plain;base64,VGVzdCBGaWxlIENvbnRlbnRz";
102+
103+
const imageEl = getByTestId("file-input-preview-image");
104+
await waitFor(() => expect(imageEl).not.toHaveClass("is-loading"));
105+
expect(imageEl).toHaveAttribute("src", expectedSrc);
106+
});
107+
108+
describe.each([
109+
{ type: "pdf", exts: ["pdf"] },
110+
{ type: "word", exts: ["doc", "pages"] },
111+
{ type: "video", exts: ["mp4", "mov"] },
112+
{ type: "excel", exts: ["xls", "numbers"] },
113+
{ type: "generic", exts: ["dat"] },
114+
])("for a $type file", ({ type, exts }) => {
115+
describe.each(exts)("using extension: %s", (ext) => {
116+
it(`shows the ${type} ${
117+
type !== "generic" ? "generic" : ""
118+
} preview`, async () => {
119+
const testFile = new File([], `testFile.${ext}`);
120+
const { getByTestId } = await waitFor(() =>
121+
render(<FilePreview {...testProps} file={testFile} />)
122+
);
123+
124+
const imageEl = getByTestId("file-input-preview-image");
125+
await waitFor(() => expect(imageEl).not.toHaveClass("is-loading"));
126+
fireEvent.error(imageEl);
127+
await waitFor(() =>
128+
expect(imageEl).toHaveAttribute("src", SPACER_GIF)
129+
);
130+
expect(imageEl).toHaveClass(`usa-file-input__preview-image--${type}`);
131+
});
132+
});
133+
});
134+
});
135+
});

0 commit comments

Comments
 (0)