Skip to content

Commit 3f57256

Browse files
Merge pull request #120 from contentstack/test/cs-43493-sanity-test-global-fields
sanity test global fields
2 parents 4d20885 + ff8dd65 commit 3f57256

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
}

test/sanity-check/mock/globalfield.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const createGlobalField = {
2+
global_field: {
3+
title: 'First',
4+
uid: 'first',
5+
schema: [{
6+
display_name: 'Name',
7+
uid: 'name',
8+
data_type: 'text'
9+
}, {
10+
data_type: 'text',
11+
display_name: 'Rich text editor',
12+
uid: 'description',
13+
field_metadata: {
14+
allow_rich_text: true,
15+
description: '',
16+
multiline: false,
17+
rich_text_type: 'advanced',
18+
options: [],
19+
version: 3
20+
},
21+
multiple: false,
22+
mandatory: false,
23+
unique: false
24+
}]
25+
}
26+
}
27+
28+
export { createGlobalField }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"title": "Upload",
3+
"uid": "upload",
4+
"schema": [
5+
{
6+
"display_name": "Name",
7+
"uid": "name",
8+
"data_type": "text",
9+
"multiple": false,
10+
"mandatory": false,
11+
"unique": false,
12+
"non_localizable": false
13+
},
14+
{
15+
"display_name": "Add",
16+
"uid": "add",
17+
"data_type": "text",
18+
"multiple": false,
19+
"mandatory": false,
20+
"unique": false,
21+
"non_localizable": false
22+
},
23+
{
24+
"display_name": "std",
25+
"uid": "std",
26+
"data_type": "text",
27+
"multiple": false,
28+
"mandatory": false,
29+
"unique": false,
30+
"non_localizable": false
31+
}
32+
],
33+
"description": ""
34+
}

test/sanity-check/sanity.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require('./api/contentType-test')
1010
require('./api/asset-test')
1111
require('./api/extension-test')
1212
require('./api/entry-test')
13+
require('./api/globalfield-test')
1314
require('./api/contentType-delete-test')
1415
require('./api/taxonomy-test')
1516
require('./api/terms-test')

0 commit comments

Comments
 (0)