Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(site): show error on template upload failure#15558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ethanndickson merged 1 commit intomainfromethan/template-upload-fail
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Copy link
Collaborator

@BrunoQuaresmaBrunoQuaresmaNov 22, 2024
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Nice test 👍

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
import { screen, waitFor, within } from "@testing-library/react";
import {fireEvent,screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { API } from "api/api";
import {
Expand All@@ -8,6 +8,7 @@ import {
MockTemplateVersionVariable1,
MockTemplateVersionVariable2,
MockTemplateVersionVariable3,
mockApiError,
} from "testHelpers/entities";
import { renderWithAuth } from "testHelpers/renderHelpers";
import CreateTemplatePage from "./CreateTemplatePage";
Expand DownExpand Up@@ -150,3 +151,30 @@ test("Create template from duplicating a template", async () => {
);
});
});

test("The page displays an error if the upload fails", async () => {
const errMsg = "Unsupported content type header";
jest.spyOn(API, "uploadFile").mockRejectedValueOnce(
mockApiError({
message: errMsg,
}),
);
await renderPage(new URLSearchParams());

const file = new File([""], "invalidfile.tar");
const dropZone = screen.getByTestId("drop-zone");

fireEvent.drop(dropZone, {
dataTransfer: { files: [file] },
});

await waitFor(() => expect(API.uploadFile).toHaveBeenCalledTimes(1));

// Error toast should be displayed
expect(await screen.findByText(errMsg)).toBeInTheDocument();

// The upload box should have reset itself
expect(
await screen.findByText(/The template has to be a .tar or .zip file/),
).toBeInTheDocument();
});
11 changes: 10 additions & 1 deletionsite/src/pages/CreateTemplatePage/UploadTemplateView.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
import { getErrorMessage } from "api/errors";
import { uploadFile } from "api/queries/files";
import {
JobError,
templateVersionLogs,
templateVersionVariables,
} from "api/queries/templates";
import { displayError } from "components/GlobalSnackbar/utils";
import { useDashboard } from "modules/dashboard/useDashboard";
import { useFeatureVisibility } from "modules/dashboard/useFeatureVisibility";
import type { FC } from "react";
Expand DownExpand Up@@ -51,7 +53,14 @@ export const UploadTemplateView: FC<CreateTemplatePageViewProps> = ({
jobError={isJobError ? error.job.error : undefined}
logs={templateVersionLogsQuery.data}
upload={{
onUpload: uploadFileMutation.mutateAsync,
onUpload: async (file: File) => {
try {
await uploadFileMutation.mutateAsync(file);
} catch (error) {
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm not familiar with this query library, but what I found is that even though you can setonError, an exception returned frommutateAsync isn't caught unless you explicitly catch it. So, If I displayed the error as part ofonError inuseMutation, I'd still need to wrapmutateAsync in a try block, with an empty catch, which seems pointless.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You did it correctly. We usually usemutateAsync to keep the error handling close to where the mutation is called.

displayError(getErrorMessage(error, "Failed to upload file"));
uploadFileMutation.reset();
}
},
isUploading: uploadFileMutation.isLoading,
onRemove: uploadFileMutation.reset,
file: uploadFileMutation.variables,
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp