-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathglobal-field.test.ts
More file actions
110 lines (96 loc) · 4.21 KB
/
Copy pathglobal-field.test.ts
File metadata and controls
110 lines (96 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import fs from 'fs';
import sinon from 'sinon';
import { resolve } from 'path';
import { fancy } from 'fancy-test';
import { expect } from 'chai';
import cloneDeep from 'lodash/cloneDeep';
import { ux } from '@contentstack/cli-utilities';
import config from '../../../src/config';
import { GlobalField } from '../../../src/modules';
import { $t, auditMsg } from '../../../src/messages';
import { CtConstructorParam, ModuleConstructorParam } from '../../../src/types';
describe('Global Fields', () => {
let constructorParam: ModuleConstructorParam & CtConstructorParam;
class AuditFixTempClass extends GlobalField {
public missingRefs: Record<string, any>;
constructor(missingRefs: Record<string, any> = {}) {
super({ ...constructorParam, fix: true, moduleName: 'global-fields' });
this.currentUid = 'audit-fix';
this.currentTitle = 'Audit fix';
this.missingRefs = missingRefs;
this.missingRefs['audit-fix'] = [];
}
}
beforeEach(() => {
constructorParam = {
log: () => {},
moduleName: 'global-fields',
ctSchema: cloneDeep(require('../mock/contents/content_types/schema.json')),
gfSchema: cloneDeep(require('../mock/contents/global_fields/globalfields.json')),
config: Object.assign(config, { basePath: resolve(__dirname, '..', 'mock', 'contents'), flags: {} }),
};
});
afterEach(() => {
sinon.restore();
});
describe('run method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(ux, 'confirm', async () => true)
.it('Should Validate the base path for global-fields', async () => {
const gfInstance = new GlobalField({ ...constructorParam });
try {
await gfInstance.run();
} catch (error: any) {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.eql($t(auditMsg.NOT_VALID_PATH, { path: gfInstance.folderPath }));
}
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(GlobalField.prototype, 'lookForReference', async () => {})
.it('should call lookForReference', async () => {
const gfInstance = new GlobalField(constructorParam);
const logSpy = sinon.spy(gfInstance, 'lookForReference');
await gfInstance.run();
expect(logSpy.callCount).to.be.equals(gfInstance.gfSchema.length);
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(GlobalField.prototype, 'lookForReference', async () => {})
.it('should return schema', async () => {
const gfInstance = new GlobalField(constructorParam);
expect(await gfInstance.run(true)).to.deep.equals(gfInstance.gfSchema);
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(GlobalField.prototype, 'lookForReference', async () => {})
.stub(GlobalField.prototype, 'writeFixContent', async () => {})
.it('should call writeFixContent', async () => {
const gfInstance = new GlobalField({ ...constructorParam, fix: true });
const logSpy = sinon.spy(gfInstance, 'writeFixContent');
await gfInstance.run();
expect(logSpy.callCount).to.be.equals(1);
});
});
describe('fix nested global field references', () => {
fancy
.stub(fs, 'rmSync', () => {})
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(GlobalField.prototype, 'writeFixContent', async () => {})
.it('perform audit operation on the given GF schema', async () => {
const gfInstance = new AuditFixTempClass();
await gfInstance.run();
expect(gfInstance.missingRefs).ownProperty('nested_global_field_2');
expect(JSON.stringify(gfInstance.missingRefs)).includes('"missingRefs":["nested_global_field_1"]');
});
fancy
.stub(fs, 'rmSync', () => {})
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(GlobalField.prototype, 'writeFixContent', async () => {})
.it('perform audit and fix operation on the given GF schema', async () => {
const gfInstance = new AuditFixTempClass();
expect(JSON.stringify(await gfInstance.run(true))).includes('"uid":"global_field_sample_2","schema":[]');
});
});
});