-
Notifications
You must be signed in to change notification settings - Fork 10
[CCUBE-2200][RYN] Migrate file-upload component #674
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a40d72f
[CCUBE-2200][RYN] add e2e tests and snapshots
fca0de9
[CCUBE-2200][RYN] add warning and custom error e2e tests
2cacfc1
[CCUBE-2200][RYN] update e2e test
e96bd46
[CCUBE-2200][RYN] update e2e tests
353c319
[CCUBE-2200][RYN] FileUpload: update e2e tests on DS bump
d01e969
[CCUBE-2200][RYN] FileUpload: fix pipeline errors
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
54 changes: 54 additions & 0 deletions
54
e2e/nextjs-app/app/components/fields/file-upload/custom-error.e2e.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,54 @@ | ||
| "use client"; | ||
|
|
||
| import { FrontendEngine, IFrontendEngineData, IFrontendEngineRef } from "@lifesg/web-frontend-engine"; | ||
| import { useRef } from "react"; | ||
|
|
||
| const FIELD_ID = "field"; | ||
|
|
||
| const SCHEMA: IFrontendEngineData = { | ||
| sections: { | ||
| section: { | ||
| uiType: "section", | ||
| children: { | ||
| [FIELD_ID]: { | ||
| uiType: "file-upload", | ||
| label: "Upload documents", | ||
| "data-testid": "file-upload", | ||
| uploadOnAddingFile: { | ||
| type: "base64", | ||
| url: "/api/upload", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default function FileUploadCustomErrorPage() { | ||
| const formRef = useRef<IFrontendEngineRef | null>(null); | ||
|
|
||
| const handleSetCustomErrors = () => { | ||
| const values = formRef.current?.getValues(); | ||
| const files = (values?.[FIELD_ID] ?? []) as { fileId: string }[]; | ||
|
|
||
| formRef.current?.setErrors({ | ||
| [FIELD_ID]: { | ||
| message: "Custom error message <strong>with bold text</strong>", | ||
| ...(files.length > 0 && { | ||
| fileErrors: Object.fromEntries( | ||
| files.map((file, index) => [file.fileId, `Custom error message per file (${index})`]) | ||
| ), | ||
| }), | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <FrontendEngine ref={formRef} data={SCHEMA} /> | ||
| <button data-testid="set-custom-errors" onClick={handleSetCustomErrors} style={{ marginTop: "2rem" }}> | ||
| Set custom errors | ||
| </button> | ||
| </> | ||
| ); | ||
| } |
26 changes: 26 additions & 0 deletions
26
e2e/nextjs-app/app/components/fields/file-upload/form-states.e2e.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,26 @@ | ||
| "use client"; | ||
|
|
||
| import { FrontendEngine, IFrontendEngineData } from "@lifesg/web-frontend-engine"; | ||
|
|
||
| const SCHEMA: IFrontendEngineData = { | ||
| sections: { | ||
| section: { | ||
| uiType: "section", | ||
| children: { | ||
| "default-field": { | ||
| uiType: "file-upload", | ||
| label: "Default", | ||
| "data-testid": "file-upload-default", | ||
| uploadOnAddingFile: { | ||
| type: "base64", | ||
| url: "/api/upload", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default function FileUploadFormStatesPage() { | ||
| return <FrontendEngine data={SCHEMA} />; | ||
| } |
38 changes: 38 additions & 0 deletions
38
e2e/nextjs-app/app/components/fields/file-upload/thumbnail.e2e.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,38 @@ | ||
| "use client"; | ||
|
|
||
| import { FrontendEngine, IFrontendEngineData } from "@lifesg/web-frontend-engine"; | ||
|
|
||
| const SCHEMA: IFrontendEngineData = { | ||
| sections: { | ||
| section: { | ||
| uiType: "section", | ||
| children: { | ||
| field: { | ||
| uiType: "file-upload", | ||
| label: "Upload documents", | ||
| "data-testid": "file-upload", | ||
| uploadOnAddingFile: { | ||
| type: "base64", | ||
| url: "/api/upload", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| defaultValues: { | ||
| field: [ | ||
| { | ||
| fileId: "pdf-1", | ||
| fileName: "document.pdf", | ||
| uploadResponse: { | ||
| fileSize: 51200, | ||
| mimeType: "application/pdf", | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| export default function FileUploadThumbnailPage() { | ||
| return <FrontendEngine data={SCHEMA} />; | ||
| } |
26 changes: 26 additions & 0 deletions
26
e2e/nextjs-app/app/components/fields/file-upload/upload-interactions.e2e.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,26 @@ | ||
| "use client"; | ||
|
|
||
| import { FrontendEngine, IFrontendEngineData } from "@lifesg/web-frontend-engine"; | ||
|
|
||
| const SCHEMA: IFrontendEngineData = { | ||
| sections: { | ||
| section: { | ||
| uiType: "section", | ||
| children: { | ||
| field: { | ||
| uiType: "file-upload", | ||
| label: "Upload documents", | ||
| "data-testid": "file-upload", | ||
| uploadOnAddingFile: { | ||
| type: "base64", | ||
| url: "/api/upload", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default function FileUploadInteractionsPage() { | ||
| return <FrontendEngine data={SCHEMA} />; | ||
| } |
25 changes: 25 additions & 0 deletions
25
e2e/nextjs-app/app/components/fields/file-upload/warning.e2e.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,25 @@ | ||
| "use client"; | ||
|
|
||
| import { IFrontendEngineData } from "@lifesg/web-frontend-engine"; | ||
| import { createWarningPage } from "../../common"; | ||
|
|
||
| const SCHEMA: IFrontendEngineData = { | ||
| sections: { | ||
| section: { | ||
| uiType: "section", | ||
| children: { | ||
| primary: { | ||
| uiType: "file-upload", | ||
| label: "Upload documents", | ||
| "data-testid": "file-upload", | ||
| uploadOnAddingFile: { | ||
| type: "base64", | ||
| url: "/api/upload", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default createWarningPage({ schema: SCHEMA }); |
Binary file added
BIN
+21.8 KB
...load-Custom-errors-Main-field-and-per-file-errors--main-and-per-file-errors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.1 KB
...hots__/chromium/FileUpload-Custom-errors-Main-field-error--main-field-error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.42 KB
...s/file-upload/__screenshots__/chromium/FileUpload-Form-states-Mobile--mount.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.53 KB
...s/file-upload/__screenshots__/chromium/FileUpload-Form-states-Visual--mount.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.5 KB
...nts/fields/file-upload/__screenshots__/chromium/FileUpload-Thumbnail--mount.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+13.1 KB
...eenshots__/chromium/FileUpload-Upload-interactions--uploaded-through-button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+11.8 KB
...nents/fields/file-upload/__screenshots__/chromium/FileUpload-Warning--mount.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 121 additions & 0 deletions
121
e2e/tests/components/fields/file-upload/file-upload.e2e.spec.ts
|
qroll marked this conversation as resolved.
|
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 { Page } from "@playwright/test"; | ||
| import { createStoryTest, expect, test } from "../../../utils/fixtures"; | ||
| import { createSolidColorPng } from "../../../utils/image-fixtures"; | ||
|
|
||
| // ============================================================================= | ||
| // HELPERS | ||
| // ============================================================================= | ||
| const SAMPLE_PNG_PAYLOAD = { | ||
| name: "sample.png", | ||
| mimeType: "image/png", | ||
| buffer: createSolidColorPng(8, 8, 220, 220, 220), | ||
| }; | ||
|
|
||
| const mockUploadAPI = (page: Page) => | ||
| page.route("**/api/upload", (route) => | ||
| route.fulfill({ | ||
| status: 200, | ||
| contentType: "application/json", | ||
| body: JSON.stringify({ success: true }), | ||
| }) | ||
| ); | ||
|
|
||
| // ============================================================================= | ||
| // TEST FACTORIES | ||
| // ============================================================================= | ||
| const createFileUploadTest = (story: string) => | ||
| createStoryTest({ | ||
| component: "fields/file-upload", | ||
| story, | ||
| createLocators: (page) => ({ | ||
| dropzone: page.getByTestId("file-upload"), | ||
| uploadButton: page.getByRole("button", { name: "Upload files" }), | ||
| fileItem: (name: string) => page.getByText(name, { exact: true }), | ||
| setCustomErrorsButton: page.getByTestId("set-custom-errors"), | ||
| }), | ||
| }); | ||
|
|
||
| const uploadInteractionsTest = createFileUploadTest("upload-interactions"); | ||
| const formStatesTest = createFileUploadTest("form-states"); | ||
| const thumbnailTest = createFileUploadTest("thumbnail"); | ||
| const warningTest = createFileUploadTest("warning"); | ||
| const customErrorTest = createFileUploadTest("custom-error"); | ||
|
|
||
| // ============================================================================= | ||
| // TESTS | ||
| // ============================================================================= | ||
| test.describe("FileUpload", () => { | ||
| uploadInteractionsTest.describe(() => { | ||
| uploadInteractionsTest("Upload interactions", async ({ story }) => { | ||
| await mockUploadAPI(story.page); | ||
| await story.goto(); | ||
|
|
||
| await test.step("Upload through button", async () => { | ||
| const fileChooserPromise = story.page.waitForEvent("filechooser"); | ||
| await story.locators.uploadButton.click(); | ||
| const fileChooser = await fileChooserPromise; | ||
| await fileChooser.setFiles(SAMPLE_PNG_PAYLOAD); | ||
|
|
||
| await expect(story.locators.fileItem(SAMPLE_PNG_PAYLOAD.name)).toBeVisible(); | ||
| await story.snapshot("uploaded-through-button"); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| test.describe("Form states", () => { | ||
| formStatesTest.describe(() => { | ||
| formStatesTest("Visual", async ({ story }) => { | ||
| await story.goto(); | ||
| await story.snapshot("mount"); | ||
| }); | ||
| }); | ||
|
|
||
| formStatesTest.describe(() => { | ||
| formStatesTest("Mobile", async ({ story }) => { | ||
| await story.setViewport({ size: "mobile" }); | ||
| await story.goto(); | ||
| await story.snapshot("mount"); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| thumbnailTest.describe(() => { | ||
| thumbnailTest("Thumbnail", async ({ story }) => { | ||
| await story.goto(); | ||
| await expect(story.locators.fileItem("document.pdf")).toBeVisible(); | ||
| await story.waitForImageLoad(); | ||
| await story.snapshot("mount"); | ||
| }); | ||
| }); | ||
|
|
||
| warningTest.describe(() => { | ||
| warningTest("Warning", async ({ story }) => { | ||
| await story.goto(); | ||
| await story.snapshot("mount"); | ||
| }); | ||
| }); | ||
|
|
||
| test.describe("Custom errors", () => { | ||
| customErrorTest.describe(() => { | ||
| customErrorTest("Main field error", async ({ story }) => { | ||
| await story.goto(); | ||
| await story.locators.setCustomErrorsButton.click(); | ||
| await story.snapshot("main-field-error"); | ||
| }); | ||
|
|
||
| customErrorTest("Main field and per-file errors", async ({ story }) => { | ||
| await mockUploadAPI(story.page); | ||
| await story.goto(); | ||
|
|
||
| const fileChooserPromise = story.page.waitForEvent("filechooser"); | ||
| await story.locators.uploadButton.click(); | ||
| const fileChooser = await fileChooserPromise; | ||
| await fileChooser.setFiles(SAMPLE_PNG_PAYLOAD); | ||
| await expect(story.locators.fileItem(SAMPLE_PNG_PAYLOAD.name)).toBeVisible(); | ||
|
|
||
| await story.locators.setCustomErrorsButton.click(); | ||
| await story.snapshot("main-and-per-file-errors"); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.