-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- [x] Support file upload for import external identity - [x] Add upload button component - [x] Add e2e tests Co-authored-by: 0xmad <[email protected]>
- Loading branch information
Showing
26 changed files
with
485 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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 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
47 changes: 47 additions & 0 deletions
47
packages/app/src/ui/components/UploadButton/UploadButton.tsx
This file contains 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,47 @@ | ||
import Box from "@mui/material/Box"; | ||
import Button from "@mui/material/Button"; | ||
import Typography from "@mui/material/Typography"; | ||
import { forwardRef, Ref, type HTMLAttributes } from "react"; | ||
|
||
import type { Accept } from "react-dropzone"; | ||
|
||
import { type onDropCallback, useUploadButton } from "./useUploadButton"; | ||
|
||
export interface IUploadButtonProps extends Omit<HTMLAttributes<HTMLInputElement>, "onDrop"> { | ||
isLoading?: boolean; | ||
errorMessage?: string; | ||
multiple?: boolean; | ||
accept: Accept; | ||
name: string; | ||
onDrop: onDropCallback; | ||
} | ||
|
||
const UploadButtonUI = ( | ||
{ isLoading = false, multiple = true, errorMessage = "", accept, name, onDrop, ...rest }: IUploadButtonProps, | ||
ref: Ref<HTMLInputElement>, | ||
): JSX.Element => { | ||
const { isDragActive, getRootProps, getInputProps } = useUploadButton({ | ||
isLoading, | ||
accept, | ||
multiple, | ||
onDrop, | ||
}); | ||
|
||
const fileTitle = multiple ? "files" : "file"; | ||
|
||
return ( | ||
<Box {...rest} {...getRootProps({ className: "dropzone" })} sx={{ width: "100%" }}> | ||
<input ref={ref} name={name} {...getInputProps()} /> | ||
|
||
<Button sx={{ width: "100%" }} variant="outlined"> | ||
{isDragActive ? `Drop the ${fileTitle} here...` : `Upload ${fileTitle}`} | ||
</Button> | ||
|
||
<Typography color="error" sx={{ mt: 1, mx: 1, fontSize: "0.8125rem" }} variant="body2"> | ||
{errorMessage} | ||
</Typography> | ||
</Box> | ||
); | ||
}; | ||
|
||
export const UploadButton = forwardRef<HTMLInputElement, IUploadButtonProps>(UploadButtonUI); |
62 changes: 62 additions & 0 deletions
62
packages/app/src/ui/components/UploadButton/__tests__/UploadButton.test.tsx
This file contains 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,62 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { render } from "@testing-library/react"; | ||
|
||
import { IUploadButtonProps, UploadButton } from ".."; | ||
import { IUseUploadButtonData, useUploadButton } from "../useUploadButton"; | ||
|
||
jest.mock("../useUploadButton", (): unknown => ({ | ||
...jest.requireActual("../useUploadButton"), | ||
useUploadButton: jest.fn(), | ||
})); | ||
|
||
describe("ui/components/UploadButton", () => { | ||
const defaultHookData: IUseUploadButtonData = { | ||
isDragActive: false, | ||
acceptedFiles: [], | ||
getInputProps: jest.fn(), | ||
getRootProps: jest.fn(), | ||
}; | ||
|
||
const defaultProps: IUploadButtonProps = { | ||
accept: { "application/json": [".json"] }, | ||
name: "file", | ||
onDrop: jest.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
(useUploadButton as jest.Mock).mockReturnValue(defaultHookData); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should render properly", async () => { | ||
const { findByText } = render(<UploadButton {...defaultProps} />); | ||
|
||
const dragText = await findByText("Upload files"); | ||
|
||
expect(dragText).toBeInTheDocument(); | ||
}); | ||
|
||
test("should render properly while drag is active", async () => { | ||
(useUploadButton as jest.Mock).mockReturnValue({ ...defaultHookData, isDragActive: true }); | ||
|
||
const { findByText } = render(<UploadButton {...defaultProps} />); | ||
|
||
const dragText = await findByText("Drop the files here..."); | ||
|
||
expect(dragText).toBeInTheDocument(); | ||
}); | ||
|
||
test("should render error properly", async () => { | ||
const { findByText } = render(<UploadButton {...defaultProps} errorMessage="error" />); | ||
|
||
const error = await findByText("error"); | ||
|
||
expect(error).toBeInTheDocument(); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
packages/app/src/ui/components/UploadButton/__tests__/useUploadButton.test.tsx
This file contains 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,37 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { act, fireEvent, render, renderHook } from "@testing-library/react"; | ||
|
||
import { createDataTransfer, mockJsonFile } from "@src/config/mock/file"; | ||
|
||
import { IUseUploadButtonArgs, useUploadButton } from "../useUploadButton"; | ||
|
||
describe("ui/components/UploadButton/useUploadButton", () => { | ||
const defaultHookArgs: IUseUploadButtonArgs = { | ||
isLoading: false, | ||
accept: { "application/json": [".json"] }, | ||
onDrop: jest.fn(), | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should upload file properly", async () => { | ||
const data = createDataTransfer([mockJsonFile]); | ||
|
||
const { result } = renderHook(() => useUploadButton(defaultHookArgs)); | ||
|
||
const { container } = render( | ||
<div {...result.current.getRootProps()}> | ||
<input {...result.current.getInputProps()} /> | ||
</div>, | ||
); | ||
|
||
await act(() => fireEvent.drop(container.querySelector("div")!, data)); | ||
|
||
expect(defaultHookArgs.onDrop).toBeCalledTimes(1); | ||
}); | ||
}); |
This file contains 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,2 @@ | ||
export { type IUploadButtonProps, UploadButton } from "./UploadButton"; | ||
export { type onDropCallback, type IUseUploadButtonArgs, type IUseUploadButtonData } from "./useUploadButton"; |
45 changes: 45 additions & 0 deletions
45
packages/app/src/ui/components/UploadButton/useUploadButton.ts
This file contains 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,45 @@ | ||
import { type HTMLAttributes } from "react"; | ||
import { | ||
type DropzoneInputProps, | ||
type DropzoneRootProps, | ||
type FileRejection, | ||
type Accept, | ||
useDropzone, | ||
} from "react-dropzone"; | ||
|
||
export interface IUseUploadButtonArgs { | ||
isLoading: boolean; | ||
accept: Accept; | ||
multiple?: boolean; | ||
onDrop: onDropCallback; | ||
} | ||
|
||
export interface IUseUploadButtonData { | ||
isDragActive: boolean; | ||
acceptedFiles: File[]; | ||
getInputProps: (props?: DropzoneInputProps) => HTMLAttributes<HTMLInputElement>; | ||
getRootProps: (props?: DropzoneRootProps) => HTMLAttributes<HTMLDivElement>; | ||
} | ||
|
||
export type onDropCallback = (acceptedFiles: File[], fileRejections: FileRejection[]) => Promise<void>; | ||
|
||
export const useUploadButton = ({ | ||
isLoading, | ||
accept, | ||
multiple = true, | ||
onDrop, | ||
}: IUseUploadButtonArgs): IUseUploadButtonData => { | ||
const { isDragActive, acceptedFiles, getRootProps, getInputProps } = useDropzone({ | ||
accept, | ||
disabled: isLoading, | ||
multiple, | ||
onDrop, | ||
}); | ||
|
||
return { | ||
isDragActive, | ||
acceptedFiles, | ||
getRootProps, | ||
getInputProps, | ||
}; | ||
}; |
This file contains 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 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 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 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 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.