|
| 1 | +import React from "react"; |
| 2 | +import { screen, render, act } from "@testing-library/react"; |
| 3 | +import { BasicTestUpload } from "./TestComponents"; |
| 4 | +import userEvent from "@testing-library/user-event"; |
| 5 | + |
| 6 | +test("It uploads a file correctly", async () => { |
| 7 | + global.xhrOpen = jest.fn(); |
| 8 | + global.xhrSend = jest.fn(); |
| 9 | + render(<BasicTestUpload method="POST" url="github.gov" />); |
| 10 | + //loading is hidden |
| 11 | + expect(screen.queryByText("loading")).toEqual(null); |
| 12 | + |
| 13 | + //upload a file into the input |
| 14 | + const file = new File(["hello"], "hello.png", { type: "image/png" }); |
| 15 | + userEvent.upload(screen.getByLabelText("upload"), file); |
| 16 | + |
| 17 | + //loading is now shown |
| 18 | + screen.getByText("loading"); |
| 19 | + |
| 20 | + await act(() => new Promise((resolve) => setTimeout(resolve, 0))); |
| 21 | + |
| 22 | + expect(global.xhrOpen.mock.calls[0][0]).toEqual("POST"); |
| 23 | + expect(global.xhrOpen.mock.calls[0][1]).toEqual("github.gov"); |
| 24 | + //check the type sent to the server |
| 25 | + expect(global.xhrSend.mock.calls[0][0].name).toEqual("hello.png"); |
| 26 | + expect(global.xhrSend.mock.calls[0][0].type).toEqual("image/png"); |
| 27 | +}); |
| 28 | + |
| 29 | +test("Renders done after upload is complete", async () => { |
| 30 | + global.xhrListener = jest.fn(); |
| 31 | + render(<BasicTestUpload method="POST" url="github.gov" />); |
| 32 | + |
| 33 | + //upload a file into the input |
| 34 | + const file = new File(["hello"], "hello.png", { type: "image/png" }); |
| 35 | + userEvent.upload(screen.getByLabelText("upload"), file); |
| 36 | + //wait for the next tick |
| 37 | + await act(() => new Promise((resolve) => setTimeout(resolve, 0))); |
| 38 | + |
| 39 | + expect(global.xhrListener.mock.calls[1][0]).toEqual("load"); |
| 40 | + expect(screen.queryByText("done")).toEqual(null); |
| 41 | + |
| 42 | + //call the load method on the xhr mock |
| 43 | + act(() => global.xhrListener.mock.calls[1][1]()); |
| 44 | + |
| 45 | + expect(screen.getByText("done")).toBeTruthy(); |
| 46 | +}); |
| 47 | + |
| 48 | +test("Renders upload progress", async () => { |
| 49 | + global.xhrListener = jest.fn(); |
| 50 | + render(<BasicTestUpload method="POST" url="github.gov" />); |
| 51 | + |
| 52 | + //upload a file into the input |
| 53 | + const file = new File(["hello"], "hello.png", { type: "image/png" }); |
| 54 | + userEvent.upload(screen.getByLabelText("upload"), file); |
| 55 | + //wait for the next tick |
| 56 | + await act(() => new Promise((resolve) => setTimeout(resolve, 0))); |
| 57 | + |
| 58 | + expect(global.xhrListener.mock.calls[0][0]).toEqual("progress"); |
| 59 | + expect(screen.queryByText("done")).toEqual(null); |
| 60 | + |
| 61 | + //call the load method on the xhr mock |
| 62 | + act(() => global.xhrListener.mock.calls[0][1]({ loaded: 20, total: 100 })); |
| 63 | + |
| 64 | + expect(screen.getByText("loading")).toBeTruthy(); |
| 65 | + expect(screen.getByText("20% progress")).toBeTruthy(); |
| 66 | +}); |
| 67 | + |
| 68 | +test("Renders error message if xhr fails", async () => { |
| 69 | + global.xhrListener = jest.fn(); |
| 70 | + render(<BasicTestUpload method="POST" url="github.gov" />); |
| 71 | + |
| 72 | + //upload a file into the input |
| 73 | + const file = new File(["hello"], "hello.png", { type: "image/png" }); |
| 74 | + userEvent.upload(screen.getByLabelText("upload"), file); |
| 75 | + //wait for the next tick |
| 76 | + await act(() => new Promise((resolve) => setTimeout(resolve, 0))); |
| 77 | + |
| 78 | + expect(global.xhrListener.mock.calls[2][0]).toEqual("error"); |
| 79 | + expect(screen.queryByText("bad error!")).toEqual(null); |
| 80 | + |
| 81 | + //call the load method on the xhr mock |
| 82 | + act(() => global.xhrListener.mock.calls[2][1]("bad error!")); |
| 83 | + |
| 84 | + expect(screen.getByText("bad error!")).toBeTruthy(); |
| 85 | +}); |
| 86 | + |
| 87 | +test("Renders error message if xhr aborts", async () => { |
| 88 | + global.xhrListener = jest.fn(); |
| 89 | + render(<BasicTestUpload method="POST" url="github.gov" />); |
| 90 | + |
| 91 | + //upload a file into the input |
| 92 | + const file = new File(["hello"], "hello.png", { type: "image/png" }); |
| 93 | + userEvent.upload(screen.getByLabelText("upload"), file); |
| 94 | + //wait for the next tick |
| 95 | + await act(() => new Promise((resolve) => setTimeout(resolve, 0))); |
| 96 | + |
| 97 | + expect(global.xhrListener.mock.calls[3][0]).toEqual("abort"); |
| 98 | + expect(screen.queryByText("bad abort!")).toEqual(null); |
| 99 | + |
| 100 | + //call the load method on the xhr mock |
| 101 | + act(() => global.xhrListener.mock.calls[3][1]("bad abort!")); |
| 102 | + |
| 103 | + expect(screen.getByText("bad abort!")).toBeTruthy(); |
| 104 | +}); |
| 105 | + |
| 106 | +test("Skips upload if options return null", async () => { |
| 107 | + let skipOptions = jest.fn(); |
| 108 | + render( |
| 109 | + <BasicTestUpload |
| 110 | + method="POST" |
| 111 | + url="github.gov" |
| 112 | + skipOptionsCb={skipOptions} |
| 113 | + /> |
| 114 | + ); |
| 115 | + |
| 116 | + //upload a file into the input |
| 117 | + const file = new File(["hello"], "hello.png", { type: "image/png" }); |
| 118 | + userEvent.upload(screen.getByLabelText("upload"), file); |
| 119 | + //wait for the next tick |
| 120 | + await act(() => new Promise((resolve) => setTimeout(resolve, 0))); |
| 121 | + |
| 122 | + expect(skipOptions.mock.calls.length).toEqual(1); |
| 123 | +}); |
0 commit comments