Skip to content

Commit 24e2be8

Browse files
Vikram KaltaVikram Kalta
authored andcommitted
test: added test for nested global fields
1 parent 226901d commit 24e2be8

File tree

3 files changed

+92
-31
lines changed

3 files changed

+92
-31
lines changed

packages/contentstack-audit/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $ npm install -g @contentstack/cli-audit
1919
$ csdx COMMAND
2020
running command...
2121
$ csdx (--version|-v)
22-
@contentstack/cli-audit/1.7.5 darwin-arm64 node-v22.2.0
22+
@contentstack/cli-audit/1.7.5 darwin-arm64 node-v18.18.0
2323
$ csdx --help [COMMAND]
2424
USAGE
2525
$ csdx COMMAND

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222

2323
describe('Entries module', () => {
2424
let constructorParam: ModuleConstructorParam & CtConstructorParam;
25+
let ctStub: Sinon.SinonStub;
26+
let gfStub: Sinon.SinonStub;
2527

2628
beforeEach(() => {
2729
constructorParam = {
@@ -33,6 +35,17 @@ describe('Entries module', () => {
3335
};
3436
});
3537

38+
before(() => {
39+
ctStub = Sinon.stub(ContentType.prototype, 'run').resolves({ ct1: [{}] });
40+
gfStub = Sinon.stub(GlobalField.prototype, 'run').resolves({ gf1: [{}] });
41+
});
42+
43+
after(() => {
44+
Sinon.restore(); // Clears Sinon spies/stubs/mocks
45+
ctStub.restore();
46+
gfStub.restore();
47+
});
48+
3649
describe('run method', () => {
3750
fancy
3851
.stdout({ print: process.env.PRINT === 'true' || false })
@@ -93,16 +106,11 @@ describe('Entries module', () => {
93106
describe('fixPrerequisiteData method', () => {
94107
fancy
95108
.stdout({ print: process.env.PRINT === 'true' || false })
96-
.stub(ContentType.prototype, 'run', async () => ({ ct1: [{}] }))
97-
.stub(GlobalField.prototype, 'run', async () => ({ gf1: [{}] }))
98-
99109
.it('should call content type and global fields fix functionality', async () => {
100-
const ctRun = Sinon.spy(ContentType.prototype, 'run');
101-
const gfRun = Sinon.spy(GlobalField.prototype, 'run');
102110
const ctInstance = new Entries(constructorParam);
103111
await ctInstance.fixPrerequisiteData();
104-
expect(ctRun.callCount).to.be.equals(1);
105-
expect(gfRun.callCount).to.be.equals(1);
112+
expect(ctStub.callCount).to.be.equals(1);
113+
expect(gfStub.callCount).to.be.equals(1);
106114
expect(ctInstance.ctSchema).deep.contain({ ct1: [{}] });
107115
expect(ctInstance.gfSchema).deep.contain({ gf1: [{}] });
108116
});
Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from 'fs';
2+
import sinon from 'sinon';
13
import { resolve } from 'path';
24
import { fancy } from 'fancy-test';
35
import { expect } from 'chai';
@@ -7,48 +9,99 @@ import { ux } from '@contentstack/cli-utilities';
79
import config from '../../../src/config';
810
import { GlobalField } from '../../../src/modules';
911
import { $t, auditMsg } from '../../../src/messages';
10-
12+
import { CtConstructorParam, ModuleConstructorParam } from '../../../src/types';
1113

1214
describe('Global Fields', () => {
13-
describe('run method with invalid path for global_feild', () => {
14-
const gfInstance = new GlobalField({
15+
let constructorParam: ModuleConstructorParam & CtConstructorParam;
16+
17+
class AuditFixTempClass extends GlobalField {
18+
constructor(public missingRefs: Record<string, any> = {}) {
19+
super({ ...constructorParam, fix: true, moduleName: 'global-fields' });
20+
this.currentUid = 'audit-fix';
21+
this.currentTitle = 'Audit fix';
22+
this.missingRefs['audit-fix'] = [];
23+
}
24+
}
25+
26+
beforeEach(() => {
27+
constructorParam = {
1528
log: () => {},
1629
moduleName: 'global-fields',
1730
ctSchema: cloneDeep(require('../mock/contents/content_types/schema.json')),
18-
gfSchema: cloneDeep(require('../mock/contents/global_fields/Global_fields_2.json')),
31+
gfSchema: cloneDeep(require('../mock/contents/global_fields/globalfields.json')),
1932
config: Object.assign(config, { basePath: resolve(__dirname, '..', 'mock', 'contents'), flags: {} }),
20-
});
33+
};
34+
});
35+
36+
afterEach(() => {
37+
sinon.restore();
38+
});
39+
40+
describe('run method', () => {
2141
fancy
2242
.stdout({ print: process.env.PRINT === 'true' || false })
2343
.stub(ux, 'confirm', async () => true)
24-
.it('Should Validate the base path for workflows', async () => {
44+
.it('Should Validate the base path for global-fields', async () => {
45+
const gfInstance = new GlobalField({ ...constructorParam });
2546
try {
2647
await gfInstance.run();
2748
} catch (error: any) {
2849
expect(error).to.be.instanceOf(Error);
2950
expect(error.message).to.eql($t(auditMsg.NOT_VALID_PATH, { path: gfInstance.folderPath }));
3051
}
3152
});
53+
54+
fancy
55+
.stdout({ print: process.env.PRINT === 'true' || false })
56+
.stub(GlobalField.prototype, 'lookForReference', async () => {})
57+
.it('should call lookForReference', async () => {
58+
const gfInstance = new GlobalField(constructorParam);
59+
const logSpy = sinon.spy(gfInstance, 'lookForReference');
60+
await gfInstance.run();
61+
expect(logSpy.callCount).to.be.equals(gfInstance.gfSchema.length);
62+
});
63+
64+
fancy
65+
.stdout({ print: process.env.PRINT === 'true' || false })
66+
.stub(GlobalField.prototype, 'lookForReference', async () => {})
67+
.it('should return schema', async () => {
68+
const gfInstance = new GlobalField(constructorParam);
69+
expect(await gfInstance.run(true)).to.deep.equals(gfInstance.gfSchema);
70+
});
71+
72+
fancy
73+
.stdout({ print: process.env.PRINT === 'true' || false })
74+
.stub(GlobalField.prototype, 'lookForReference', async () => {})
75+
.stub(GlobalField.prototype, 'writeFixContent', async () => {})
76+
.it('should call writeFixContent', async () => {
77+
const gfInstance = new GlobalField({ ...constructorParam, fix: true });
78+
const logSpy = sinon.spy(gfInstance, 'writeFixContent');
79+
await gfInstance.run();
80+
expect(logSpy.callCount).to.be.equals(1);
81+
});
3282
});
3383

34-
describe('run method with valid Path', () => {
35-
const gfInstance = new GlobalField({
36-
log: () => {},
37-
moduleName: 'global-fields',
38-
ctSchema: cloneDeep(require('../mock/contents/content_types/schema.json')),
39-
gfSchema: cloneDeep(require('../mock/contents/global_fields/globalfields.json')),
40-
config: Object.assign(config, { basePath: resolve(__dirname, '..', 'mock', 'contents'), flags: {} }),
41-
});
84+
describe('fix nested global field references', () => {
4285
fancy
43-
.stdout({ print: process.env.PRINT === 'true' || true })
44-
.stub(ux, 'confirm', async () => true)
45-
.it('Should output the global feild where nested feilds are not present', async () => {
46-
try {
47-
const missingRefs = await gfInstance.run();
48-
expect(JSON.stringify(missingRefs)).to.be.equal(
49-
'{\n nested_global_field_2: [\n {\n tree: [\n {\n uid: "nested_global_field_2",\n name: "Nested Global Field 2",\n },\n {\n uid: "global_field",\n name: "Global",\n },\n ],\n ct: "nested_global_field_2",\n name: "Nested Global Field 2",\n data_type: "global_field",\n display_name: "Global",\n missingRefs: "Referred Global Field Does not Exist",\n treeStr: "Nested Global Field 2 ➜ Global",\n },\n ],\n global_field_sample_2: [\n {\n tree: [\n {\n uid: "global_field_sample_2",\n name: "global_field_sample_2",\n },\n {\n uid: "group",\n name: "Group",\n },\n {\n uid: "group",\n name: "Group",\n },\n {\n uid: "global_field",\n name: "Global",\n },\n ],\n ct: "global_field_sample_2",\n name: "global_field_sample_2",\n data_type: "global_field",\n display_name: "Global",\n missingRefs: "Referred Global Field Does not Exist",\n treeStr: "global_field_sample_2 ➜ Group ➜ Group ➜ Global",\n },\n ],\n}',
50-
);
51-
} catch (error) {}
86+
.stub(fs, 'rmSync', () => {})
87+
.stdout({ print: process.env.PRINT === 'true' || false })
88+
.stub(GlobalField.prototype, 'writeFixContent', async () => {})
89+
.it('perform audit operation on the given GF schema', async () => {
90+
const gfInstance = new AuditFixTempClass();
91+
92+
await gfInstance.run();
93+
94+
expect(gfInstance.missingRefs).ownProperty('nested_global_field_2');
95+
expect(JSON.stringify(gfInstance.missingRefs)).includes('"missingRefs":["nested_global_field_1"]');
96+
});
97+
98+
fancy
99+
.stub(fs, 'rmSync', () => {})
100+
.stdout({ print: process.env.PRINT === 'true' || false })
101+
.stub(GlobalField.prototype, 'writeFixContent', async () => {})
102+
.it('perform audit and fix operation on the given GF schema', async () => {
103+
const gfInstance = new AuditFixTempClass();
104+
expect(JSON.stringify(await gfInstance.run(true))).includes('"uid":"global_field_sample_2","schema":[]');
52105
});
53106
});
54107
});

0 commit comments

Comments
 (0)