Skip to content

Commit

Permalink
[#3383] Added tests for plugin validator
Browse files Browse the repository at this point in the history
  • Loading branch information
vaszig committed Sep 26, 2023
1 parent 0eeac4e commit 49cbc34
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/formio/validators/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const errorMessageMap = {
'phonenumber-nl': 'Invalid Dutch phonenumber',
};

const pluginsAPIValidator = {
export const pluginsAPIValidator = {
key: `validate.backendApi`,
message(component) {
return component.t(component.errorMessage('Invalid'), {
Expand Down Expand Up @@ -47,7 +47,6 @@ const pluginsAPIValidator = {
*/
const enableValidationPlugins = component => {
if (Array.isArray(component.component.validate.plugins)) {
debugger;
component.component.validateOn = 'blur';
component.validator.validators.backendApi = pluginsAPIValidator;
component.validators.push('backendApi');
Expand Down
15 changes: 15 additions & 0 deletions src/jstests/formio/components/fixtures/phonenumber.js
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 = ['630123456', '+31630123456'];
const inValidSamples = ['63012345', '+3163012345'];

export {phoneNumberComponent, validSamples, inValidSamples};
34 changes: 26 additions & 8 deletions src/jstests/formio/validators/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@ import {rest} from 'msw';

import {BASE_URL} from 'api-mocks';

const VALIDATION_ENDPOINT = `${BASE_URL}validation/plugins/phonenumber-international`;

export const mockValidationGet = rest.get(VALIDATION_ENDPOINT, (req, res, ctx) => {
const response = {
isValid: true,
messages: [],
};
return res(ctx.status(200), ctx.json(response));
const INTERNATIONAL_VALIDATION_ENDPOINT = `${BASE_URL}validation/plugins/phonenumber-international`;
const DUTCH_VALIDATION_ENDPOINT = `${BASE_URL}validation/plugins/phonenumber-nl`;

export const mockInternationalValidPost = rest.post(
INTERNATIONAL_VALIDATION_ENDPOINT,
async (req, res, ctx) => {
return res(ctx.status(200), ctx.json({isValid: true, messages: []}));
}
);

export const mockInternationalInValidPost = rest.post(
INTERNATIONAL_VALIDATION_ENDPOINT,
async (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({isValid: false, messages: ['Invalid international phone number']})
);
}
);

export const mockDutchValidPost = rest.post(DUTCH_VALIDATION_ENDPOINT, async (req, res, ctx) => {
return res(ctx.status(200), ctx.json({isValid: true, messages: []}));
});

export const mockDutchInValidPost = rest.post(DUTCH_VALIDATION_ENDPOINT, async (req, res, ctx) => {
return res(ctx.status(200), ctx.json({isValid: false, messages: ['Invalid dutch phone number']}));
});
14 changes: 0 additions & 14 deletions src/jstests/formio/validators/pluginApiValidator.spec.js

This file was deleted.

65 changes: 65 additions & 0 deletions src/jstests/formio/validators/pluginapivalidator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
inValidSamples,
phoneNumberComponent,
validSamples,
} from 'jstests/formio/components/fixtures/phonenumber';
import {invalid} from 'moment';

import {BASE_URL} from 'api-mocks';
import mswServer from 'api-mocks/msw-server';
import {pluginsAPIValidator} from 'formio/validators/plugins';

import {
mockDutchInValidPost,
mockDutchValidPost,
mockInternationalInValidPost,
mockInternationalValidPost,
} from './mocks';

describe('The OpenForms plugins validation', () => {
test('tests expected errors are returned when phone number is invalid', async () => {
mswServer.use(mockDutchInValidPost, mockInternationalInValidPost);

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(mockDutchValidPost, mockInternationalValidPost);

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(mockDutchValidPost, mockInternationalValidPost);

const component = {
component: phoneNumberComponent,
options: {
baseUrl: BASE_URL,
},
};

const result = await pluginsAPIValidator.check(component, undefined, null);
expect(result).toBe(true);
});
});

0 comments on commit 49cbc34

Please sign in to comment.