Add tests for sanitizeFields
This commit is contained in:
@@ -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
111
src/fields/sanitize.spec.js
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user