-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests on smart action field validator
- Loading branch information
1 parent
a0e1e0b
commit 1cb27ec
Showing
2 changed files
with
123 additions
and
1 deletion.
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,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`); | ||
}); | ||
}); | ||
}); | ||
}); |