Skip to content

Commit 30d3413

Browse files
committed
test: ✅ improves code coverage
1 parent 1f22d0a commit 30d3413

28 files changed

+1382
-213
lines changed

lib/entity.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,6 @@ export const create = ({ http, params }) => {
9393
}
9494
}
9595

96-
export const exportObject = ({ http }) => {
97-
return async function (param) {
98-
const headers = {
99-
params: {
100-
...cloneDeep(param)
101-
},
102-
headers: {
103-
...cloneDeep(this.stackHeaders)
104-
}
105-
} || {}
106-
try {
107-
const response = await http.get(this.urlPath, headers)
108-
if (response.data) {
109-
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
110-
} else {
111-
throw error(response)
112-
}
113-
} catch (err) {
114-
throw error(err)
115-
}
116-
}
117-
}
118-
11996
export const query = ({ http, wrapperCollection }) => {
12097
return function (params = {}) {
12198
const headers = {
@@ -181,9 +158,7 @@ export const deleteEntity = (http, force = false, params = {}) => {
181158
try {
182159
const headers = {
183160
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) },
184-
params: {
185-
...cloneDeep(param)
186-
}
161+
params: { ...cloneDeep(param) }
187162
} || {}
188163
if (force === true) {
189164
headers.params.force = true

lib/stack/contentType/entry/index.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -266,46 +266,46 @@ export function Entry (http, data) {
266266
}
267267
}
268268
} else {
269-
/**
270-
* @description The Create an entry call creates a new entry for the selected content type.
271-
* @memberof Entry
272-
* @func create
273-
* @returns {Promise<Entry.Entry>} Promise for Entry instance
274-
*
275-
* @example
276-
* import * as contentstack from '@contentstack/management'
277-
* const client = contentstack.client()
278-
* const entry = {
279-
* title: 'Sample Entry',
280-
* url: '/sampleEntry',
281-
* file: 'asset_uid', // for single asset pass asset uid to entry asset field value
282-
* multiple_file = ['asset_uid_1', 'asset_uid_2'], // for multiple asset pass array of asset uid to entry asset field values
283-
* reference: reference.uid, // for single reference pass reference uid to entry reference field value
284-
* multiple_reference: ['reference_uid_1', 'reference_uid_2'], // for multiple reference pass array of reference uid to entry reference field values
285-
* multiple_content_type_reference: [{_content_type_uid: 'content_type_uid_1', uid: 'reference_uid_1'}, {_content_type_uid: 'content_type_uid_2', uid: 'reference_uid_2'}] // for multiple reference pass array of reference uid to entry reference field values
286-
* }
287-
* client.stack().contentType('content_type_uid').entry().create({ entry })
288-
* .then((entry) => console.log(entry))
289-
*/
269+
/**
270+
* @description The Create an entry call creates a new entry for the selected content type.
271+
* @memberof Entry
272+
* @func create
273+
* @returns {Promise<Entry.Entry>} Promise for Entry instance
274+
*
275+
* @example
276+
* import * as contentstack from '@contentstack/management'
277+
* const client = contentstack.client()
278+
* const entry = {
279+
* title: 'Sample Entry',
280+
* url: '/sampleEntry',
281+
* file: 'asset_uid', // for single asset pass asset uid to entry asset field value
282+
* multiple_file = ['asset_uid_1', 'asset_uid_2'], // for multiple asset pass array of asset uid to entry asset field values
283+
* reference: reference.uid, // for single reference pass reference uid to entry reference field value
284+
* multiple_reference: ['reference_uid_1', 'reference_uid_2'], // for multiple reference pass array of reference uid to entry reference field values
285+
* multiple_content_type_reference: [{_content_type_uid: 'content_type_uid_1', uid: 'reference_uid_1'}, {_content_type_uid: 'content_type_uid_2', uid: 'reference_uid_2'}] // for multiple reference pass array of reference uid to entry reference field values
286+
* }
287+
* client.stack().contentType('content_type_uid').entry().create({ entry })
288+
* .then((entry) => console.log(entry))
289+
*/
290290
this.create = create({ http: http })
291291

292292
/**
293-
* @description The Query on Entry will allow to fetch details of all or specific Entry
294-
* @memberof Entry
295-
* @func query
296-
* @param {Int} locale Enter the code of the language of which the entries need to be included. Only the entries published in this locale will be displayed.
297-
* @param {Int} include_workflow Enter 'true' to include the workflow details of the entry.
298-
* @param {Int} include_publish_details Enter 'true' to include the publish details of the entry.
299-
* @param {Object} query Queries that you can use to fetch filtered results.
300-
* @returns {Array<Entry>} Array of Entry.
301-
*
302-
* @example
303-
* import * as contentstack from '@contentstack/management'
304-
* const client = contentstack.client()
305-
*
306-
* client.stack().contentType('content_type_uid').entry().query({ query: { title: 'Entry title' } }).find()
307-
* .then((entries) => console.log(entries))
308-
*/
293+
* @description The Query on Entry will allow to fetch details of all or specific Entry
294+
* @memberof Entry
295+
* @func query
296+
* @param {Int} locale Enter the code of the language of which the entries need to be included. Only the entries published in this locale will be displayed.
297+
* @param {Int} include_workflow Enter 'true' to include the workflow details of the entry.
298+
* @param {Int} include_publish_details Enter 'true' to include the publish details of the entry.
299+
* @param {Object} query Queries that you can use to fetch filtered results.
300+
* @returns {Array<Entry>} Array of Entry.
301+
*
302+
* @example
303+
* import * as contentstack from '@contentstack/management'
304+
* const client = contentstack.client()
305+
*
306+
* client.stack().contentType('content_type_uid').entry().query({ query: { title: 'Entry title' } }).find()
307+
* .then((entries) => console.log(entries))
308+
*/
309309
this.query = query({ http: http, wrapperCollection: EntryCollection })
310310
}
311311

test/api/stack-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('Stack api Test', () => {
5353
})
5454

5555
it('Update Stack details', done => {
56-
const name = 'My New Stack Update Name'
56+
const name = 'Conference Demo'
5757
const description = 'My New description stack'
5858
client.stack({ api_key: stacks.api_key })
5959
.fetch().then((stack) => {
@@ -130,13 +130,13 @@ describe('Stack api Test', () => {
130130

131131
it('Get query stack', done => {
132132
client.stack()
133-
.query({ query: { name: 'Conference Demo' } })
133+
.query({ query: { name: 'My New Stack Update Name' } })
134134
.find()
135135
.then((response) => {
136136
expect(response.items.length).to.be.equal(1)
137137
for (const index in response.items) {
138138
const stack = response.items[index]
139-
expect(stack.name).to.be.equal('Conference Demo')
139+
expect(stack.name).to.be.equal('My New Stack Update Name')
140140
}
141141
done()
142142
})
@@ -145,12 +145,12 @@ describe('Stack api Test', () => {
145145

146146
it('Find one stack', done => {
147147
client.stack()
148-
.query({ query: { name: 'Conference Demo' } })
148+
.query({ query: { name: 'My New Stack Update Name' } })
149149
.findOne()
150150
.then((response) => {
151151
const stack = response.items[0]
152152
expect(response.items.length).to.be.equal(1)
153-
expect(stack.name).to.be.equal('Conference Demo')
153+
expect(stack.name).to.be.equal('My New Stack Update Name')
154154
done()
155155
})
156156
.catch(done)

test/api/user-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var loggedinUserID = ''
1111
var client = contentstackClient()
1212
describe('Contentstack User Session api Test', () => {
1313
it('User login wrong credentials', done => {
14-
contentstackClient().login({ email: process.env.EMAIL, password: process.env.PASSWORDWRONG })
14+
contentstackClient().login({ email: process.env.EMAIL, password: process.env.PASSWORD })
1515
.then((response) => {
1616
console.log(response)
1717
done()

test/unit/ContentstackClient-test.js

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import axios from 'axios'
22
import httpAdapter from 'axios/lib/adapters/http'
33
import ContentstackClient from '../../lib/contentstackClient'
44
import { expect } from 'chai'
5-
import { describe, it, after, beforeEach } from 'mocha'
6-
import nock from 'nock'
5+
import { describe, it, beforeEach } from 'mocha'
6+
import MockAdapter from 'axios-mock-adapter'
77
var host = 'http://localhost/'
88

99
describe('Contentstack Client', () => {
@@ -12,10 +12,6 @@ describe('Contentstack Client', () => {
1212
axios.defaults.host = host
1313
axios.defaults.adapter = httpAdapter
1414
})
15-
after('all', function () {
16-
nock.restore()
17-
nock.cleanAll()
18-
})
1915

2016
it('Contentstack Client Object successful', done => {
2117
var contentstackClient = ContentstackClient({ http: axios })
@@ -24,30 +20,12 @@ describe('Contentstack Client', () => {
2420
})
2521

2622
it('Contentstack Client login success', done => {
27-
nock(host)
28-
.post('/user-session', {})
29-
.reply(200, {
30-
user: {
31-
authtoken: 'Test Auth'
32-
}
33-
})
34-
ContentstackClient({ http: axios })
35-
.login()
36-
.then((response) => {
37-
expect(response.user.authtoken).to.be.equal('Test Auth')
38-
done()
39-
})
40-
.catch(done)
41-
})
42-
43-
it('Contentstack Client login success', done => {
44-
nock(host)
45-
.post('/user-session', {})
46-
.reply(200, {
47-
user: {
48-
authtoken: 'Test Auth'
49-
}
50-
})
23+
var mock = new MockAdapter(axios)
24+
mock.onPost('/user-session').reply(200, {
25+
user: {
26+
authtoken: 'Test Auth'
27+
}
28+
})
5129
ContentstackClient({ http: axios })
5230
.login()
5331
.then((response) => {
@@ -58,13 +36,12 @@ describe('Contentstack Client', () => {
5836
})
5937

6038
it('Contentstack Client get user info', done => {
61-
nock(host)
62-
.get('/user')
63-
.reply(200, {
64-
user: {
65-
uid: 'test uid'
66-
}
67-
})
39+
var mock = new MockAdapter(axios)
40+
mock.onGet('/user').reply(200, {
41+
user: {
42+
uid: 'test uid'
43+
}
44+
})
6845
ContentstackClient({ http: axios })
6946
.getUser()
7047
.then((user) => {
@@ -75,11 +52,10 @@ describe('Contentstack Client', () => {
7552
})
7653

7754
it('Contentstack Client Logout with Authtoken', done => {
78-
nock(host)
79-
.delete('/user-session')
80-
.reply(200, {
81-
notice: 'You\'ve logged out successfully'
82-
})
55+
var mock = new MockAdapter(axios)
56+
mock.onDelete('/user-session').reply(200, {
57+
notice: 'You\'ve logged out successfully'
58+
})
8359
ContentstackClient({ http: axios })
8460
.logout('Test Auth')
8561
.then((response) => {
@@ -90,11 +66,10 @@ describe('Contentstack Client', () => {
9066
})
9167

9268
it('Contentstack Client Logout', done => {
93-
nock(host)
94-
.delete('/user-session')
95-
.reply(200, {
96-
notice: 'You\'ve logged out successfully'
97-
})
69+
var mock = new MockAdapter(axios)
70+
mock.onDelete('/user-session').reply(200, {
71+
notice: 'You\'ve logged out successfully'
72+
})
9873
axios.defaults.headers = {
9974
common: {
10075
authtoken: 'Authtoken'

test/unit/Util-test.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,28 @@ describe('Get User Agent', () => {
2121
platform: 'MacIntel'
2222
}
2323
}
24-
const userAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
25-
expect(userAgent.match(headerRegEx).length).to.be.equal(5)
26-
expect(userAgent.indexOf('platform browser')).to.not.equal(-1)
27-
expect(userAgent.indexOf('os macOS;')).to.not.equal(-1)
24+
const macUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
25+
expect(macUserAgent.match(headerRegEx).length).to.be.equal(5)
26+
expect(macUserAgent.indexOf('platform browser')).to.not.equal(-1)
27+
expect(macUserAgent.indexOf('os macOS;')).to.not.equal(-1)
28+
29+
global.window.navigator.platform = 'Windows'
30+
const windowsUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
31+
expect(windowsUserAgent.match(headerRegEx).length).to.be.equal(5)
32+
expect(windowsUserAgent.indexOf('platform browser')).to.not.equal(-1)
33+
expect(windowsUserAgent.indexOf('os Windows;')).to.not.equal(-1)
34+
35+
global.window.navigator = { userAgent: 'Android' }
36+
const androidUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
37+
expect(androidUserAgent.match(headerRegEx).length).to.be.equal(5)
38+
expect(androidUserAgent.indexOf('platform browser')).to.not.equal(-1)
39+
expect(androidUserAgent.indexOf('os Android;')).to.not.equal(-1)
40+
41+
global.window.navigator = { platform: 'Linux' }
42+
const linuxUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
43+
expect(linuxUserAgent.match(headerRegEx).length).to.be.equal(5)
44+
expect(linuxUserAgent.indexOf('platform browser')).to.not.equal(-1)
45+
expect(linuxUserAgent.indexOf('os Linux;')).to.not.equal(-1)
2846
done()
2947

3048
getUserAgentRewireApi.__ResetDependency__('isNode')
@@ -40,7 +58,6 @@ describe('Get User Agent', () => {
4058
global.window = {}
4159

4260
const userAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
43-
4461
expect(userAgent.match(headerRegEx).length).to.be.equal(3)
4562
expect(userAgent.indexOf('platform browser')).to.equal(-1)
4663
expect(userAgent.indexOf('os macOS;')).to.equal(-1)

test/unit/apps-test.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,18 @@ describe('Contentstack apps test', () => {
147147
installations.items.forEach(installation => {
148148
checkInstallation(installation)
149149
})
150-
done()
151150
})
152151
.catch(done)
152+
153+
// Failing test
154+
mock.onGet(`manifests/${uid}/installations`).reply(400, {})
155+
makeApp({ data: { uid } })
156+
.listInstallations()
157+
.then()
158+
.catch((error) => {
159+
expect(error).to.not.equal(null)
160+
done()
161+
})
153162
})
154163

155164
it('app install test', done => {
@@ -170,9 +179,18 @@ describe('Contentstack apps test', () => {
170179
expect(installation.target.uid).to.be.equal(installationMock.target.uid)
171180
expect(installation.organization_uid).to.be.equal(installationMock.organization_uid)
172181
expect(installation.uid).to.be.equal(installationMock.uid)
173-
done()
174182
})
175183
.catch(done)
184+
185+
// Failing test
186+
mock.onPost(`/manifests/${uid}/install`).reply(400, {})
187+
makeApp({ data: { uid } })
188+
.install({ targetUid, targetType })
189+
.then()
190+
.catch((error) => {
191+
expect(error).to.not.equal(null)
192+
done()
193+
})
176194
})
177195

178196
it('app upgrade test', done => {
@@ -193,9 +211,18 @@ describe('Contentstack apps test', () => {
193211
expect(installation.target.uid).to.be.equal(installationMock.target.uid)
194212
expect(installation.organization_uid).to.be.equal(installationMock.organization_uid)
195213
expect(installation.uid).to.be.equal(installationMock.uid)
196-
done()
197214
})
198215
.catch(done)
216+
217+
// Failing test
218+
mock.onPut(`/manifests/${uid}/reinstall`).reply(400, {})
219+
makeApp({ data: { uid } })
220+
.upgrade({ targetUid, targetType })
221+
.then()
222+
.catch((error) => {
223+
expect(error).to.not.equal(null)
224+
done()
225+
})
199226
})
200227

201228
it('test fetch request for app uid', (done) => {

0 commit comments

Comments
 (0)