From 012902d94f343076b6b15805f30f33ad53d35f1b Mon Sep 17 00:00:00 2001 From: Tanmoy Bhowmik Date: Fri, 2 Oct 2020 18:50:37 +0530 Subject: [PATCH] Feat: Add private option field for relations (#8149) * feat(content-type): add private option to relation field Signed-off-by: tanmoyopenroot * test(content-type): add test for private option in relation field Signed-off-by: tanmoyopenroot --- .../src/containers/FormModal/utils/forms.js | 1 + .../FormModal/utils/tests/forms.test.js | 11 +++++ .../test/content-types.test.e2e.js | 43 +++++++++++++++++++ .../utils/attributes.js | 1 + 4 files changed, 56 insertions(+) create mode 100644 packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/tests/forms.test.js diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/forms.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/forms.js index ddcaee7b5f..d8bbcad185 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/forms.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/forms.js @@ -331,6 +331,7 @@ const forms = { const nameValue = get(data, 'name', null); const relationItems = [ [fields.divider], + [fields.private], [fields.unique], [ { diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/tests/forms.test.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/tests/forms.test.js new file mode 100644 index 0000000000..85d3f8fa48 --- /dev/null +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/tests/forms.test.js @@ -0,0 +1,11 @@ +import fields from '../forms'; + +describe('forms', () => { + it('should contain private field in relation input type', () => { + const { items } = fields.attribute.form.advanced({}, 'relation', null); + + expect( + items.find(relationItem => relationItem.find(item => item.name === 'private')).length + ).toBeTruthy(); + }); +}); diff --git a/packages/strapi-plugin-content-type-builder/test/content-types.test.e2e.js b/packages/strapi-plugin-content-type-builder/test/content-types.test.e2e.js index a7d06a95e0..ae5112c9ef 100644 --- a/packages/strapi-plugin-content-type-builder/test/content-types.test.e2e.js +++ b/packages/strapi-plugin-content-type-builder/test/content-types.test.e2e.js @@ -189,4 +189,47 @@ describe('Content Type Builder - Content types', () => { expect(updateRes.body.error).toMatch('multiple entries in DB'); }); }); + + describe('Private relation field', () => { + const singleTypeUID = 'application::test-single-type.test-single-type'; + + test('should add a relation field', async () => { + const res = await rq({ + method: 'PUT', + url: `/content-type-builder/content-types/${singleTypeUID}`, + body: { + contentType: { + kind: 'singleType', + name: 'test-collection', + attributes: { + relation: { + private: true, + nature: 'oneWay', + target: 'plugins::users-permissions.user', + targetAttribute: 'test', + }, + }, + }, + }, + }); + + expect(res.statusCode).toBe(201); + expect(res.body).toEqual({ + data: { + uid: singleTypeUID, + }, + }); + }); + + test('should contain a private relation field', async () => { + const res = await rq({ + method: 'GET', + url: `/content-type-builder/content-types/${singleTypeUID}`, + }); + + expect(res.statusCode).toBe(200); + expect(res.body.data.schema.attributes.relation).toBeDefined(); + expect(res.body.data.schema.attributes.relation.private).toBeTruthy(); + }); + }); }); diff --git a/packages/strapi-plugin-content-type-builder/utils/attributes.js b/packages/strapi-plugin-content-type-builder/utils/attributes.js index bd265daa60..9be70b12c8 100644 --- a/packages/strapi-plugin-content-type-builder/utils/attributes.js +++ b/packages/strapi-plugin-content-type-builder/utils/attributes.js @@ -85,6 +85,7 @@ const formatAttribute = (key, attribute, { model }) => { ['attributes', attribute.via, 'columnName'], undefined ), + private: attribute.private ? true : false, unique: attribute.unique ? true : false, autoPopulate: attribute.autoPopulate, };