Skip to content

Commit

Permalink
Add async onSubmit
Browse files Browse the repository at this point in the history
  • Loading branch information
masiulis committed Dec 5, 2023
1 parent ef15f1c commit a3442f1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/steps/ValidationStep/ValidationStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
const [selectedRows, setSelectedRows] = useState<ReadonlySet<number | string>>(new Set())
const [filterByErrors, setFilterByErrors] = useState(false)
const [showSubmitAlert, setShowSubmitAlert] = useState(false)
const [isSubmitting, setSubmitting] = useState(false)

const updateData = useCallback(
async (rows: typeof data, indexes?: number[]) => {
Expand Down Expand Up @@ -96,9 +97,16 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
},
{ validData: [] as Data<T>[], invalidData: [] as Data<T>[], all: data },
)
onSubmit(calculatedData, file)
setShowSubmitAlert(false)
onClose()
setSubmitting(true)
onSubmit(calculatedData, file)
?.then(() => {
onClose()
})
?.catch(() => {})
?.finally(() => {
setSubmitting(false)
})
}
const onContinue = () => {
const invalidData = data.find((value) => {
Expand Down Expand Up @@ -153,6 +161,7 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
/>
</ModalBody>
<ContinueButton
isLoading={isSubmitting}
onContinue={onContinue}
onBack={onBack}
title={translations.validationStep.nextButtonTitle}
Expand Down
62 changes: 62 additions & 0 deletions src/steps/ValidationStep/tests/ValidationStep.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,68 @@ describe("Validation step tests", () => {
})
})

test("Submit data with a successful async return", async () => {
const onSuccess = jest.fn()
const onSubmit = jest.fn(async (): Promise<void> => {
onSuccess()
return Promise.resolve()
})
const onClose = jest.fn()
render(
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, onSubmit, onClose }}>
<ModalWrapper isOpen={true} onClose={() => {}}>
<ValidationStep initialData={[]} file={file} />
</ModalWrapper>
</Providers>,
)

const finishButton = screen.getByRole("button", {
name: "Confirm",
})

await userEvent.click(finishButton)

await waitFor(() => {
expect(onSubmit).toBeCalledWith({ all: [], invalidData: [], validData: [] }, file)
})
await waitFor(() => {
expect(onSuccess).toBeCalled()
expect(onClose).toBeCalled()
})
})

test("Submit data with a unsuccessful async return", async () => {
const onReject = jest.fn()
const onSubmit = jest.fn(async (): Promise<void> => {
onReject()
return Promise.reject()
})
const onClose = jest.fn()
try {
render(
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, onSubmit, onClose }}>
<ModalWrapper isOpen={true} onClose={() => {}}>
<ValidationStep initialData={[]} file={file} />
</ModalWrapper>
</Providers>,
)

const finishButton = screen.getByRole("button", {
name: "Confirm",
})

await userEvent.click(finishButton)

await waitFor(() => {
expect(onSubmit).toBeCalledWith({ all: [], invalidData: [], validData: [] }, file)
})
} catch (e) {}
await waitFor(() => {
expect(onReject).toBeCalled()
expect(onClose).not.toBeCalled()
})
})

test("Filters rows with required errors", async () => {
const UNIQUE_NAME = "very unique name"
const fields = [
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export type RsiProps<T extends string> = {
rowHook?: RowHook<T>
// Runs after column matching and on entry change
tableHook?: TableHook<T>
// Function called after user finishes the flow
onSubmit: (data: Result<T>, file: File) => void
// Function called after user finishes the flow. You can return a promise that will be awaited.
onSubmit: (data: Result<T>, file: File) => void | Promise<any>
// Allows submitting with errors. Default: true
allowInvalidSubmit?: boolean
// Enable navigation in stepper component and show back button. Default: false
Expand Down

0 comments on commit a3442f1

Please sign in to comment.