From a3442f10997b513b6219ea4856fbc488cd5850e7 Mon Sep 17 00:00:00 2001 From: masiulis Date: Tue, 5 Dec 2023 13:32:45 +0200 Subject: [PATCH] Add async onSubmit --- src/steps/ValidationStep/ValidationStep.tsx | 13 +++- .../tests/ValidationStep.test.tsx | 62 +++++++++++++++++++ src/types.ts | 4 +- 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/steps/ValidationStep/ValidationStep.tsx b/src/steps/ValidationStep/ValidationStep.tsx index b6ae8ed9..9141518b 100644 --- a/src/steps/ValidationStep/ValidationStep.tsx +++ b/src/steps/ValidationStep/ValidationStep.tsx @@ -28,6 +28,7 @@ export const ValidationStep = ({ initialData, file, onBack }: const [selectedRows, setSelectedRows] = useState>(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[]) => { @@ -96,9 +97,16 @@ export const ValidationStep = ({ initialData, file, onBack }: }, { validData: [] as Data[], invalidData: [] as Data[], 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) => { @@ -153,6 +161,7 @@ export const ValidationStep = ({ initialData, file, onBack }: /> { }) }) + test("Submit data with a successful async return", async () => { + const onSuccess = jest.fn() + const onSubmit = jest.fn(async (): Promise => { + onSuccess() + return Promise.resolve() + }) + const onClose = jest.fn() + render( + + {}}> + + + , + ) + + 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 => { + onReject() + return Promise.reject() + }) + const onClose = jest.fn() + try { + render( + + {}}> + + + , + ) + + 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 = [ diff --git a/src/types.ts b/src/types.ts index 9d967107..743dbe09 100644 --- a/src/types.ts +++ b/src/types.ts @@ -21,8 +21,8 @@ export type RsiProps = { rowHook?: RowHook // Runs after column matching and on entry change tableHook?: TableHook - // Function called after user finishes the flow - onSubmit: (data: Result, file: File) => void + // Function called after user finishes the flow. You can return a promise that will be awaited. + onSubmit: (data: Result, file: File) => void | Promise // Allows submitting with errors. Default: true allowInvalidSubmit?: boolean // Enable navigation in stepper component and show back button. Default: false