From 1fa399cfd2a8358399201dbe7d1e24eba27cedbd Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Tue, 23 Jul 2019 12:45:54 +0200 Subject: [PATCH] Add attributes tests --- .../__tests__/attributes.test.js | 210 +++++++++++++++++- .../utils/configuration/attributes.js | 2 +- .../services/utils/configuration/metadatas.js | 4 +- 3 files changed, 209 insertions(+), 7 deletions(-) diff --git a/packages/strapi-plugin-content-manager/services/utils/configuration/__tests__/attributes.test.js b/packages/strapi-plugin-content-manager/services/utils/configuration/__tests__/attributes.test.js index ca51193e5c..8a3f04f092 100644 --- a/packages/strapi-plugin-content-manager/services/utils/configuration/__tests__/attributes.test.js +++ b/packages/strapi-plugin-content-manager/services/utils/configuration/__tests__/attributes.test.js @@ -1,9 +1,9 @@ const { isSortable, - // isEditable, - // hasEditableAttribute, - // hasListableAttribute, - // hasRelationAttribute, + isEditable, + hasEditableAttribute, + hasListableAttribute, + hasRelationAttribute, } = require('../attributes'); describe('attributesUtils', () => { @@ -11,5 +11,207 @@ describe('attributesUtils', () => { test('The id attribute is always sortable', () => { expect(isSortable({}, 'id')).toBe(true); }); + + test('Group fields are not sortable', () => { + expect( + isSortable( + { + allAttributes: { + someGroup: { + type: 'group', + }, + }, + }, + 'someGroup' + ) + ).toBe(false); + }); + + test('Json fields are not sortable', () => { + expect( + isSortable( + { + allAttributes: { + jsonInput: { + type: 'json', + }, + }, + }, + 'jsonInput' + ) + ).toBe(false); + }); + + test('Relations are not sortable', () => { + expect( + isSortable( + { + allAttributes: { + oneWayRel: { + model: 'someModel', + }, + }, + }, + 'oneWayRel' + ) + ).toBe(false); + + expect( + isSortable( + { + allAttributes: { + manyWayRel: { + collection: 'someModel', + }, + }, + }, + 'manyWayRel' + ) + ).toBe(false); + }); + }); + + describe('isEditable', () => { + test('Check if the attribute is in a model attributes', () => { + expect( + isEditable( + { + attributes: { + field: { + type: 'string', + }, + }, + }, + 'field' + ) + ).toBe(true); + + expect( + isEditable( + { + attributes: { + field: { + type: 'string', + }, + }, + }, + 'createdAt' + ) + ).toBe(false); + }); + }); + + describe('hasEditableAttribute', () => { + test('Check if the attribute exists and is not a relation', () => { + const model = { + allAttributes: { + rel1: { + model: 'someModel', + }, + rel2: { + collection: 'someModel', + }, + title: { + type: 'string', + }, + }, + }; + + expect(hasEditableAttribute(model, 'rel1')).toBe(false); + expect(hasEditableAttribute(model, 'rel2')).toBe(false); + expect(hasEditableAttribute(model, 'unkown')).toBe(false); + expect(hasEditableAttribute(model, 'title')).toBe(true); + }); + }); + + describe('hasListableAttribute', () => { + test('Ids are listable', () => { + expect(hasListableAttribute({}, 'id')).toBe(true); + }); + + test('Unknown attributes are not listable', () => { + const model = { + allAttributes: { + rel1: { + model: 'someModel', + }, + rel2: { + collection: 'someModel', + }, + title: { + type: 'string', + }, + }, + }; + + expect(hasListableAttribute(model, 'unkown')).toBe(false); + }); + + test('Group attributes are not listable', () => { + const model = { + allAttributes: { + someGroup: { + type: 'group', + }, + }, + }; + + expect(hasListableAttribute(model, 'someGroup')).toBe(false); + }); + + test('JSON attributes are not listable', () => { + const model = { + allAttributes: { + someJson: { + type: 'json', + }, + }, + }; + + expect(hasListableAttribute(model, 'someJson')).toBe(false); + }); + + test('Relations are not listable', () => { + const model = { + allAttributes: { + rel1: { + model: 'someModel', + }, + rel2: { + collection: 'someModel', + }, + title: { + type: 'string', + }, + }, + }; + + expect(hasListableAttribute(model, 'rel1')).toBe(false); + expect(hasListableAttribute(model, 'rel2')).toBe(false); + expect(hasListableAttribute(model, 'title')).toBe(true); + }); + }); + + describe('hasRelationAttribute', () => { + test('Only validate relational attributes', () => { + const model = { + allAttributes: { + rel1: { + model: 'someModel', + }, + rel2: { + collection: 'someModel', + }, + title: { + type: 'string', + }, + }, + }; + + expect(hasRelationAttribute(model, 'rel1')).toBe(true); + expect(hasRelationAttribute(model, 'rel2')).toBe(true); + expect(hasRelationAttribute(model, 'unkown')).toBe(false); + expect(hasRelationAttribute(model, 'title')).toBe(false); + }); }); }); diff --git a/packages/strapi-plugin-content-manager/services/utils/configuration/attributes.js b/packages/strapi-plugin-content-manager/services/utils/configuration/attributes.js index 3eb07dd9bf..3305f966d2 100644 --- a/packages/strapi-plugin-content-manager/services/utils/configuration/attributes.js +++ b/packages/strapi-plugin-content-manager/services/utils/configuration/attributes.js @@ -2,7 +2,7 @@ const _ = require('lodash'); -const NON_SORTABLES = ['group', 'json', 'array']; +const NON_SORTABLES = ['group', 'json']; const isSortable = (model, name) => { if (name === 'id') return true; diff --git a/packages/strapi-plugin-content-manager/services/utils/configuration/metadatas.js b/packages/strapi-plugin-content-manager/services/utils/configuration/metadatas.js index bd9ae74426..a522d210dc 100644 --- a/packages/strapi-plugin-content-manager/services/utils/configuration/metadatas.js +++ b/packages/strapi-plugin-content-manager/services/utils/configuration/metadatas.js @@ -7,7 +7,7 @@ const { hasListableAttribute, } = require('./attributes'); -async function createDefaultMetadatas(model) { +function createDefaultMetadatas(model) { return { id: { edit: {}, @@ -62,7 +62,7 @@ async function syncMetadatas(configuration, model) { // add new keys and missing fields const metasWithDefaults = _.merge( {}, - await createDefaultMetadatas(model), + createDefaultMetadatas(model), metasWithValidKeys );