-
Notifications
You must be signed in to change notification settings - Fork 77
[WC-3363] Fileuploader: dismiss action for validation errors #2198
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yordan-st
wants to merge
5
commits into
main
Choose a base branch
from
fix/WC-3363_fileuploader-close-warning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1c63e05
chore: update changelog
yordan-st 2384658
fix: improve file dismissal handling and update changelog
yordan-st ced995d
test: add unit tests for FileStore and FileUploaderStore dismiss func…
yordan-st 50cd545
fix: enhance file dismissal logic and update error message handling
yordan-st b0c0dc3
refactor: make dismissValidationErrors private and remove related tests
yordan-st File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...es/pluggableWidgets/file-uploader-web/src/components/__tests__/DismissActionsBar.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import "@testing-library/jest-dom"; | ||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||
| import { FileUploaderContainerProps } from "../../../typings/FileUploaderProps"; | ||
| import { FileStore } from "../../stores/FileStore"; | ||
| import { TranslationsStoreProvider } from "../../utils/useTranslationsStore"; | ||
| import { ActionsBar } from "../ActionsBar"; | ||
|
|
||
| jest.mock("../../utils/mx-data", () => ({ | ||
| fetchDocumentUrl: jest.fn(), | ||
| fetchImageThumbnail: jest.fn(), | ||
| fetchMxObject: jest.fn(), | ||
| removeObject: jest.fn(), | ||
| saveFile: jest.fn(), | ||
| fileHasContents: jest.fn() | ||
| })); | ||
|
|
||
| function makeFakeProps(): FileUploaderContainerProps { | ||
| const dv = (v: string): { value: string; status: string } => ({ value: v, status: "available" }); | ||
| return { | ||
| name: "fileUploader1", | ||
| uploadMode: "files", | ||
| maxFileSize: 10, | ||
| maxFilesPerUpload: { value: { toNumber: () => 5 } }, | ||
| readOnlyMode: false, | ||
| objectCreationTimeout: 30, | ||
| allowedFileFormats: "", | ||
| removeButtonTextMessage: dv("Remove"), | ||
| downloadButtonTextMessage: dv("Download"), | ||
| unavailableCreateActionMessage: dv("Unavailable"), | ||
| uploadFailureTooManyFilesMessage: dv("Too many"), | ||
| uploadFailureInvalidFileFormatMessage: dv("Invalid format"), | ||
| uploadFailureFileIsTooBigMessage: dv("Too big") | ||
| } as unknown as FileUploaderContainerProps; | ||
| } | ||
|
|
||
| function makeValidationErrorStore(): { store: FileStore; dismiss: jest.Mock } { | ||
| const dismiss = jest.fn(); | ||
| const rootStore = { dismissFile: dismiss, _uploadMode: "files", isReadOnly: false } as any; | ||
| const store = FileStore.newFileWithError(new File([], "bad.txt"), "bad format", rootStore); | ||
| return { store, dismiss }; | ||
| } | ||
|
|
||
| function renderWithTranslations(store: FileStore): void { | ||
| const props = makeFakeProps(); | ||
| render( | ||
| <TranslationsStoreProvider props={props}> | ||
| <ActionsBar store={store} /> | ||
| </TranslationsStoreProvider> | ||
| ); | ||
| } | ||
|
|
||
| describe("DismissActionsBar", () => { | ||
| it("renders dismiss button for validationError file", () => { | ||
| const { store } = makeValidationErrorStore(); | ||
| renderWithTranslations(store); | ||
| expect(screen.getByRole("button", { name: "Remove" })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("calls store.dismiss() when button clicked", () => { | ||
| const { store } = makeValidationErrorStore(); | ||
| const dismissSpy = jest.spyOn(store, "dismiss"); | ||
| renderWithTranslations(store); | ||
| fireEvent.click(screen.getByRole("button")); | ||
| expect(dismissSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("does not render DismissActionsBar for non-validationError file", () => { | ||
| const rootStore = { dismissFile: jest.fn(), _uploadMode: "files", isReadOnly: false } as any; | ||
| const store = FileStore.newFile(new File([], "ok.txt"), rootStore); | ||
| (store as any).fileStatus = "done"; | ||
| renderWithTranslations(store); | ||
| // DefaultActionsBar renders 2 buttons (download + remove) | ||
| expect(screen.queryAllByRole("button")).toHaveLength(2); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/pluggableWidgets/file-uploader-web/src/stores/__tests__/FileStore.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { FileStore } from "../FileStore"; | ||
| import { FileUploaderStore } from "../FileUploaderStore"; | ||
|
|
||
| jest.mock("../../utils/mx-data", () => ({ | ||
| fetchDocumentUrl: jest.fn(), | ||
| fetchImageThumbnail: jest.fn(), | ||
| fetchMxObject: jest.fn(), | ||
| removeObject: jest.fn(), | ||
| saveFile: jest.fn(), | ||
| fileHasContents: jest.fn() | ||
| })); | ||
|
|
||
| function makeRootStore(): { dismissFile: jest.Mock } { | ||
| return { | ||
| dismissFile: jest.fn(), | ||
| _uploadMode: "files", | ||
| isReadOnly: false | ||
| } as unknown as FileUploaderStore & { dismissFile: jest.Mock }; | ||
| } | ||
|
|
||
| describe("FileStore.dismiss()", () => { | ||
| it("calls dismissFile on root store with itself", () => { | ||
| const rootStore = makeRootStore(); | ||
| const store = FileStore.newFileWithError(new File([], "test.txt"), "bad format", rootStore as any); | ||
| store.dismiss(); | ||
| expect(rootStore.dismissFile).toHaveBeenCalledTimes(1); | ||
| expect(rootStore.dismissFile).toHaveBeenCalledWith(store); | ||
| }); | ||
| }); |
121 changes: 121 additions & 0 deletions
121
packages/pluggableWidgets/file-uploader-web/src/stores/__tests__/FileUploaderStore.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { FileStore } from "../FileStore"; | ||
| import { FileUploaderStore } from "../FileUploaderStore"; | ||
| import { TranslationsStore } from "../TranslationsStore"; | ||
|
|
||
| jest.mock("../../utils/mx-data", () => ({ | ||
| fetchDocumentUrl: jest.fn(), | ||
| fetchImageThumbnail: jest.fn(), | ||
| fetchMxObject: jest.fn(), | ||
| removeObject: jest.fn(), | ||
| saveFile: jest.fn(), | ||
| fileHasContents: jest.fn() | ||
| })); | ||
|
|
||
| function makeStore(): FileUploaderStore { | ||
| const translations = { get: jest.fn(() => "msg"), updateProps: jest.fn() } as unknown as TranslationsStore; | ||
| const store = Object.create(FileUploaderStore.prototype) as FileUploaderStore; | ||
| store.files = []; | ||
| return Object.assign(store, { | ||
| translations, | ||
| objectCreationHelper: { canCreateFiles: true, enable: jest.fn(), updateProps: jest.fn(), request: jest.fn() }, | ||
| updateProcessor: { processUpdate: jest.fn() }, | ||
| isReadOnly: false, | ||
| _uploadMode: "files" as const, | ||
| _maxFileSizeMiB: 10, | ||
| _maxFileSize: 10 * 1024 * 1024, | ||
| acceptedFileTypes: [], | ||
| existingItemsLoaded: false | ||
| }); | ||
| } | ||
|
|
||
| function makeValidationErrorFile(store: FileUploaderStore): FileStore { | ||
| return FileStore.newFileWithError(new File([], "bad.txt"), "bad format", store); | ||
| } | ||
|
|
||
| function makeDoneFile(store: FileUploaderStore): FileStore { | ||
| const f = FileStore.newFile(new File([], "good.txt"), store); | ||
| (f as any).fileStatus = "done"; | ||
| return f; | ||
| } | ||
|
|
||
| describe("FileUploaderStore.dismissFile()", () => { | ||
| it("removes the specific file from the list", () => { | ||
| const store = makeStore(); | ||
| const fileA = makeValidationErrorFile(store); | ||
| const fileB = makeValidationErrorFile(store); | ||
| store.files = [fileA, fileB]; | ||
|
|
||
| store.dismissFile(fileA); | ||
|
|
||
| expect(store.files).toHaveLength(1); | ||
| expect(store.files[0]).toBe(fileB); | ||
| }); | ||
|
|
||
| it("does not remove other files", () => { | ||
| const store = makeStore(); | ||
| const fileA = makeValidationErrorFile(store); | ||
| const fileB = makeDoneFile(store); | ||
| store.files = [fileA, fileB]; | ||
|
|
||
| store.dismissFile(fileA); | ||
|
|
||
| expect(store.files[0]).toBe(fileB); | ||
| }); | ||
|
|
||
| it("is a no-op when file is not in the list", () => { | ||
| const store = makeStore(); | ||
| store.errorMessage = "Some files may not be uploadable."; | ||
| const fileA = makeValidationErrorFile(store); | ||
| const fileB = makeValidationErrorFile(store); | ||
| store.files = [fileA]; | ||
|
|
||
| store.dismissFile(fileB); | ||
|
|
||
| expect(store.files).toHaveLength(1); | ||
| expect(store.files[0]).toBe(fileA); | ||
| expect(store.errorMessage).toBe("Some files may not be uploadable."); | ||
| }); | ||
|
|
||
| it("clears errorMessage when last validationError file is dismissed", () => { | ||
| const store = makeStore(); | ||
| store.errorMessage = "Some files may not be uploadable."; | ||
| const file = makeValidationErrorFile(store); | ||
| store.files = [file]; | ||
|
|
||
| store.dismissFile(file); | ||
|
|
||
| expect(store.errorMessage).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("keeps errorMessage while other validationError files remain", () => { | ||
| const store = makeStore(); | ||
| store.errorMessage = "Some files may not be uploadable."; | ||
| const fileA = makeValidationErrorFile(store); | ||
| const fileB = makeValidationErrorFile(store); | ||
| store.files = [fileA, fileB]; | ||
|
|
||
| store.dismissFile(fileA); | ||
|
|
||
| expect(store.errorMessage).toBe("Some files may not be uploadable."); | ||
| }); | ||
| }); | ||
|
|
||
| describe("FileUploaderStore.processDrop() errorMessage", () => { | ||
| it("sets errorMessage when file rejections exist", () => { | ||
| const store = makeStore(); | ||
| const rejection = { file: new File([], "bad.xlsx"), errors: [{ code: "file-invalid-type", message: "bad" }] }; | ||
|
|
||
| store.processDrop([], [rejection]); | ||
|
|
||
| expect(store.errorMessage).toBe("msg"); | ||
| }); | ||
|
|
||
| it("clears errorMessage when no rejections", () => { | ||
| const store = makeStore(); | ||
| store.errorMessage = "Some files may not be uploadable."; | ||
|
|
||
| store.processDrop([], []); | ||
|
|
||
| expect(store.errorMessage).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is ugly,
dismissFileshouldn't do it. This should be a calculated field. Next PR kills it fortunately.