diff --git a/test/sanity-check/api/globalfield-test.js b/test/sanity-check/api/globalfield-test.js new file mode 100644 index 00000000..acecd852 --- /dev/null +++ b/test/sanity-check/api/globalfield-test.js @@ -0,0 +1,140 @@ +import path from 'path' +import { expect } from 'chai' +import { cloneDeep } from 'lodash' +import { describe, it, setup } from 'mocha' +import { jsonReader } from '../utility/fileOperations/readwrite' +import { createGlobalField } from '../mock/globalfield' +import { contentstackClient } from '../utility/ContentstackClient.js' +import dotenv from 'dotenv' + +dotenv.config() +let client = {} +let createGlobalFieldUid = '' + +describe('Global Field api Test', () => { + setup(() => { + const user = jsonReader('loggedinuser.json') + client = contentstackClient(user.authtoken) + }) + + it('should create global field', done => { + makeGlobalField().create(createGlobalField) + .then((globalField) => { + expect(globalField.uid).to.be.equal(createGlobalField.global_field.uid) + expect(globalField.title).to.be.equal(createGlobalField.global_field.title) + expect(globalField.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid) + expect(globalField.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type) + expect(globalField.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name) + done() + }) + .catch(done) + }) + + it('should fetch global Field', done => { + makeGlobalField(createGlobalField.global_field.uid).fetch() + .then((globalField) => { + expect(globalField.uid).to.be.equal(createGlobalField.global_field.uid) + expect(globalField.title).to.be.equal(createGlobalField.global_field.title) + expect(globalField.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid) + expect(globalField.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type) + expect(globalField.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name) + done() + }) + .catch(done) + }) + + it('should fetch and update global Field', done => { + makeGlobalField(createGlobalField.global_field.uid).fetch() + .then((globalField) => { + globalField.title = 'Update title' + return globalField.update() + }) + .then((updateGlobal) => { + expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid) + expect(updateGlobal.title).to.be.equal('Update title') + expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid) + expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type) + expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name) + done() + }) + .catch(done) + }) + + it('should update global Field', done => { + const globalField = makeGlobalField(createGlobalField.global_field.uid) + Object.assign(globalField, cloneDeep(createGlobalField.global_field)) + globalField.update() + .then((updateGlobal) => { + expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid) + expect(updateGlobal.title).to.be.equal(createGlobalField.global_field.title) + expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid) + expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type) + expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name) + done() + }) + .catch(done) + }) + + it('should import global Field', done => { + makeGlobalField().import({ + global_field: path.join(__dirname, '../mock/globalfield.json') + }) + .then((response) => { + createGlobalFieldUid = response.uid + expect(response.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should get all global field from Query', done => { + makeGlobalField().query() + .find() + .then((collection) => { + collection.items.forEach(globalField => { + expect(globalField.uid).to.be.not.equal(null) + expect(globalField.title).to.be.not.equal(null) + expect(globalField.schema).to.be.not.equal(null) + }) + done() + }) + .catch(done) + }) + + it('should get global field title matching Upload', done => { + makeGlobalField().query({ query: { title: 'Upload' } }) + .find() + .then((collection) => { + collection.items.forEach(globalField => { + expect(globalField.uid).to.be.not.equal(null) + expect(globalField.title).to.be.equal('Upload') + }) + done() + }) + .catch(done) + }) + + it('should delete global Field', done => { + makeGlobalField(createGlobalField.global_field.uid) + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Global Field deleted successfully.') + done() + }) + .catch(done) + }) + + it('should delete imported global Field', done => { + makeGlobalField(createGlobalFieldUid) + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Global Field deleted successfully.') + done() + }) + .catch(done) + }) +}) + +function makeGlobalField (uid = null) { + return client.stack({ api_key: process.env.API_KEY }).globalField(uid) +} diff --git a/test/sanity-check/mock/globalfield.js b/test/sanity-check/mock/globalfield.js new file mode 100644 index 00000000..22f07718 --- /dev/null +++ b/test/sanity-check/mock/globalfield.js @@ -0,0 +1,28 @@ +const createGlobalField = { + global_field: { + title: 'First', + uid: 'first', + schema: [{ + display_name: 'Name', + uid: 'name', + data_type: 'text' + }, { + data_type: 'text', + display_name: 'Rich text editor', + uid: 'description', + field_metadata: { + allow_rich_text: true, + description: '', + multiline: false, + rich_text_type: 'advanced', + options: [], + version: 3 + }, + multiple: false, + mandatory: false, + unique: false + }] + } +} + +export { createGlobalField } diff --git a/test/sanity-check/mock/globalfield.json b/test/sanity-check/mock/globalfield.json new file mode 100644 index 00000000..56b6de61 --- /dev/null +++ b/test/sanity-check/mock/globalfield.json @@ -0,0 +1,34 @@ +{ + "title": "Upload", + "uid": "upload", + "schema": [ + { + "display_name": "Name", + "uid": "name", + "data_type": "text", + "multiple": false, + "mandatory": false, + "unique": false, + "non_localizable": false + }, + { + "display_name": "Add", + "uid": "add", + "data_type": "text", + "multiple": false, + "mandatory": false, + "unique": false, + "non_localizable": false + }, + { + "display_name": "std", + "uid": "std", + "data_type": "text", + "multiple": false, + "mandatory": false, + "unique": false, + "non_localizable": false + } + ], + "description": "" + } \ No newline at end of file diff --git a/test/sanity-check/sanity.js b/test/sanity-check/sanity.js index 74ab6d47..cebee4d2 100644 --- a/test/sanity-check/sanity.js +++ b/test/sanity-check/sanity.js @@ -10,6 +10,7 @@ require('./api/contentType-test') require('./api/asset-test') require('./api/extension-test') require('./api/entry-test') +require('./api/globalfield-test') require('./api/contentType-delete-test') require('./api/taxonomy-test') require('./api/terms-test')