-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathglobalfield-test.js
More file actions
260 lines (242 loc) · 8.92 KB
/
globalfield-test.js
File metadata and controls
260 lines (242 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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, createNestedGlobalFieldForReference, createNestedGlobalField } 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 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 get all nested global fields from Query', (done) => {
makeGlobalField({ api_version: '3.2' })
.query()
.find()
.then((collection) => {
collection.items.forEach((globalField) => {
expect(globalField.uid).to.be.not.equal(null)
expect(globalField.title).to.be.not.equal(null)
})
done()
})
.catch(done)
})
it('should create nested global field for reference', done => {
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalFieldForReference)
.then(globalField => {
expect(globalField.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid)
done()
})
.catch(err => {
console.error('Error:', err.response?.data || err.message)
done(err)
})
})
it('should create nested global field', done => {
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalField)
.then(globalField => {
expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid)
done()
})
.catch(err => {
console.error('Error:', err.response?.data || err.message)
done(err)
})
})
it('should fetch nested global field', done => {
makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }).fetch()
.then(globalField => {
expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid)
done()
})
.catch(err => {
console.error('Error:', err.response?.data || err.message)
done(err)
})
})
it('should fetch and update nested global Field', done => {
makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' }).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 nested global Field', done => {
const globalField = makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' })
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 delete nested global field', (done) => {
makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' })
.delete()
.then((data) => {
expect(data.notice).to.be.equal('Global Field deleted successfully.')
done()
})
.catch((err) => {
console.error('Error:', err.response?.data || err.message)
done(err)
})
})
it('should delete nested global reference field', (done) => {
makeGlobalField(createNestedGlobalFieldForReference.global_field.uid, { api_version: '3.2' })
.delete()
.then((data) => {
expect(data.notice).to.be.equal('Global Field deleted successfully.')
done()
})
.catch((err) => {
console.error('Error:', err.response?.data || err.message)
done(err)
})
})
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 (globalFieldUid = null, options = {}) {
let uid = null
let finalOptions = options
if (typeof globalFieldUid === 'object') {
finalOptions = globalFieldUid
} else {
uid = globalFieldUid
}
finalOptions = finalOptions || {}
return client
.stack({ api_key: process.env.API_KEY }).globalField(uid, finalOptions)
}