From 53599a476f2f5fafa5d6945b5286d0cf3f01ff01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B8egh?= Date: Tue, 1 Oct 2024 20:44:24 +0200 Subject: [PATCH] Add tests that had a conflict --- .../Iterate/Array/__tests__/Array.test.tsx | 141 ++++++++++++++++++ .../__tests__/WizardContainer.test.tsx | 57 +++++++ 2 files changed, 198 insertions(+) diff --git a/packages/dnb-eufemia/src/extensions/forms/Iterate/Array/__tests__/Array.test.tsx b/packages/dnb-eufemia/src/extensions/forms/Iterate/Array/__tests__/Array.test.tsx index a7eaabd11bb..433d86a1fe1 100644 --- a/packages/dnb-eufemia/src/extensions/forms/Iterate/Array/__tests__/Array.test.tsx +++ b/packages/dnb-eufemia/src/extensions/forms/Iterate/Array/__tests__/Array.test.tsx @@ -640,6 +640,61 @@ describe('Iterate.Array', () => { ) }) + it('should handle "defaultValue" (with null) in React.StrictMode', () => { + const onSubmit = jest.fn() + + render( + + + + + + + + ) + + const form = document.querySelector('form') + const input = document.querySelector('input') + + expect(input).toHaveValue('foo') + + fireEvent.submit(form) + + expect(onSubmit).toHaveBeenCalledTimes(1) + expect(onSubmit).toHaveBeenLastCalledWith( + { myList: ['foo'] }, + expect.anything() + ) + }) + + it('should not set defaultValue when item gets removed', () => { + const onSubmit = jest.fn() + + render( + + + + + + + + + ) + + const form = document.querySelector('form') + const input = document.querySelector('input') + + expect(input).toHaveValue('foo') + + fireEvent.submit(form) + + expect(onSubmit).toHaveBeenCalledTimes(1) + expect(onSubmit).toHaveBeenLastCalledWith( + { myList: ['foo'] }, + expect.anything() + ) + }) + it('should set empty array in the data context', () => { const onSubmit = jest.fn() @@ -1320,6 +1375,92 @@ describe('Iterate.Array', () => { }) }) + describe('value and defaultValue', () => { + it('should support "value" on fields inside iterate', () => { + const onSubmit = jest.fn() + + render( + + + {(value, index) => { + return ( + + ) + }} + + + ) + + const form = document.querySelector('form') + const [first, second, third, forth] = Array.from( + document.querySelectorAll('input') + ) + + expect(first).toHaveValue('value 1') + expect(second).toHaveValue('value 2') + expect(third).toHaveValue('value 3') + expect(forth).toHaveValue('value 4') + + fireEvent.submit(form) + + expect(onSubmit).toHaveBeenCalledTimes(1) + expect(onSubmit).toHaveBeenLastCalledWith( + { + myList: ['value 1', 'value 2', 'value 3', 'value 4'], + }, + expect.anything() + ) + }) + + it('should support "defaultValue" on fields inside iterate', () => { + const onSubmit = jest.fn() + + render( + + + {(value, index) => { + return ( + + ) + }} + + + ) + + const form = document.querySelector('form') + const [first, second, third] = Array.from( + document.querySelectorAll('input') + ) + + expect(first).toHaveValue('default value 1') + expect(second).toHaveValue('default value 2') + expect(third).toHaveValue('something') + + fireEvent.submit(form) + + expect(onSubmit).toHaveBeenCalledTimes(1) + expect(onSubmit).toHaveBeenLastCalledWith( + { + myList: ['default value 1', 'default value 2', 'something'], + }, + expect.anything() + ) + }) + }) + it('should contain tabindex of -1', () => { render(content) diff --git a/packages/dnb-eufemia/src/extensions/forms/Wizard/Container/__tests__/WizardContainer.test.tsx b/packages/dnb-eufemia/src/extensions/forms/Wizard/Container/__tests__/WizardContainer.test.tsx index 86ca77233ae..76d1e772fe9 100644 --- a/packages/dnb-eufemia/src/extensions/forms/Wizard/Container/__tests__/WizardContainer.test.tsx +++ b/packages/dnb-eufemia/src/extensions/forms/Wizard/Container/__tests__/WizardContainer.test.tsx @@ -2061,5 +2061,62 @@ describe('Wizard.Container', () => { ) expect(document.querySelector('input')).toHaveValue('1234') }) + + it('should set defaultValue of Iterate.Array only once between step changes', async () => { + const onChange = jest.fn() + const onStepChange = jest.fn() + + render( + + + + + + + + + + + + + + + + + + ) + + expect(document.querySelectorAll('input')).toHaveLength(1) + expect(document.querySelector('input')).toHaveValue('123') + + await userEvent.type(document.querySelector('input'), '4') + + expect(document.querySelector('input')).toHaveValue('1234') + + const pushButton = document.querySelector( + '.dnb-forms-iterate-push-button' + ) + await userEvent.click(pushButton) + + expect(document.querySelectorAll('input')).toHaveLength(2) + + await userEvent.click(nextButton()) + + expect(onStepChange).toHaveBeenLastCalledWith( + 1, + 'next', + expect.anything() + ) + + await userEvent.click(previousButton()) + + expect(document.querySelectorAll('input')).toHaveLength(2) + expect(onStepChange).toHaveBeenLastCalledWith( + 0, + 'previous', + expect.anything() + ) + expect(document.querySelector('input')).toHaveValue('1234') + }) }) })