1+ import fs from 'fs' ;
2+ import sinon from 'sinon' ;
13import { resolve } from 'path' ;
24import { fancy } from 'fancy-test' ;
35import { expect } from 'chai' ;
@@ -7,48 +9,99 @@ import { ux } from '@contentstack/cli-utilities';
79import config from '../../../src/config' ;
810import { GlobalField } from '../../../src/modules' ;
911import { $t , auditMsg } from '../../../src/messages' ;
10-
12+ import { CtConstructorParam , ModuleConstructorParam } from '../../../src/types' ;
1113
1214describe ( '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