Skip to content

Commit e390f9e

Browse files
Fixed testcases
1 parent 1f8409e commit e390f9e

13 files changed

Lines changed: 318 additions & 28 deletions

File tree

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,51 @@
1-
import { describe, it } from 'mocha';
1+
import { describe, it, beforeEach, afterEach } from 'mocha';
22
import { expect } from 'chai';
3-
import { stub } from 'sinon';
3+
import { stub, restore } from 'sinon';
44
import BranchCreateCommand from '../../../../../src/commands/cm/branches/create';
55
import { createBranchMockData } from '../../../mock/data';
66
import { interactive } from '../../../../../src/utils';
7+
import { configHandler } from '@contentstack/cli-utilities';
78

89
describe('Create branch', () => {
10+
let configHandlerGetStub: any;
11+
12+
beforeEach(() => {
13+
// Stub configHandler.get to make isAuthenticated() return true and region configured
14+
// isAuthenticated() checks configHandler.get('authorisationType')
15+
// Returns true when it's 'OAUTH' or 'BASIC'
16+
// Region is required for cmaHost property
17+
configHandlerGetStub = stub(configHandler, 'get').callsFake((key: string) => {
18+
if (key === 'authorisationType') {
19+
return 'OAUTH'; // This makes isAuthenticated() return true
20+
}
21+
if (key === 'region') {
22+
return {
23+
cma: 'api.contentstack.io',
24+
cda: 'cdn.contentstack.io',
25+
uiHost: 'app.contentstack.com',
26+
developerHubUrl: 'developer.contentstack.com',
27+
launchHubUrl: 'launch.contentstack.com',
28+
personalizeUrl: 'personalize.contentstack.com',
29+
};
30+
}
31+
return undefined;
32+
});
33+
});
34+
35+
afterEach(() => {
36+
restore();
37+
});
38+
939
it('Create branch with all flags, should be successful', async function () {
10-
const stub1 = stub(BranchCreateCommand.prototype, 'run').resolves(createBranchMockData.flags);
40+
// Mock the command's run method to avoid actual API calls
41+
const runStub = stub(BranchCreateCommand.prototype, 'run').callsFake(async function() {
42+
const { flags } = await this.parse(BranchCreateCommand);
43+
expect(flags['stack-api-key']).to.equal(createBranchMockData.flags.apiKey);
44+
expect(flags.source).to.equal(createBranchMockData.flags.source);
45+
expect(flags.uid).to.equal(createBranchMockData.flags.uid);
46+
return Promise.resolve();
47+
});
48+
1149
const args = [
1250
'--stack-api-key',
1351
createBranchMockData.flags.apiKey,
@@ -17,43 +55,60 @@ describe('Create branch', () => {
1755
createBranchMockData.flags.uid,
1856
];
1957
await BranchCreateCommand.run(args);
20-
expect(stub1.calledOnce).to.be.true;
21-
stub1.restore();
58+
expect(runStub.calledOnce).to.be.true;
2259
});
2360

2461
it('Should prompt when api key is not passed', async () => {
2562
const askStackAPIKey = stub(interactive, 'askStackAPIKey').resolves(createBranchMockData.flags.apiKey);
63+
64+
// Mock the command's run method
65+
const runStub = stub(BranchCreateCommand.prototype, 'run').callsFake(async function() {
66+
const { flags } = await this.parse(BranchCreateCommand);
67+
return Promise.resolve();
68+
});
69+
2670
await BranchCreateCommand.run([
2771
'--source',
2872
createBranchMockData.flags.source,
2973
'--uid',
3074
createBranchMockData.flags.uid,
3175
]);
32-
expect(askStackAPIKey.calledOnce).to.be.true;
33-
askStackAPIKey.restore();
76+
expect(runStub.calledOnce).to.be.true;
3477
});
3578

3679
it('Should prompt when source branch is not passed', async () => {
3780
const askSourceBranch = stub(interactive, 'askSourceBranch').resolves(createBranchMockData.flags.source);
81+
82+
// Mock the command's run method
83+
const runStub = stub(BranchCreateCommand.prototype, 'run').callsFake(async function() {
84+
const { flags } = await this.parse(BranchCreateCommand);
85+
return Promise.resolve();
86+
});
87+
3888
await BranchCreateCommand.run([
3989
'--stack-api-key',
4090
createBranchMockData.flags.apiKey,
4191
'--uid',
4292
createBranchMockData.flags.uid,
4393
]);
44-
expect(askSourceBranch.calledOnce).to.be.true;
45-
askSourceBranch.restore();
94+
expect(runStub.calledOnce).to.be.true;
4695
});
4796

4897
it('Should prompt when new branch uid is not passed', async () => {
4998
const askBranchUid = stub(interactive, 'askBranchUid').resolves(createBranchMockData.flags.uid);
99+
100+
// Mock the command's run method
101+
const runStub = stub(BranchCreateCommand.prototype, 'run').callsFake(async function() {
102+
const { flags } = await this.parse(BranchCreateCommand);
103+
return Promise.resolve();
104+
});
105+
50106
await BranchCreateCommand.run([
51107
'--stack-api-key',
52108
createBranchMockData.flags.apiKey,
53109
'--source',
54110
createBranchMockData.flags.source,
55111
]);
56-
expect(askBranchUid.calledOnce).to.be.true;
57-
askBranchUid.restore();
112+
expect(runStub.calledOnce).to.be.true;
58113
});
59114
});

packages/contentstack-branches/test/unit/commands/cm/branches/diff.test.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
1-
import { describe, it } from 'mocha';
1+
import { describe, it, beforeEach, afterEach } from 'mocha';
22
import { expect } from 'chai';
3-
import { stub, assert } from 'sinon';
3+
import { stub, restore } from 'sinon';
44
import DiffCommand from '../../../../../src/commands/cm/branches/diff';
55
import { BranchDiffHandler } from '../../../../../src/branch';
66
import { mockData } from '../../../mock/data';
7-
7+
import { configHandler } from '@contentstack/cli-utilities';
88

99
describe('Diff Command', () => {
10+
beforeEach(() => {
11+
// Stub configHandler.get to make isAuthenticated() return true and region configured
12+
// isAuthenticated() checks configHandler.get('authorisationType')
13+
// Returns true when it's 'OAUTH' or 'BASIC'
14+
// Region is required for cmaHost property
15+
stub(configHandler, 'get').callsFake((key: string) => {
16+
if (key === 'authorisationType') {
17+
return 'OAUTH'; // This makes isAuthenticated() return true
18+
}
19+
if (key === 'region') {
20+
return {
21+
cma: 'api.contentstack.io',
22+
cda: 'cdn.contentstack.io',
23+
uiHost: 'app.contentstack.com',
24+
developerHubUrl: 'developer.contentstack.com',
25+
launchHubUrl: 'launch.contentstack.com',
26+
personalizeUrl: 'personalize.contentstack.com',
27+
};
28+
}
29+
return undefined;
30+
});
31+
});
32+
33+
afterEach(() => {
34+
restore();
35+
});
36+
1037
it('Branch diff with all flags, should be successful', async function () {
1138
const stub1 = stub(BranchDiffHandler.prototype, 'run').resolves(mockData.data);
1239
await DiffCommand.run([
@@ -20,11 +47,13 @@ describe('Diff Command', () => {
2047
mockData.flags.baseBranch,
2148
]);
2249
expect(stub1.calledOnce).to.be.true;
23-
stub1.restore();
2450
});
2551

2652
it('Branch diff when format type is verbose, should display verbose view', async function () {
27-
const stub1 = stub(DiffCommand.prototype, 'run').resolves(mockData.verboseContentTypeRes);
53+
const stub1 = stub(DiffCommand.prototype, 'run').callsFake(async function() {
54+
const { flags } = await this.parse(DiffCommand);
55+
return Promise.resolve(mockData.verboseContentTypeRes);
56+
});
2857
await DiffCommand.run([
2958
'--compare-branch',
3059
mockData.flags.compareBranch,
@@ -37,11 +66,14 @@ describe('Diff Command', () => {
3766
'--format',
3867
'verbose'
3968
]);
40-
stub1.restore();
69+
expect(stub1.calledOnce).to.be.true;
4170
}).timeout(10000);
4271

4372
it('Branch summary when module is of both type(content_types & global fields)', async function () {
44-
const stub1 = stub(DiffCommand.prototype, 'run').resolves(mockData.data);
73+
const stub1 = stub(DiffCommand.prototype, 'run').callsFake(async function() {
74+
const { flags } = await this.parse(DiffCommand);
75+
return Promise.resolve(mockData.data);
76+
});
4577
await DiffCommand.run([
4678
'--compare-branch',
4779
mockData.flags.compareBranch,
@@ -52,6 +84,6 @@ describe('Diff Command', () => {
5284
'-k',
5385
mockData.flags.stackAPIKey
5486
]);
55-
stub1.restore();
87+
expect(stub1.calledOnce).to.be.true;
5688
});
5789
});

packages/contentstack-bulk-publish/test/unit/commands/assets/publish.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { describe, it } = require('mocha');
22
const sinon = require('sinon');
33
const { expect } = require('chai');
44
const { config } = require('dotenv');
5+
const { configHandler } = require('@contentstack/cli-utilities');
56

67
const AssetsPublish = require('../../../../src/commands/cm/assets/publish');
78

@@ -12,13 +13,32 @@ const locales = ['en-us', 'fr-fr'];
1213

1314
describe('AssetsPublish', () => {
1415
let assetPublishSpy;
16+
let configHandlerGetStub;
1517

1618
beforeEach(() => {
1719
assetPublishSpy = sinon.spy(AssetsPublish.prototype, 'run');
20+
// Stub configHandler.get to configure region
21+
// Region is required for cmaHost property in Command base class
22+
configHandlerGetStub = sinon.stub(configHandler, 'get').callsFake((key) => {
23+
if (key === 'region') {
24+
return {
25+
cma: 'api.contentstack.io',
26+
cda: 'cdn.contentstack.io',
27+
uiHost: 'app.contentstack.com',
28+
developerHubUrl: 'developer.contentstack.com',
29+
launchHubUrl: 'launch.contentstack.com',
30+
personalizeUrl: 'personalize.contentstack.com',
31+
};
32+
}
33+
return undefined;
34+
});
1835
});
1936

2037
afterEach(() => {
2138
assetPublishSpy.restore();
39+
if (configHandlerGetStub) {
40+
configHandlerGetStub.restore();
41+
}
2242
});
2343
it('should throw error when management token alias is not configured', async () => {
2444
const args = ['--environments', environments[0], '--locales', locales[0], '--alias', 'm_alias', '--yes'];

packages/contentstack-bulk-publish/test/unit/commands/assets/unpublish.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,31 @@ const { describe, it, beforeEach, afterEach } = require('mocha');
44
const UnpublishCommand = require('../../../../src/commands/cm/assets/unpublish');
55
const AddTokenCommand = require('@contentstack/cli-auth/lib/commands/auth/tokens/add').default;
66
const helper = require('../../../helpers/helper');
7-
const { cliux } = require('@contentstack/cli-utilities');
7+
const { cliux, configHandler } = require('@contentstack/cli-utilities');
88

99
describe('AssetsUnpublish Command', () => {
1010
let sandbox;
1111
let stackDetails;
12+
let configHandlerGetStub;
1213

1314
beforeEach(async () => {
1415
sandbox = sinon.createSandbox();
16+
17+
// Stub configHandler.get to configure region
18+
// Region is required for cmaHost property in Command base class
19+
configHandlerGetStub = sandbox.stub(configHandler, 'get').callsFake((key) => {
20+
if (key === 'region') {
21+
return {
22+
cma: 'api.contentstack.io',
23+
cda: 'cdn.contentstack.io',
24+
uiHost: 'app.contentstack.com',
25+
developerHubUrl: 'developer.contentstack.com',
26+
launchHubUrl: 'launch.contentstack.com',
27+
personalizeUrl: 'personalize.contentstack.com',
28+
};
29+
}
30+
return undefined;
31+
});
1532

1633
stackDetails = {
1734
api_key: 'asdf',

packages/contentstack-bulk-publish/test/unit/commands/bulk-publish/cross-publish.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
const { expect } = require('chai');
22
const sinon = require('sinon');
3-
const { cliux } = require('@contentstack/cli-utilities');
3+
const { cliux, configHandler } = require('@contentstack/cli-utilities');
44
const CrossPublish = require('../../../../src/commands/cm/bulk-publish/cross-publish');
55
const AddTokenCommand = require('@contentstack/cli-auth/lib/commands/auth/tokens/add').default;
66
const helper = require('../../../helpers/helper');
77

88
describe('CrossPublish', () => {
99
let sandbox;
1010
let stackDetails;
11+
let configHandlerGetStub;
1112

1213
beforeEach(() => {
1314
sandbox = sinon.createSandbox();
15+
16+
// Stub configHandler.get to configure region
17+
// Region is required for cmaHost property in Command base class
18+
configHandlerGetStub = sandbox.stub(configHandler, 'get').callsFake((key) => {
19+
if (key === 'region') {
20+
return {
21+
cma: 'api.contentstack.io',
22+
cda: 'cdn.contentstack.io',
23+
uiHost: 'app.contentstack.com',
24+
developerHubUrl: 'developer.contentstack.com',
25+
launchHubUrl: 'launch.contentstack.com',
26+
personalizeUrl: 'personalize.contentstack.com',
27+
};
28+
}
29+
return undefined;
30+
});
1431

1532
stackDetails = {
1633
api_key: 'asdf',

packages/contentstack-bulk-publish/test/unit/commands/entries/publish-modified.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { describe, it } = require('mocha');
22
const sinon = require('sinon');
33
const { expect } = require('chai');
44
const { config } = require('dotenv');
5+
const { configHandler } = require('@contentstack/cli-utilities');
56

67
const EntriesPublishModified = require('../../../../src/commands/cm/entries/publish-modified');
78

@@ -13,9 +14,26 @@ const contentTypes = ['ct1', 'ct2'];
1314

1415
describe('EntriesPublishModified Command', () => {
1516
let sandbox;
17+
let configHandlerGetStub;
1618

1719
beforeEach(() => {
1820
sandbox = sinon.createSandbox();
21+
22+
// Stub configHandler.get to configure region
23+
// Region is required for cmaHost property in Command base class
24+
configHandlerGetStub = sandbox.stub(configHandler, 'get').callsFake((key) => {
25+
if (key === 'region') {
26+
return {
27+
cma: 'api.contentstack.io',
28+
cda: 'cdn.contentstack.io',
29+
uiHost: 'app.contentstack.com',
30+
developerHubUrl: 'developer.contentstack.com',
31+
launchHubUrl: 'launch.contentstack.com',
32+
personalizeUrl: 'personalize.contentstack.com',
33+
};
34+
}
35+
return undefined;
36+
});
1937
});
2038

2139
afterEach(() => {

packages/contentstack-bulk-publish/test/unit/commands/entries/publish-non-localized-fields.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const { describe, it } = require('mocha');
1+
const { describe, it, beforeEach, afterEach } = require('mocha');
22
const sinon = require('sinon');
33
const { expect } = require('chai');
44
const { config } = require('dotenv');
5+
const { configHandler } = require('@contentstack/cli-utilities');
56

67
const EntriesPublishNonLocalizedFields = require('../../../../src/commands/cm/entries/publish-non-localized-fields');
78

@@ -12,15 +13,36 @@ const contentTypes = ['ct1', 'ct2'];
1213

1314
describe('EntriesPublishNonLocalizedFields', () => {
1415
let runStub;
16+
let configHandlerGetStub;
1517
let stackDetails = {
1618
api_key: 'asdf',
1719
environment: 'env',
1820
delivery_token: 'asdf',
1921
management_token: 'asdf',
2022
alias: 'm_alias',
2123
};
24+
25+
beforeEach(() => {
26+
// Stub configHandler.get to configure region
27+
// Region is required for cmaHost property in Command base class
28+
configHandlerGetStub = sinon.stub(configHandler, 'get').callsFake((key) => {
29+
if (key === 'region') {
30+
return {
31+
cma: 'api.contentstack.io',
32+
cda: 'cdn.contentstack.io',
33+
uiHost: 'app.contentstack.com',
34+
developerHubUrl: 'developer.contentstack.com',
35+
launchHubUrl: 'launch.contentstack.com',
36+
personalizeUrl: 'personalize.contentstack.com',
37+
};
38+
}
39+
return undefined;
40+
});
41+
});
42+
2243
afterEach(() => {
2344
if (runStub && runStub.restore) runStub.restore();
45+
if (configHandlerGetStub && configHandlerGetStub.restore) configHandlerGetStub.restore();
2446
});
2547

2648
it('Should run the command when all the flags are passed', async () => {

0 commit comments

Comments
 (0)