Skip to content
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

fix(Forms): add support for Form.SubmitConfirmation inside Wizard #4088

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function SubmitConfirmation(props: ConfirmProps) {
})

useMemo(() => {
if (Object.keys(removeUndefinedProps(submitState)).length > 0) {
if (Object.keys(removeUndefinedProps(submitState) || {}).length > 0) {
submitStateRef.current = {
...submitState,
} as EventStateObject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { act, fireEvent, render, waitFor } from '@testing-library/react'
import { Form } from '../../..'
import { Form, Wizard } from '../../..'
import { Button, Dialog } from '../../../../../components'
import { ConfirmParams } from '../SubmitConfirmation'
import userEvent from '@testing-library/user-event'
Expand Down Expand Up @@ -630,6 +630,66 @@ describe('Form.SubmitConfirmation', () => {
})
})

it('should prevent "onSubmit" when used inside a Wizard.Container (with prerender)', async () => {
const onSubmit = jest.fn()
const onStepChange = jest.fn()

render(
<Form.Handler onSubmit={onSubmit}>
<Wizard.Container onStepChange={onStepChange}>
<Wizard.Step title="Step 1">
<Wizard.Buttons />
</Wizard.Step>
<Wizard.Step title="Step 2">
<Form.SubmitConfirmation preventSubmitWhen={() => true} />
<Wizard.Buttons />
</Wizard.Step>
</Wizard.Container>
<Form.SubmitButton />
</Form.Handler>
)

const form = document.querySelector('form')
await act(async () => {
fireEvent.submit(form)
})

expect(onSubmit).toHaveBeenCalledTimes(0)
expect(onStepChange).toHaveBeenCalledTimes(1)
expect(onStepChange).toHaveBeenLastCalledWith(
1,
'next',
expect.anything()
)

await userEvent.click(
document.querySelector('.dnb-forms-previous-button')
)
expect(onStepChange).toHaveBeenCalledTimes(2)
expect(onStepChange).toHaveBeenLastCalledWith(
0,
'previous',
expect.anything()
)

await userEvent.click(
document.querySelector('.dnb-forms-submit-button')
)
expect(onSubmit).toHaveBeenCalledTimes(0)
expect(onStepChange).toHaveBeenCalledTimes(3)
expect(onStepChange).toHaveBeenLastCalledWith(
1,
'next',
expect.anything()
)

await userEvent.click(
document.querySelector('dnb-forms-submit-button')
)
expect(onSubmit).toHaveBeenCalledTimes(0)
expect(onStepChange).toHaveBeenCalledTimes(3)
})

it('should not disable buttons when disabled is set to true', () => {
render(
<Form.Handler disabled>
Expand Down
24 changes: 24 additions & 0 deletions packages/dnb-eufemia/src/shared/__tests__/component-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
matchAll,
convertJsxToString,
escapeRegexChars,
removeUndefinedProps,
} from '../component-helper'

beforeAll(() => {
Expand Down Expand Up @@ -664,3 +665,26 @@ describe('"escapeRegexChars" should', () => {
)
})
})

describe('"removeUndefinedProps" should', () => {
const object = {
foo: undefined,
bar: null,
baz: undefined,
qux: null,
quux: undefined,
}

it('remove undefined props', () => {
expect(removeUndefinedProps(object)).toEqual({
bar: null,
baz: undefined,
qux: null,
quux: undefined,
})
})

it('remove support undefined as data', () => {
expect(removeUndefinedProps(undefined)).toBeUndefined()
})
})
2 changes: 1 addition & 1 deletion packages/dnb-eufemia/src/shared/component-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ export function escapeRegexChars(str) {
}

export function removeUndefinedProps(object) {
Object.keys(object).forEach((key) => {
Object.keys(object || {}).forEach((key) => {
if (object[key] === undefined) {
delete object[key]
}
Expand Down
Loading