Add tests for sanitizeFields

This commit is contained in:
Elliot DeNolf
2020-09-10 21:47:55 -04:00
parent da3495cb4b
commit bf452c23bc
2 changed files with 111 additions and 6 deletions

View File

@@ -9,12 +9,6 @@ const sanitizeFields = (fields, validRelationships) => {
if (!field.type) throw new MissingFieldType(field);
if (field.type === 'blocks') {
field.blocks.forEach((blockCollection) => {
sanitizeFields(blockCollection.fields, validRelationships);
});
}
if (field.type === 'relationship') {
const relationships = Array.isArray(field.relationTo) ? field.relationTo : [field.relationTo];
relationships.forEach((relationship) => {

111
src/fields/sanitize.spec.js Normal file
View File

@@ -0,0 +1,111 @@
/* eslint-disable jest/require-to-throw-message */
const sanitizeFields = require('./sanitize');
const { MissingFieldType, InvalidFieldRelationship } = require('../errors');
describe('sanitizeFields', () => {
it('should throw on missing type field', () => {
const fields = [{
label: 'some-collection',
name: 'Some Collection',
}];
expect(() => {
sanitizeFields(fields, []);
}).toThrow(MissingFieldType);
});
describe('relationships', () => {
it('should not throw on valid relationship', () => {
const validRelationships = ['some-collection'];
const fields = [{
type: 'relationship',
label: 'my-relationship',
name: 'My Relationship',
relationTo: 'some-collection',
}];
expect(() => {
sanitizeFields(fields, validRelationships);
}).not.toThrow();
});
it('should not throw on valid relationship - multiple', () => {
const validRelationships = ['some-collection', 'another-collection'];
const fields = [{
type: 'relationship',
label: 'my-relationship',
name: 'My Relationship',
relationTo: ['some-collection', 'another-collection'],
}];
expect(() => {
sanitizeFields(fields, validRelationships);
}).not.toThrow();
});
it('should not throw on valid relationship inside blocks', () => {
const validRelationships = ['some-collection'];
const fields = [{
name: 'layout',
label: 'Layout Blocks',
singularLabel: 'Block',
type: 'blocks',
blocks: [{
fields: [{
type: 'relationship',
label: 'my-relationship',
name: 'My Relationship',
relationTo: 'some-collection',
}],
}],
}];
expect(() => {
sanitizeFields(fields, validRelationships);
}).not.toThrow();
});
it('should throw on invalid relationship', () => {
const validRelationships = ['some-collection'];
const fields = [{
type: 'relationship',
label: 'my-relationship',
name: 'My Relationship',
relationTo: 'not-valid',
}];
expect(() => {
sanitizeFields(fields, validRelationships);
}).toThrow(InvalidFieldRelationship);
});
it('should throw on invalid relationship - multiple', () => {
const validRelationships = ['some-collection', 'another-collection'];
const fields = [{
type: 'relationship',
label: 'my-relationship',
name: 'My Relationship',
relationTo: ['some-collection', 'not-valid'],
}];
expect(() => {
sanitizeFields(fields, validRelationships);
}).toThrow(InvalidFieldRelationship);
});
it('should throw on invalid relationship inside blocks', () => {
const validRelationships = ['some-collection'];
const fields = [{
name: 'layout',
label: 'Layout Blocks',
singularLabel: 'Block',
type: 'blocks',
blocks: [{
fields: [{
type: 'relationship',
label: 'my-relationship',
name: 'My Relationship',
relationTo: 'not-valid',
}],
}],
}];
expect(() => {
sanitizeFields(fields, validRelationships);
}).toThrow(InvalidFieldRelationship);
});
});
});