Skip to content

Commit 27bf6a6

Browse files
Added test cases for utilities
1 parent 6530c9b commit 27bf6a6

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

packages/contentstack-utilities/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@
1717
"@contentstack/management": "^1.2.1",
1818
"inquirer": "^8.0.0",
1919
"ora": "^5.4.0"
20+
},
21+
"devDependencies": {
22+
"chai": "^4.3.4",
23+
"mocha": "^8.3.2",
24+
"nyc": "^15.1.0",
25+
"sinon": "^10.0.0"
2026
}
2127
}

packages/contentstack-utilities/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function chooseOrganization(displayMessage, region) {
3030
choices: orgList
3131
}
3232
inquirer.prompt(inquirerConfig).then(({chosenOrganization}) => {
33+
debugger
3334
resolve({orgUid: orgMap[chosenOrganization], orgName: chosenOrganization})
3435
})
3536
} catch (error) {
@@ -68,5 +69,6 @@ function chooseStack(organizationId, displayMessage, region) {
6869

6970
module.exports = {
7071
chooseOrganization,
71-
chooseStack
72+
chooseStack,
73+
orgClass
7274
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const expect = require('chai').expect
2+
const sinon = require('sinon')
3+
const inquirer = require('inquirer')
4+
const { chooseOrganization, chooseStack, orgClass } = require('../src/index.js')
5+
6+
describe('Tests for utilities', () => {
7+
// tests the base case for organization selector
8+
it("test chooseOrganization", async function () {
9+
sinon.stub(inquirer, "prompt").callsFake(() => { return new Promise(resolve => resolve({ chosenOrganization: 'org1' })) })
10+
sinon.stub(orgClass.prototype, "managementAPIClient").get(() => {
11+
return {
12+
organization: function() {
13+
return {
14+
fetchAll: function() {
15+
return new Promise(resolve => resolve({
16+
items: [{
17+
name: 'org1',
18+
uid: 'org1'
19+
}, {
20+
name: 'org2',
21+
uid: 'org2'
22+
}]
23+
}))
24+
}
25+
}
26+
}
27+
}
28+
})
29+
let chosenOrganization = await chooseOrganization()
30+
expect(chosenOrganization.orgName).to.equal('org1')
31+
expect(chosenOrganization.orgUid).to.equal('org1')
32+
33+
// restore stubs
34+
inquirer.prompt.restore()
35+
})
36+
37+
// tests the base case for stack selector
38+
it ("test chooseStack", async () => {
39+
sinon.stub(inquirer, "prompt").callsFake(() => { return new Promise(resolve => resolve({ chosenStack: 'stack1' })) })
40+
sinon.stub(orgClass.prototype, "managementAPIClient").get(() => {
41+
return {
42+
stack: function() {
43+
return {
44+
query: function() {
45+
return {
46+
find: function() {
47+
return Promise.resolve({
48+
items: [{
49+
name: 'stack1',
50+
api_key: 'stack1'
51+
}, {
52+
name: 'stack2',
53+
api_key: 'stack2'
54+
}]
55+
})
56+
}
57+
}
58+
}
59+
}
60+
}
61+
}
62+
})
63+
let chosenStack = await chooseStack()
64+
expect(chosenStack.name).to.equal('stack1')
65+
expect(chosenStack.api_key).to.equal('stack1')
66+
67+
// restore stubs
68+
inquirer.prompt.restore()
69+
})
70+
})

0 commit comments

Comments
 (0)