Skip to content

Commit

Permalink
test: add tests on smart action field validator
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-moncel committed May 20, 2021
1 parent a0e1e0b commit 1cb27ec
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/services/smart-action-field-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SmartActionFieldValidator {

if (isReadOnly && typeof isReadOnly !== 'boolean') throw new Error(`isReadOnly of "${fieldName}" must be a boolean.`);

if (reference && typeof reference !== 'string') throw new Error(`reference of "${fieldName}" should be a string.`);
if (reference && typeof reference !== 'string') throw new Error(`reference of "${fieldName}" must be a string.`);

if (type !== undefined && (Array.isArray(type)
? !this.validFieldArrayType.includes(type[0])
Expand Down
122 changes: 122 additions & 0 deletions test/services/smart-action-field-validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const SmartActionFieldValidator = require('../../src/services/smart-action-field-validator');

const smartActionFieldValidator = new SmartActionFieldValidator();

describe('services > smart-action-field-validator', () => {
describe('validateField', () => {
it('should not throw if the field is valid', () => {
expect.assertions(1);

const field = {
field: 'test',
description: 'a description',
isRequired: false,
isReadOnly: false,
type: 'String',
};

expect(() => smartActionFieldValidator.validateField(field)).not.toThrow();
});

describe('field is not an object', () => {
it('should throw if field is null', () => {
expect.assertions(1);

const field = null;

expect(() => smartActionFieldValidator.validateField(field)).toThrow('Field must be an object.');
});

it('should throw if field is an array', () => {
expect.assertions(1);

const field = [];

expect(() => smartActionFieldValidator.validateField(field)).toThrow('Field must be an object.');
});

it('should throw if field is a function', () => {
expect.assertions(1);

const field = () => {};

expect(() => smartActionFieldValidator.validateField(field)).toThrow('Field must be an object.');
});
});

describe('field property is not valid', () => {
const generateField = () => ({ field: 'test' });

it('should throw if field.field is not defined', () => {
expect.assertions(1);

const field = {};

expect(() => smartActionFieldValidator.validateField(field)).toThrow('field attribute must be defined.');
});

it('should throw if field.field is not a string', () => {
expect.assertions(1);

const field = generateField();
field.field = 1;

expect(() => smartActionFieldValidator.validateField(field)).toThrow('field attribute must be a string.');
});

it('should throw if field.description is not a string', () => {
expect.assertions(1);

const field = generateField();
field.description = () => {};

expect(() => smartActionFieldValidator.validateField(field)).toThrow(`description of "${field.field}" must be a string.`);
});

it('should throw if field.enums is not an array', () => {
expect.assertions(1);

const field = generateField();
field.enums = () => {};

expect(() => smartActionFieldValidator.validateField(field)).toThrow(`enums of "${field.field}" must be an array.`);
});

it('should throw if field.isRequired is not a boolean', () => {
expect.assertions(1);

const field = generateField();
field.isRequired = 1;

expect(() => smartActionFieldValidator.validateField(field)).toThrow(`isRequired of "${field.field}" must be a boolean.`);
});

it('should throw if field.isReadOnly is not a boolean', () => {
expect.assertions(1);

const field = generateField();
field.isReadOnly = 1;

expect(() => smartActionFieldValidator.validateField(field)).toThrow(`isReadOnly of "${field.field}" must be a boolean.`);
});

it('should throw if field.reference is not a string', () => {
expect.assertions(1);

const field = generateField();
field.reference = 1;

expect(() => smartActionFieldValidator.validateField(field)).toThrow(`reference of "${field.field}" must be a string.`);
});

it('should throw if field.type is not a valid type', () => {
expect.assertions(1);

const field = generateField();
field.type = 1;

expect(() => smartActionFieldValidator.validateField(field)).toThrow(`type of "${field.field}" must be a valid type. See the documentation for more information. https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields#available-field-options`);
});
});
});
});

0 comments on commit 1cb27ec

Please sign in to comment.