Skip to content

Commit 0f76f88

Browse files
update audit test cases
1 parent 8c40736 commit 0f76f88

9 files changed

Lines changed: 57 additions & 108 deletions

File tree

packages/contentstack-audit/test/unit/audit-base-command.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe('AuditBaseCommand class', () => {
9898
.stub(winston, 'createLogger', (stub) => stub.callsFake(createMockWinstonLogger))
9999
.stub(fs, 'mkdirSync', (stub) => stub.returns(undefined))
100100
.stub(fs, 'writeFileSync', (stub) => stub.returns(undefined))
101-
.stub(ux, 'table', (stub) => stub.returns(undefined))
101+
.stub(cliux, 'table', (stub) => stub.returns(undefined))
102102
.stub(ux.action, 'stop', (stub) => stub.returns(undefined))
103103
.stub(ux.action, 'start', (stub) => stub.returns(undefined))
104104
.stub(cliux, 'inquire', (stub) => stub.returns(resolve(__dirname, 'mock', 'contents')))
@@ -143,10 +143,9 @@ describe('AuditBaseCommand class', () => {
143143
.stub(fs, 'mkdirSync', (stub) => stub.returns(undefined))
144144
.stub(fs, 'writeFileSync', (stub) => stub.returns(undefined))
145145
.stub(AuditBaseCommand.prototype, 'showOutputOnScreenWorkflowsAndExtension', (stub) => stub.returns(undefined))
146-
.stub(ux, 'table', (stub) => stub.callsFake((...args: any) => {
146+
.stub(cliux, 'table', (stub) => stub.callsFake((...args: any) => {
147147
args[1].missingRefs.get({ missingRefs: ['gf_0'] });
148148
}))
149-
.stub(AuditBaseCommand.prototype, 'showOutputOnScreenWorkflowsAndExtension', (stub) => stub.returns(undefined))
150149
.stub(ux.action, 'stop', (stub) => stub.returns(undefined))
151150
.stub(ux.action, 'start', (stub) => stub.returns(undefined))
152151
.stub(AuditBaseCommand.prototype, 'scanAndFix', (stub) => stub.returns({
@@ -172,7 +171,6 @@ describe('AuditBaseCommand class', () => {
172171
missingFieldRules: {},
173172
missingMultipleFields: {}
174173
}))
175-
.stub(fs, 'createBackUp', (stub) => stub.returns(undefined))
176174
.stub(fs, 'createWriteStream', (stub) => stub.callsFake(() => new PassThrough()))
177175
.stub(AuditBaseCommand.prototype, 'createBackUp', (stub) => stub.returns(undefined))
178176
.it('should print missing ref and fix status on table formate', async (ctx) => {

packages/contentstack-audit/test/unit/commands/fix.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import fs from 'fs';
22
import winston from 'winston';
33
import { expect } from 'chai';
4-
import { runCommand } from '@oclif/test';
54
import * as sinon from 'sinon';
65
import { FileTransportInstance } from 'winston/lib/winston/transports';
76

7+
import AuditFix from '../../../src/commands/cm/stacks/audit/fix';
88
import { AuditBaseCommand } from '../../../src/audit-base-command';
99

1010
describe('AuditFix command', () => {
@@ -19,18 +19,16 @@ describe('AuditFix command', () => {
1919
sinon.stub(fs, 'rmSync').callsFake(() => {});
2020
sinon.stub(winston.transports, 'File').callsFake(() => fsTransport);
2121
sinon.stub(winston, 'createLogger').callsFake(() => ({ log: () => {}, error: () => {} } as any));
22-
startSpy = sinon.stub(AuditBaseCommand.prototype, 'start').callsFake(() => {
23-
return Promise.resolve(true);
24-
});
22+
startSpy = sinon.stub(AuditBaseCommand.prototype, 'start').resolves(true as any);
2523
});
2624

2725
afterEach(() => {
2826
sinon.restore();
2927
});
3028

3129
it('should trigger AuditBaseCommand start method', async () => {
32-
await runCommand(['cm:stacks:audit:fix', '-d', 'data-dir'], { root: process.cwd() });
33-
expect(startSpy.args).to.be.eql([['cm:stacks:audit']]);
30+
await AuditFix.prototype.run.call({ flags: {}, start: startSpy, sharedConfig: {} } as any);
31+
expect(startSpy.args).to.be.eql([['cm:stacks:audit:fix']]);
3432
});
3533
});
3634
});

packages/contentstack-audit/test/unit/modules/content-types.test.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,10 @@ describe('Content types', () => {
145145
describe('writeFixContent method', () => {
146146
fancy
147147
.stdout({ print: process.env.PRINT === 'true' || false })
148-
.stub(fs, 'writeFileSync', (stub) => stub.returns(undefined))
149148
.stub(cliux, 'confirm', (stub) => stub.resolves(true))
150149
.it('should not write the file', async () => {
150+
const fsSpy = sinon.stub(fs, 'writeFileSync').returns(undefined);
151151
const ctInstance = new ContentType({ ...constructorParam, fix: true });
152-
const fsSpy = sinon.spy(fs, 'writeFileSync');
153152
await ctInstance.writeFixContent();
154153
expect(fsSpy.callCount).to.be.equals(1);
155154
});
@@ -169,24 +168,20 @@ describe('Content types', () => {
169168
describe('lookForReference method', () => {
170169
fancy
171170
.stdout({ print: process.env.PRINT === 'true' || false })
172-
.stub(ContentType.prototype, 'validateReferenceField', (stub) => stub.returns([]))
173-
.stub(ContentType.prototype, 'validateGlobalField', (stub) => stub.returns(undefined))
174-
.stub(ContentType.prototype, 'validateJsonRTEFields', (stub) => stub.returns([]))
175-
.stub(ContentType.prototype, 'validateGroupField', (stub) => stub.returns([]))
176-
.stub(ContentType.prototype, 'validateModularBlocksField', (stub) => stub.returns([]))
177171
.it('should call all CT type audit methods', async () => {
172+
const validateReferenceFieldSpy = sinon.stub(ContentType.prototype, 'validateReferenceField').returns([] as any);
173+
const validateGlobalFieldSpy = sinon.stub(ContentType.prototype, 'validateGlobalField').resolves();
174+
const validateJsonRTEFieldsSpy = sinon.stub(ContentType.prototype, 'validateJsonRTEFields').returns([] as any);
175+
const validateGroupFieldSpy = sinon.stub(ContentType.prototype, 'validateGroupField').resolves();
176+
const validateModularBlocksFieldSpy = sinon.stub(ContentType.prototype, 'validateModularBlocksField').resolves();
177+
178178
const ctInstance = new (class TempClass extends ContentType {
179179
constructor() {
180180
super(constructorParam);
181181
this.currentUid = 'test';
182182
this.missingRefs['test'] = [];
183183
}
184184
})();
185-
const validateReferenceFieldSpy = sinon.spy(ctInstance, 'validateReferenceField');
186-
const validateGlobalFieldSpy = sinon.spy(ctInstance, 'validateGlobalField');
187-
const validateJsonRTEFieldsSpy = sinon.spy(ctInstance, 'validateJsonRTEFields');
188-
const validateModularBlocksFieldSpy = sinon.spy(ctInstance, 'validateModularBlocksField');
189-
const validateGroupFieldSpy = sinon.spy(ctInstance, 'validateGroupField');
190185

191186
// NOTE dummy CT schema
192187
const schema = [
@@ -207,13 +202,12 @@ describe('Content types', () => {
207202

208203
fancy
209204
.stdout({ print: process.env.PRINT === 'true' || false })
210-
.stub(ContentType.prototype, 'runFixOnSchema', (stub) => stub.returns([]))
211205
.it('should call runFixOnSchema method', async () => {
206+
const runFixOnSchemaSpy = sinon.stub(ContentType.prototype, 'runFixOnSchema').returns([]);
212207
const ctInstance = new ContentType({ ...constructorParam, fix: true });
213-
const validateReferenceFieldSpy = sinon.spy(ctInstance, 'runFixOnSchema');
214208
await ctInstance.lookForReference([], { schema: [] } as unknown as CtType);
215209

216-
expect(validateReferenceFieldSpy.callCount).to.be.equals(1);
210+
expect(runFixOnSchemaSpy.callCount).to.be.equals(1);
217211
});
218212
});
219213

packages/contentstack-audit/test/unit/modules/custom-roles.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,26 @@ describe('Custom roles module', () => {
3737

3838
fancy
3939
.stdout({ print: process.env.PRINT === 'true' || false })
40-
.stub(CustomRoles.prototype, 'fixCustomRoleSchema', (stub) => stub.resolves())
4140
.it('should call fixCustomRoleSchema', async () => {
41+
const logSpy = Sinon.stub(CustomRoles.prototype, 'fixCustomRoleSchema').resolves();
4242
const customRoleInstance = new CustomRoles({
4343
...constructorParam,
4444
config: { ...constructorParam.config, branch: 'test' },
4545
fix: true,
4646
});
47-
const logSpy = Sinon.spy(customRoleInstance, 'fixCustomRoleSchema');
4847
await customRoleInstance.run();
4948
expect(logSpy.callCount).to.be.equals(1);
5049
});
5150

5251
fancy
5352
.stdout({ print: process.env.PRINT === 'true' || false })
54-
.stub(CustomRoles.prototype, 'writeFixContent', (stub) => stub.resolves())
5553
.it('should call writeFixContent', async () => {
54+
const logSpy = Sinon.stub(CustomRoles.prototype, 'writeFixContent').resolves();
5655
const customRoleInstance = new CustomRoles({
5756
...constructorParam,
5857
config: { ...constructorParam.config, branch: 'test' },
5958
fix: true,
6059
});
61-
const logSpy = Sinon.spy(customRoleInstance, 'writeFixContent');
6260
await customRoleInstance.run();
6361
expect(logSpy.callCount).to.be.equals(1);
6462
});

packages/contentstack-audit/test/unit/modules/entries.test.ts

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,13 @@ describe('Entries module', () => {
3333
gfSchema: cloneDeep(require('../mock/contents/global_fields/globalfields.json')),
3434
config: Object.assign(config, { basePath: resolve(__dirname, '..', 'mock', 'contents'), flags: {} }),
3535
};
36-
37-
// Mock the logger for all tests
3836
Sinon.stub(require('@contentstack/cli-utilities'), 'log').value(mockLogger);
39-
});
40-
41-
before(() => {
4237
ctStub = Sinon.stub(ContentType.prototype, 'run').resolves({ ct1: [{}] });
4338
gfStub = Sinon.stub(GlobalField.prototype, 'run').resolves({ gf1: [{}] });
4439
});
4540

46-
after(() => {
47-
Sinon.restore(); // Clears Sinon spies/stubs/mocks
48-
ctStub.restore();
49-
gfStub.restore();
41+
afterEach(() => {
42+
Sinon.restore();
5043
});
5144

5245
describe('run method', () => {
@@ -71,12 +64,12 @@ describe('Entries module', () => {
7164
.stub(Entries.prototype, 'fixPrerequisiteData', (stub) => stub.resolves())
7265
.stub(Entries.prototype, 'writeFixContent', (stub) => stub.resolves())
7366
.stub(Entries.prototype, 'lookForReference', (stub) => stub.resolves())
74-
.stub(Entries.prototype, 'locales', [{ code: 'en-us' }] as any)
7567
.it('should return missing refs', async () => {
7668
const ctInstance = new (class Class extends Entries {
7769
constructor() {
7870
super(constructorParam);
7971
this.missingRefs['test-entry-id'] = [{ uid: 'test', treeStr: 'gf_0' }];
72+
this.locales = [{ code: 'en-us' }] as any;
8073
}
8174
})();
8275
const missingRefs = await ctInstance.run();
@@ -86,17 +79,13 @@ describe('Entries module', () => {
8679

8780
fancy
8881
.stdout({ print: process.env.PRINT === 'true' || false })
89-
.stub(Entries.prototype, 'prepareEntryMetaData', (stub) => stub.resolves())
90-
.stub(Entries.prototype, 'fixPrerequisiteData', (stub) => stub.resolves())
91-
.stub(Entries.prototype, 'lookForReference', (stub) => stub.resolves())
92-
.stub(Entries.prototype, 'writeFixContent', (stub) => stub.resolves())
93-
.stub(Entries.prototype, 'locales', [{ code: 'en-us' }] as any)
9482
.it('should call prepareEntryMetaData & fixPrerequisiteData methods', async () => {
95-
const prepareEntryMetaData = Sinon.spy(Entries.prototype, 'prepareEntryMetaData');
96-
const fixPrerequisiteData = Sinon.spy(Entries.prototype, 'fixPrerequisiteData');
97-
const lookForReference = Sinon.spy(Entries.prototype, 'lookForReference');
98-
const writeFixContent = Sinon.spy(Entries.prototype, 'writeFixContent');
83+
const prepareEntryMetaData = Sinon.stub(Entries.prototype, 'prepareEntryMetaData').resolves();
84+
const fixPrerequisiteData = Sinon.stub(Entries.prototype, 'fixPrerequisiteData').resolves();
85+
const lookForReference = Sinon.stub(Entries.prototype, 'lookForReference').resolves();
86+
const writeFixContent = Sinon.stub(Entries.prototype, 'writeFixContent').resolves();
9987
const ctInstance = new Entries({ ...constructorParam, fix: true });
88+
ctInstance.locales = [{ code: 'en-us' }] as any;
10089
const missingRefs = await ctInstance.run();
10190
expect((missingRefs as any).missingEntryRefs).to.be.empty;
10291
expect(writeFixContent.callCount).to.be.equals(1);
@@ -126,10 +115,9 @@ describe('Entries module', () => {
126115
describe('writeFixContent method', () => {
127116
fancy
128117
.stdout({ print: process.env.PRINT === 'true' || false })
129-
.stub(fs, 'writeFileSync', (stub) => stub.returns(undefined))
130118
.stub(cliux, 'confirm', (stub) => stub.resolves(true))
131119
.it('should ask confirmation adn write content in given path', async ({}) => {
132-
const writeFileSync = Sinon.spy(fs, 'writeFileSync');
120+
const writeFileSync = Sinon.stub(fs, 'writeFileSync').returns(undefined);
133121
const ctInstance = new Entries({ ...constructorParam, fix: true });
134122
await ctInstance.writeFixContent(resolve(__dirname, '..', 'mock', 'contents'), {});
135123

@@ -138,9 +126,8 @@ describe('Entries module', () => {
138126

139127
fancy
140128
.stdout({ print: process.env.PRINT === 'true' || false })
141-
.stub(fs, 'writeFileSync', (stub) => stub.returns(undefined))
142129
.it("should skip confirmation if 'yes' flag passed", async ({}) => {
143-
const writeFileSync = Sinon.spy(fs, 'writeFileSync');
130+
const writeFileSync = Sinon.stub(fs, 'writeFileSync').returns(undefined);
144131
const ctInstance = new Entries({ ...constructorParam, fix: true });
145132
ctInstance.config.flags.yes = true;
146133
await ctInstance.writeFixContent(resolve(__dirname, '..', 'mock', 'contents'), {});
@@ -154,13 +141,13 @@ describe('Entries module', () => {
154141
describe('lookForReference method', () => {
155142
fancy
156143
.stdout({ print: process.env.PRINT === 'true' || false })
157-
.stub(Entries.prototype, 'runFixOnSchema', (stub) => stub.returns(emptyEntries))
158-
.stub(Entries.prototype, 'validateReferenceField', (stub) => stub.returns([]))
159-
.stub(Entries.prototype, 'validateGlobalField', (stub) => stub.returns(undefined))
160-
.stub(Entries.prototype, 'validateJsonRTEFields', (stub) => stub.returns(undefined))
161-
.stub(Entries.prototype, 'validateModularBlocksField', (stub) => stub.returns(undefined))
162-
.stub(Entries.prototype, 'validateGroupField', (stub) => stub.returns(undefined))
163144
.it('should call datatype specific methods', async ({}) => {
145+
const runFixOnSchema = Sinon.stub(Entries.prototype, 'runFixOnSchema').returns(emptyEntries as any);
146+
const validateReferenceField = Sinon.stub(Entries.prototype, 'validateReferenceField').returns([] as any);
147+
const validateGlobalField = Sinon.stub(Entries.prototype, 'validateGlobalField').resolves();
148+
const validateJsonRTEFields = Sinon.stub(Entries.prototype, 'validateJsonRTEFields').returns([] as any);
149+
const validateModularBlocksField = Sinon.stub(Entries.prototype, 'validateModularBlocksField').resolves();
150+
const validateGroupField = Sinon.stub(Entries.prototype, 'validateGroupField').resolves();
164151
const ctInstance = new (class Class extends Entries {
165152
constructor() {
166153
super({ ...constructorParam, fix: true });
@@ -169,12 +156,6 @@ describe('Entries module', () => {
169156
this.missingMandatoryFields['reference'] = [];
170157
}
171158
})();
172-
const runFixOnSchema = Sinon.spy(ctInstance, 'runFixOnSchema');
173-
const validateReferenceField = Sinon.spy(ctInstance, 'validateReferenceField');
174-
const validateGlobalField = Sinon.spy(ctInstance, 'validateGlobalField');
175-
const validateJsonRTEFields = Sinon.spy(ctInstance, 'validateJsonRTEFields');
176-
const validateModularBlocksField = Sinon.spy(ctInstance, 'validateModularBlocksField');
177-
const validateGroupField = Sinon.spy(ctInstance, 'validateGroupField');
178159
await ctInstance.lookForReference([], { schema } as any, {});
179160

180161
expect(runFixOnSchema.callCount).to.be.equals(1);
@@ -199,10 +180,8 @@ describe('Entries module', () => {
199180

200181
fancy
201182
.stdout({ print: process.env.PRINT === 'true' || false })
202-
.stub(Entries.prototype, 'validateReferenceValues', (stub) => stub.returns(undefined))
203-
204183
.it('should call validateReferenceField method', async ({}) => {
205-
const validateReferenceValues = Sinon.spy(Entries.prototype, 'validateReferenceValues');
184+
const validateReferenceValues = Sinon.stub(Entries.prototype, 'validateReferenceValues').returns(undefined as any);
206185
const ctInstance = new Class();
207186

208187
await ctInstance.validateReferenceField([], ctInstance.ctSchema[3].schema as any, ctInstance.entries as any);
@@ -282,9 +261,8 @@ describe('Entries module', () => {
282261
describe('validateJsonRTEFields method', () => {
283262
fancy
284263
.stdout({ print: process.env.PRINT === 'true' || false })
285-
.stub(Entries.prototype, 'jsonRefCheck', (stub) => stub.returns(undefined))
286264
.it('should do recursive call on validateJsonRTEFields method', async ({}) => {
287-
const jsonRefCheck = Sinon.spy(Entries.prototype, 'jsonRefCheck');
265+
const jsonRefCheck = Sinon.stub(Entries.prototype, 'jsonRefCheck').returns(undefined as any);
288266
const validateJsonRTEFields = Sinon.spy(Entries.prototype, 'validateJsonRTEFields');
289267
const ctInstance = new Entries(constructorParam);
290268
(ctInstance as any).currentUid = 'test-entry';
@@ -302,14 +280,11 @@ describe('Entries module', () => {
302280
describe('validateModularBlocksField method', () => {
303281
fancy
304282
.stdout({ print: process.env.PRINT === 'true' || false })
305-
.stub(Entries.prototype, 'modularBlockRefCheck', (stub) => stub.returns(undefined))
306-
.stub(Entries.prototype, 'lookForReference', (stub) => stub.returns(undefined))
307-
308283
.it(
309284
'should iterate each blocks and call modularBlockRefCheck & lookForReference methods number of blocks exist in the entry times',
310285
async ({}) => {
311-
const modularBlockRefCheck = Sinon.spy(Entries.prototype, 'modularBlockRefCheck');
312-
const lookForReference = Sinon.spy(Entries.prototype, 'lookForReference');
286+
const modularBlockRefCheck = Sinon.stub(Entries.prototype, 'modularBlockRefCheck').returns(undefined as any);
287+
const lookForReference = Sinon.stub(Entries.prototype, 'lookForReference').returns(undefined as any);
313288
const ctInstance = new Entries(constructorParam);
314289
(ctInstance as any).currentUid = 'test-entry';
315290
(ctInstance as any).missingRefs = { 'test-entry': [] };
@@ -334,9 +309,8 @@ describe('Entries module', () => {
334309
describe('validateGroupField method', () => {
335310
fancy
336311
.stdout({ print: process.env.PRINT === 'true' || false })
337-
.stub(Entries.prototype, 'lookForReference', (stub) => stub.returns(undefined))
338312
.it('should call lookForReference method to iterate GroupField schema', async ({}) => {
339-
const lookForReference = Sinon.spy(Entries.prototype, 'lookForReference');
313+
const lookForReference = Sinon.stub(Entries.prototype, 'lookForReference').returns(undefined as any);
340314
const ctInstance = new Entries(constructorParam);
341315
(ctInstance as any).currentUid = 'test-entry';
342316
(ctInstance as any).missingRefs = { 'test-entry': [] };
@@ -349,11 +323,10 @@ describe('Entries module', () => {
349323

350324
fancy
351325
.stdout({ print: process.env.PRINT === 'true' || false })
352-
.stub(Entries.prototype, 'lookForReference', (stub) => stub.returns(undefined))
353326
.it(
354327
'should iterate all group entries and call lookForReference method to iterate GroupField schema',
355328
async ({}) => {
356-
const lookForReference = Sinon.spy(Entries.prototype, 'lookForReference');
329+
const lookForReference = Sinon.stub(Entries.prototype, 'lookForReference').returns(undefined as any);
357330

358331
const ctInstance = new Entries(constructorParam);
359332
(ctInstance as any).currentUid = 'test-entry';
@@ -377,9 +350,8 @@ describe('Entries module', () => {
377350
describe('fixGlobalFieldReferences method', () => {
378351
fancy
379352
.stdout({ print: process.env.PRINT === 'true' || false })
380-
.stub(Entries.prototype, 'runFixOnSchema', (...args: any[]) => args[2])
381353
.it('should call runFixOnSchema for single global field entry', async ({}) => {
382-
const runFixOnSchema = Sinon.spy(Entries.prototype, 'runFixOnSchema');
354+
const runFixOnSchema = Sinon.stub(Entries.prototype, 'runFixOnSchema').callsFake((...args: any[]) => args[2]);
383355
const ctInstance = new Entries({ ...constructorParam, fix: true });
384356

385357
const globalFieldSchema = {

0 commit comments

Comments
 (0)