Skip to content

Commit e8f98f2

Browse files
authored
Merge pull request #236 from UgnisSoftware/UGN-426
Fix modal not closing after submit
2 parents e3c154f + 4d3951f commit e8f98f2

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

src/steps/ValidationStep/ValidationStep.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,28 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
100100
)
101101
setShowSubmitAlert(false)
102102
setSubmitting(true)
103-
onSubmit(calculatedData, file)
104-
?.then(() => {
105-
onClose()
106-
})
107-
.catch((err: Error) => {
108-
toast({
109-
status: "error",
110-
variant: "left-accent",
111-
position: "bottom-left",
112-
title: `${translations.alerts.submitError.title}`,
113-
description: err?.message || `${translations.alerts.submitError.defaultMessage}`,
114-
isClosable: true,
103+
const response = onSubmit(calculatedData, file)
104+
if (response?.then) {
105+
response
106+
.then(() => {
107+
onClose()
115108
})
116-
})
117-
.finally(() => {
118-
setSubmitting(false)
119-
})
109+
.catch((err: Error) => {
110+
toast({
111+
status: "error",
112+
variant: "left-accent",
113+
position: "bottom-left",
114+
title: `${translations.alerts.submitError.title}`,
115+
description: err?.message || `${translations.alerts.submitError.defaultMessage}`,
116+
isClosable: true,
117+
})
118+
})
119+
.finally(() => {
120+
setSubmitting(false)
121+
})
122+
} else {
123+
onClose()
124+
}
120125
}
121126
const onContinue = () => {
122127
const invalidData = data.find((value) => {

src/steps/ValidationStep/tests/ValidationStep.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,35 @@ describe("Validation step tests", () => {
4848
})
4949
})
5050

51+
test("Submit data without returning promise", async () => {
52+
const onSuccess = jest.fn()
53+
const onSubmit = jest.fn(() => {
54+
onSuccess()
55+
})
56+
const onClose = jest.fn()
57+
render(
58+
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, onSubmit, onClose }}>
59+
<ModalWrapper isOpen={true} onClose={() => {}}>
60+
<ValidationStep initialData={[]} file={file} />
61+
</ModalWrapper>
62+
</Providers>,
63+
)
64+
65+
const finishButton = screen.getByRole("button", {
66+
name: "Confirm",
67+
})
68+
69+
await userEvent.click(finishButton)
70+
71+
await waitFor(() => {
72+
expect(onSubmit).toBeCalledWith({ all: [], invalidData: [], validData: [] }, file)
73+
})
74+
await waitFor(() => {
75+
expect(onSuccess).toBeCalled()
76+
expect(onClose).toBeCalled()
77+
})
78+
})
79+
5180
test("Submit data with a successful async return", async () => {
5281
const onSuccess = jest.fn()
5382
const onSubmit = jest.fn(async (): Promise<void> => {

0 commit comments

Comments
 (0)