|
| 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