Skip to content

Commit

Permalink
chore: removes pattern from national id
Browse files Browse the repository at this point in the history
  • Loading branch information
langz committed Oct 9, 2024
1 parent f49eb34 commit 6ad7937
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function NationalIdentityNumber(props: Props) {
],
[omitMask]
)
const validationPattern = '^[0-9]{11}$'

const fnrValidator = useCallback(
(value: string) => {
Expand All @@ -66,9 +65,9 @@ function NationalIdentityNumber(props: Props) {

const dnrAndFnrValidator = useCallback(
(value: string) => {
const validationPattern = '^[4-9].*' // 1st num is increased by 4. i.e, if 01.01.1985, D number would be 410185.
const dnrValidationPattern = '^[4-9].*' // 1st num is increased by 4. i.e, if 01.01.1985, D number would be 410185.

if (new RegExp(validationPattern).test(value)) {
if (new RegExp(dnrValidationPattern).test(value)) {
return dnrValidator(value)
}
return fnrValidator(value)
Expand All @@ -78,12 +77,6 @@ function NationalIdentityNumber(props: Props) {

const StringFieldProps: Props = {
...props,
pattern:
validate && props.pattern
? props.pattern
: validate && !props.validator
? validationPattern
: undefined,
label: props.label ?? label,
errorMessages,
mask,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ describe('Field.NationalIdentityNumber', () => {
)
})

it('should show "errorRequired" error message when "pattern" not matches', async () => {
render(<Field.NationalIdentityNumber validateInitially value="123" />)

expect(screen.queryByRole('alert')).toBeInTheDocument()
expect(screen.queryByRole('alert')).toHaveTextContent(
nb.NationalIdentityNumber.errorRequired
)
})

it('should validate internal validator', async () => {
const { rerender } = render(
<Field.NationalIdentityNumber
Expand Down Expand Up @@ -100,6 +91,49 @@ describe('Field.NationalIdentityNumber', () => {
})
})

it('should support custom pattern', async () => {
render(
<Form.Handler>
<Field.NationalIdentityNumber
validateInitially
value="58081633086" // valid, but not in the pattern
pattern="^6"
/>
</Form.Handler>
)

await waitFor(() => {
expect(screen.queryByRole('alert')).toBeInTheDocument()
expect(screen.queryByRole('alert').textContent).toBe(
nb.NationalIdentityNumber.errorRequired
)
})
})

it('should support custom pattern without validator', async () => {
const dummyValidator = jest.fn()

render(
<Form.Handler>
<Field.NationalIdentityNumber
validateInitially
value="6"
pattern="^6"
onBlurValidator={() => {
return [dummyValidator]
}}
/>
</Form.Handler>
)

await expect(() => {
expect(screen.queryByRole('alert')).toBeInTheDocument()
}).neverToResolve()

expect(dummyValidator).toHaveBeenCalledTimes(1)
expect(dummyValidator).toHaveBeenCalledWith('6', expect.anything())
})

it('should validate given function', async () => {
const text = 'Custom Error message'
const validator = jest.fn((value) => {
Expand Down Expand Up @@ -213,7 +247,7 @@ describe('Field.NationalIdentityNumber', () => {
await waitFor(() => {
expect(screen.queryByRole('alert')).toBeInTheDocument()
expect(screen.queryByRole('alert')).toHaveTextContent(
nb.NationalIdentityNumber.errorRequired
nb.NationalIdentityNumber.errorFnr
)
})
})
Expand All @@ -226,22 +260,6 @@ describe('Field.NationalIdentityNumber', () => {
}).neverToResolve()
})

it('should not validate custom pattern when validate false', async () => {
const invalidPattern = '1234'
render(
<Field.NationalIdentityNumber
pattern="[A-Z]"
value={invalidPattern}
validateInitially
validate={false}
/>
)

fireEvent.blur(document.querySelector('input'))

expect(screen.queryByRole('alert')).toBeNull()
})

it('should not validate dnum when validate false', async () => {
const invalidDnum = '69020112345'
render(
Expand Down

0 comments on commit 6ad7937

Please sign in to comment.