From bf452c23bcd99a60694b2bfd2937547fd250b119 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Thu, 10 Sep 2020 21:47:55 -0400 Subject: [PATCH] Add tests for sanitizeFields --- src/fields/sanitize.js | 6 -- src/fields/sanitize.spec.js | 111 ++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 src/fields/sanitize.spec.js diff --git a/src/fields/sanitize.js b/src/fields/sanitize.js index ca95d6e006..4e5d8ca156 100644 --- a/src/fields/sanitize.js +++ b/src/fields/sanitize.js @@ -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) => { diff --git a/src/fields/sanitize.spec.js b/src/fields/sanitize.spec.js new file mode 100644 index 0000000000..c19c085b5b --- /dev/null +++ b/src/fields/sanitize.spec.js @@ -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); + }); + }); +});