diff --git a/packages/dnb-eufemia/src/extensions/forms/Field/NationalIdentityNumber/NationalIdentityNumber.tsx b/packages/dnb-eufemia/src/extensions/forms/Field/NationalIdentityNumber/NationalIdentityNumber.tsx index 2591fea373a..6fa481c5863 100644 --- a/packages/dnb-eufemia/src/extensions/forms/Field/NationalIdentityNumber/NationalIdentityNumber.tsx +++ b/packages/dnb-eufemia/src/extensions/forms/Field/NationalIdentityNumber/NationalIdentityNumber.tsx @@ -42,7 +42,6 @@ function NationalIdentityNumber(props: Props) { ], [omitMask] ) - const validationPattern = '^[0-9]{11}$' const fnrValidator = useCallback( (value: string) => { @@ -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) @@ -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, diff --git a/packages/dnb-eufemia/src/extensions/forms/Field/NationalIdentityNumber/__tests__/NationalIdentityNumber.test.tsx b/packages/dnb-eufemia/src/extensions/forms/Field/NationalIdentityNumber/__tests__/NationalIdentityNumber.test.tsx index 21629856a52..7d9a19b3047 100644 --- a/packages/dnb-eufemia/src/extensions/forms/Field/NationalIdentityNumber/__tests__/NationalIdentityNumber.test.tsx +++ b/packages/dnb-eufemia/src/extensions/forms/Field/NationalIdentityNumber/__tests__/NationalIdentityNumber.test.tsx @@ -59,15 +59,6 @@ describe('Field.NationalIdentityNumber', () => { ) }) - it('should show "errorRequired" error message when "pattern" not matches', async () => { - render() - - expect(screen.queryByRole('alert')).toBeInTheDocument() - expect(screen.queryByRole('alert')).toHaveTextContent( - nb.NationalIdentityNumber.errorRequired - ) - }) - it('should validate internal validator', async () => { const { rerender } = render( { }) }) + it('should support custom pattern', async () => { + render( + + + + ) + + 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( + + { + return [dummyValidator] + }} + /> + + ) + + 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) => { @@ -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 ) }) }) @@ -226,22 +260,6 @@ describe('Field.NationalIdentityNumber', () => { }).neverToResolve() }) - it('should not validate custom pattern when validate false', async () => { - const invalidPattern = '1234' - render( - - ) - - fireEvent.blur(document.querySelector('input')) - - expect(screen.queryByRole('alert')).toBeNull() - }) - it('should not validate dnum when validate false', async () => { const invalidDnum = '69020112345' render(