-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle accountPin correctly and ensure sanitized data
- Loading branch information
Showing
5 changed files
with
152 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { render, screen } from '@testing-library/preact' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
import { portingFactory } from '@/testing/factories/porting' | ||
|
||
import { PortingForm } from '../PortingForm' | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => { | ||
return ( | ||
<div> | ||
{children} | ||
<button form="gigsPortingEmbedForm">Submit</button> | ||
</div> | ||
) | ||
} | ||
|
||
it('can enter and submit', async () => { | ||
const user = userEvent.setup() | ||
const porting = portingFactory.build() | ||
const submit = vi.fn() | ||
render(<PortingForm porting={porting} onSubmit={submit} />, { wrapper }) | ||
|
||
await user.type(screen.getByLabelText('Account Number'), '1234') | ||
await user.type(screen.getByLabelText('Account PIN'), '0000') | ||
await user.type(screen.getByLabelText('Birthday'), '01.01.1990') | ||
await user.type(screen.getByLabelText('First Name'), 'Jerry') | ||
await user.type(screen.getByLabelText('Last Name'), 'Seinfeld') | ||
await user.click(screen.getByRole('button', { name: 'Submit' })) | ||
|
||
expect(submit).toHaveBeenCalledWith({ | ||
accountNumber: '1234', | ||
accountPin: '0000', | ||
birthday: '01.01.1990', | ||
firstName: 'Jerry', | ||
lastName: 'Seinfeld', | ||
}) | ||
}) | ||
|
||
describe('with existing porting fields', () => { | ||
it('prefills the inputs', async () => { | ||
const porting = portingFactory.build({ | ||
accountNumber: '1234', | ||
accountPinExists: true, | ||
birthday: '01.01.1990', | ||
firstName: 'Jerry', | ||
lastName: 'Seinfeld', | ||
}) | ||
const submit = vi.fn() | ||
render(<PortingForm porting={porting} onSubmit={submit} />, { wrapper }) | ||
|
||
expect(screen.getByLabelText('Account Number')).toHaveValue('1234') | ||
expect(screen.getByLabelText('Birthday')).toHaveValue('01.01.1990') | ||
expect(screen.getByLabelText('First Name')).toHaveValue('Jerry') | ||
expect(screen.getByLabelText('Last Name')).toHaveValue('Seinfeld') | ||
|
||
// the account pin cannot be prefilled, and instead indicates it exists with | ||
// a placeholder | ||
expect(screen.getByLabelText('Account PIN')).toHaveValue('') | ||
expect(screen.getByLabelText('Account PIN')).toHaveAttribute( | ||
'placeholder', | ||
'••••', | ||
) | ||
}) | ||
|
||
it('only submits changed fields', async () => { | ||
const user = userEvent.setup() | ||
const porting = portingFactory.build({ | ||
accountNumber: '1234', | ||
accountPinExists: true, | ||
birthday: '01.01.1990', | ||
firstName: 'Jerry', | ||
lastName: 'Seinfeld', | ||
}) | ||
const submit = vi.fn() | ||
render(<PortingForm porting={porting} onSubmit={submit} />, { wrapper }) | ||
|
||
await user.clear(screen.getByLabelText('Account Number')) | ||
await user.type(screen.getByLabelText('Account Number'), '5678') | ||
await user.click(screen.getByRole('button', { name: 'Submit' })) | ||
|
||
expect(submit).toHaveBeenCalledWith({ | ||
accountNumber: '5678', | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { sanitizeSubmitData } from '../sanitizeSubmitData' | ||
|
||
it('removes any null values', () => { | ||
expect(sanitizeSubmitData({ foo: 'bar', bar: null, baz: null })).toEqual({ | ||
foo: 'bar', | ||
}) | ||
}) | ||
|
||
it('removes any empty string values', () => { | ||
expect(sanitizeSubmitData({ foo: 'bar', bar: '', baz: '' })).toEqual({ | ||
foo: 'bar', | ||
}) | ||
}) | ||
|
||
it('removes any undefined values', () => { | ||
expect(sanitizeSubmitData({ foo: 'bar', bar: undefined })).toEqual({ | ||
foo: 'bar', | ||
}) | ||
}) | ||
|
||
it('keeps any other values', () => { | ||
expect( | ||
sanitizeSubmitData({ foo: 'bar', bar: 1, baz: true, qux: {}, quux: false }), | ||
).toEqual({ | ||
foo: 'bar', | ||
bar: 1, | ||
baz: true, | ||
qux: {}, | ||
quux: false, | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Removes any empty, null or undefined values from submit data, to make sure | ||
* we never submit any empty data to the API which resets already filled-out | ||
* porting fields. | ||
*/ | ||
export function sanitizeSubmitData<T extends Record<string, unknown>>(data: T) { | ||
const sanitizedData = Object.fromEntries( | ||
Object.entries(data).filter( | ||
([_, v]) => v !== '' && v !== null && v !== undefined, | ||
), | ||
) | ||
|
||
return sanitizedData | ||
} |