|
| 1 | +import path from 'path' |
| 2 | +import { expect } from 'chai' |
| 3 | +import { cloneDeep } from 'lodash' |
| 4 | +import { describe, it, setup } from 'mocha' |
| 5 | +import { jsonReader } from '../utility/fileOperations/readwrite' |
| 6 | +import { createGlobalField } from '../mock/globalfield' |
| 7 | +import { contentstackClient } from '../utility/ContentstackClient.js' |
| 8 | +import dotenv from 'dotenv' |
| 9 | + |
| 10 | +dotenv.config() |
| 11 | +let client = {} |
| 12 | +let createGlobalFieldUid = '' |
| 13 | + |
| 14 | +describe('Global Field api Test', () => { |
| 15 | + setup(() => { |
| 16 | + const user = jsonReader('loggedinuser.json') |
| 17 | + client = contentstackClient(user.authtoken) |
| 18 | + }) |
| 19 | + |
| 20 | + it('should create global field', done => { |
| 21 | + makeGlobalField().create(createGlobalField) |
| 22 | + .then((globalField) => { |
| 23 | + expect(globalField.uid).to.be.equal(createGlobalField.global_field.uid) |
| 24 | + expect(globalField.title).to.be.equal(createGlobalField.global_field.title) |
| 25 | + expect(globalField.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid) |
| 26 | + expect(globalField.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type) |
| 27 | + expect(globalField.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name) |
| 28 | + done() |
| 29 | + }) |
| 30 | + .catch(done) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should fetch global Field', done => { |
| 34 | + makeGlobalField(createGlobalField.global_field.uid).fetch() |
| 35 | + .then((globalField) => { |
| 36 | + expect(globalField.uid).to.be.equal(createGlobalField.global_field.uid) |
| 37 | + expect(globalField.title).to.be.equal(createGlobalField.global_field.title) |
| 38 | + expect(globalField.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid) |
| 39 | + expect(globalField.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type) |
| 40 | + expect(globalField.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name) |
| 41 | + done() |
| 42 | + }) |
| 43 | + .catch(done) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should fetch and update global Field', done => { |
| 47 | + makeGlobalField(createGlobalField.global_field.uid).fetch() |
| 48 | + .then((globalField) => { |
| 49 | + globalField.title = 'Update title' |
| 50 | + return globalField.update() |
| 51 | + }) |
| 52 | + .then((updateGlobal) => { |
| 53 | + expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid) |
| 54 | + expect(updateGlobal.title).to.be.equal('Update title') |
| 55 | + expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid) |
| 56 | + expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type) |
| 57 | + expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name) |
| 58 | + done() |
| 59 | + }) |
| 60 | + .catch(done) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should update global Field', done => { |
| 64 | + const globalField = makeGlobalField(createGlobalField.global_field.uid) |
| 65 | + Object.assign(globalField, cloneDeep(createGlobalField.global_field)) |
| 66 | + globalField.update() |
| 67 | + .then((updateGlobal) => { |
| 68 | + expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid) |
| 69 | + expect(updateGlobal.title).to.be.equal(createGlobalField.global_field.title) |
| 70 | + expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid) |
| 71 | + expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type) |
| 72 | + expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name) |
| 73 | + done() |
| 74 | + }) |
| 75 | + .catch(done) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should import global Field', done => { |
| 79 | + makeGlobalField().import({ |
| 80 | + global_field: path.join(__dirname, '../mock/globalfield.json') |
| 81 | + }) |
| 82 | + .then((response) => { |
| 83 | + createGlobalFieldUid = response.uid |
| 84 | + expect(response.uid).to.be.not.equal(null) |
| 85 | + done() |
| 86 | + }) |
| 87 | + .catch(done) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should get all global field from Query', done => { |
| 91 | + makeGlobalField().query() |
| 92 | + .find() |
| 93 | + .then((collection) => { |
| 94 | + collection.items.forEach(globalField => { |
| 95 | + expect(globalField.uid).to.be.not.equal(null) |
| 96 | + expect(globalField.title).to.be.not.equal(null) |
| 97 | + expect(globalField.schema).to.be.not.equal(null) |
| 98 | + }) |
| 99 | + done() |
| 100 | + }) |
| 101 | + .catch(done) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should get global field title matching Upload', done => { |
| 105 | + makeGlobalField().query({ query: { title: 'Upload' } }) |
| 106 | + .find() |
| 107 | + .then((collection) => { |
| 108 | + collection.items.forEach(globalField => { |
| 109 | + expect(globalField.uid).to.be.not.equal(null) |
| 110 | + expect(globalField.title).to.be.equal('Upload') |
| 111 | + }) |
| 112 | + done() |
| 113 | + }) |
| 114 | + .catch(done) |
| 115 | + }) |
| 116 | + |
| 117 | + it('should delete global Field', done => { |
| 118 | + makeGlobalField(createGlobalField.global_field.uid) |
| 119 | + .delete() |
| 120 | + .then((data) => { |
| 121 | + expect(data.notice).to.be.equal('Global Field deleted successfully.') |
| 122 | + done() |
| 123 | + }) |
| 124 | + .catch(done) |
| 125 | + }) |
| 126 | + |
| 127 | + it('should delete imported global Field', done => { |
| 128 | + makeGlobalField(createGlobalFieldUid) |
| 129 | + .delete() |
| 130 | + .then((data) => { |
| 131 | + expect(data.notice).to.be.equal('Global Field deleted successfully.') |
| 132 | + done() |
| 133 | + }) |
| 134 | + .catch(done) |
| 135 | + }) |
| 136 | +}) |
| 137 | + |
| 138 | +function makeGlobalField (uid = null) { |
| 139 | + return client.stack({ api_key: process.env.API_KEY }).globalField(uid) |
| 140 | +} |
0 commit comments