Skip to content

Commit a35c01d

Browse files
fix(site): show error on template upload failure (#15558)
Closes #15441.
1 parent b5fbfd7 commit a35c01d

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

site/src/pages/CreateTemplatePage/CreateTemplatePage.test.tsx

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { screen, waitFor, within } from "@testing-library/react";
1+
import { fireEvent, screen, waitFor, within } from "@testing-library/react";
22
import userEvent from "@testing-library/user-event";
33
import { API } from "api/api";
44
import {
@@ -8,6 +8,7 @@ import {
88
MockTemplateVersionVariable1,
99
MockTemplateVersionVariable2,
1010
MockTemplateVersionVariable3,
11+
mockApiError,
1112
} from "testHelpers/entities";
1213
import { renderWithAuth } from "testHelpers/renderHelpers";
1314
import CreateTemplatePage from "./CreateTemplatePage";
@@ -150,3 +151,30 @@ test("Create template from duplicating a template", async () => {
150151
);
151152
});
152153
});
154+
155+
test("The page displays an error if the upload fails", async () => {
156+
const errMsg = "Unsupported content type header";
157+
jest.spyOn(API, "uploadFile").mockRejectedValueOnce(
158+
mockApiError({
159+
message: errMsg,
160+
}),
161+
);
162+
await renderPage(new URLSearchParams());
163+
164+
const file = new File([""], "invalidfile.tar");
165+
const dropZone = screen.getByTestId("drop-zone");
166+
167+
fireEvent.drop(dropZone, {
168+
dataTransfer: { files: [file] },
169+
});
170+
171+
await waitFor(() => expect(API.uploadFile).toHaveBeenCalledTimes(1));
172+
173+
// Error toast should be displayed
174+
expect(await screen.findByText(errMsg)).toBeInTheDocument();
175+
176+
// The upload box should have reset itself
177+
expect(
178+
await screen.findByText(/The template has to be a .tar or .zip file/),
179+
).toBeInTheDocument();
180+
});

site/src/pages/CreateTemplatePage/UploadTemplateView.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { getErrorMessage } from "api/errors";
12
import { uploadFile } from "api/queries/files";
23
import {
34
JobError,
45
templateVersionLogs,
56
templateVersionVariables,
67
} from "api/queries/templates";
8+
import { displayError } from "components/GlobalSnackbar/utils";
79
import { useDashboard } from "modules/dashboard/useDashboard";
810
import { useFeatureVisibility } from "modules/dashboard/useFeatureVisibility";
911
import type { FC } from "react";
@@ -51,7 +53,14 @@ export const UploadTemplateView: FC<CreateTemplatePageViewProps> = ({
5153
jobError={isJobError ? error.job.error : undefined}
5254
logs={templateVersionLogsQuery.data}
5355
upload={{
54-
onUpload: uploadFileMutation.mutateAsync,
56+
onUpload: async (file: File) => {
57+
try {
58+
await uploadFileMutation.mutateAsync(file);
59+
} catch (error) {
60+
displayError(getErrorMessage(error, "Failed to upload file"));
61+
uploadFileMutation.reset();
62+
}
63+
},
5564
isUploading: uploadFileMutation.isLoading,
5665
onRemove: uploadFileMutation.reset,
5766
file: uploadFileMutation.variables,

0 commit comments

Comments
 (0)