@@ -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