-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #553 from open-formulieren/of-task/3383-phone-numb…
…er-validation [OF#3383] Refactor phone number validation
- Loading branch information
Showing
4 changed files
with
143 additions
and
39 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,15 @@ | ||
const phoneNumberComponent = { | ||
label: 'Phone Number', | ||
key: 'phonenumber', | ||
type: 'phoneNumber', | ||
input: true, | ||
validate: { | ||
backendApi: true, | ||
plugins: ['phonenumber-international', 'phonenumber-nl'], | ||
}, | ||
}; | ||
|
||
const validSamples = ['0630123456', '+31630123456']; | ||
const inValidSamples = ['63012345', '+3163012345']; | ||
|
||
export {phoneNumberComponent, validSamples, inValidSamples}; |
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,36 @@ | ||
import {rest} from 'msw'; | ||
|
||
import {BASE_URL} from 'api-mocks'; | ||
|
||
const INTERNATIONAL_VALIDATION_ENDPOINT = `${BASE_URL}validation/plugins/phonenumber-international`; | ||
const DUTCH_VALIDATION_ENDPOINT = `${BASE_URL}validation/plugins/phonenumber-nl`; | ||
|
||
export const phoneNumberValidations = { | ||
mockValidInternationalPhonenumberPost: rest.post( | ||
INTERNATIONAL_VALIDATION_ENDPOINT, | ||
async (req, res, ctx) => { | ||
return res(ctx.status(200), ctx.json({isValid: true, messages: []})); | ||
} | ||
), | ||
|
||
mockInValidInternationalPhonenumberPost: rest.post( | ||
INTERNATIONAL_VALIDATION_ENDPOINT, | ||
async (req, res, ctx) => { | ||
return res( | ||
ctx.status(200), | ||
ctx.json({isValid: false, messages: ['Invalid international phone number']}) | ||
); | ||
} | ||
), | ||
|
||
mockValidDutchPhonenumberPost: rest.post(DUTCH_VALIDATION_ENDPOINT, async (req, res, ctx) => { | ||
return res(ctx.status(200), ctx.json({isValid: true, messages: []})); | ||
}), | ||
|
||
mockInValidDutchPhonenumberPost: rest.post(DUTCH_VALIDATION_ENDPOINT, async (req, res, ctx) => { | ||
return res( | ||
ctx.status(200), | ||
ctx.json({isValid: false, messages: ['Invalid dutch phone number']}) | ||
); | ||
}), | ||
}; |
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,68 @@ | ||
import { | ||
inValidSamples, | ||
phoneNumberComponent, | ||
validSamples, | ||
} from 'jstests/formio/components/fixtures/phonenumber'; | ||
|
||
import {BASE_URL} from 'api-mocks'; | ||
import mswServer from 'api-mocks/msw-server'; | ||
import {pluginsAPIValidator} from 'formio/validators/plugins'; | ||
|
||
import {phoneNumberValidations} from './mocks'; | ||
|
||
describe('The OpenForms plugins validation', () => { | ||
test('tests expected errors are returned when phone number is invalid', async () => { | ||
mswServer.use( | ||
phoneNumberValidations.mockInValidDutchPhonenumberPost, | ||
phoneNumberValidations.mockInValidInternationalPhonenumberPost | ||
); | ||
|
||
const component = { | ||
component: phoneNumberComponent, | ||
options: { | ||
baseUrl: BASE_URL, | ||
}, | ||
}; | ||
|
||
for (const sample of inValidSamples) { | ||
const result = await pluginsAPIValidator.check(component, undefined, sample); | ||
expect(result).toBe('Invalid international phone number<br>Invalid dutch phone number'); | ||
} | ||
}); | ||
|
||
test('tests no errors are returned when phone number is valid', async () => { | ||
mswServer.use( | ||
phoneNumberValidations.mockValidDutchPhonenumberPost, | ||
phoneNumberValidations.mockValidInternationalPhonenumberPost | ||
); | ||
|
||
const component = { | ||
component: phoneNumberComponent, | ||
options: { | ||
baseUrl: BASE_URL, | ||
}, | ||
}; | ||
|
||
for (const sample of validSamples) { | ||
const result = await pluginsAPIValidator.check(component, undefined, sample); | ||
expect(result).toBe(true); | ||
} | ||
}); | ||
|
||
test('tests no errors are returned when phone number is null', async () => { | ||
mswServer.use( | ||
phoneNumberValidations.mockValidDutchPhonenumberPost, | ||
phoneNumberValidations.mockValidInternationalPhonenumberPost | ||
); | ||
|
||
const component = { | ||
component: phoneNumberComponent, | ||
options: { | ||
baseUrl: BASE_URL, | ||
}, | ||
}; | ||
|
||
const result = await pluginsAPIValidator.check(component, undefined, null); | ||
expect(result).toBe(true); | ||
}); | ||
}); |