-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupload.test.tsx
111 lines (88 loc) · 3.29 KB
/
upload.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { Upload } from "./upload";
import { screen, fireEvent, waitFor } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { renderWithQueryClient } from "@/utils/test-helpers/renderForm";
const defaultProps = {
dataTestId: "upload-component",
files: [],
setFiles: vi.fn(),
setErrorMessage: vi.fn(),
};
describe("Upload", () => {
const testIdSuffix = "upload";
beforeEach(() => {
vi.clearAllMocks();
});
it("renders correctly with initial props", () => {
renderWithQueryClient(<Upload {...defaultProps} />);
expect(screen.getByTestId(`${defaultProps.dataTestId}-${testIdSuffix}`)).toBeInTheDocument();
expect(screen.queryByText("Uploading...")).not.toBeInTheDocument();
});
it("uploads files correctly", async () => {
renderWithQueryClient(<Upload {...defaultProps} />);
const dropzone = screen.getByRole("presentation");
const file = new File(["file contents"], "file.pdf", { type: "application/pdf" });
Object.defineProperty(dropzone, "files", {
value: [file],
writable: false,
});
fireEvent.drop(dropzone);
await waitFor(() => {
expect(defaultProps.setFiles).toHaveBeenCalledWith([
{
bucket: "hello",
key: "world",
filename: "file.pdf",
title: "file",
uploadDate: expect.any(Number), // Since it's a timestamp
},
]);
});
});
it("displays an error for unsupported file types", async () => {
renderWithQueryClient(<Upload {...defaultProps} />);
const dropzone = screen.getByRole("presentation");
const file = new File(["file contents"], "file.exe", { type: "application/x-msdownload" });
Object.defineProperty(dropzone, "files", {
value: [file],
writable: false,
});
fireEvent.drop(dropzone);
await waitFor(() => {
expect(
screen.getByText("Selected file(s) is too large or of a disallowed file type."),
).toBeInTheDocument();
});
});
it("does not display the dropzone when uploading", async () => {
renderWithQueryClient(<Upload {...defaultProps} />);
const dropzone = screen.getByTestId("upload-component-upload");
const file = new File(["file contents"], "file.pdf", { type: "application/pdf" });
Object.defineProperty(dropzone, "files", {
value: [file],
writable: false,
});
fireEvent.drop(dropzone);
await waitFor(() => {
expect(screen.getByTestId("upload-component-upload")).not.toBeVisible();
});
});
it("handles file removal on event", () => {
const mockSetFiles = vi.fn();
const files = [
{ filename: "file-1.txt" },
{ filename: "file-to-remove.txt" },
{ filename: "file-2.txt" },
];
// Render the component with necessary props
renderWithQueryClient(<Upload {...defaultProps} files={files} setFiles={mockSetFiles} />);
// Simulate the event (e.g., a click on the remove button)
const removeButton = screen.getByTestId("upload-component-remove-file-file-to-remove.txt"); // Ensure your component uses this testId
fireEvent.click(removeButton);
// Assert that setFiles was called with the updated files array
expect(mockSetFiles).toHaveBeenCalledWith([
{ filename: "file-1.txt" },
{ filename: "file-2.txt" },
]);
});
});