@@ -6,6 +6,7 @@ import { fsUtil } from '../../../../src/utils';
66import * as contentTypeHelper from '../../../../src/utils/content-type-helper' ;
77import * as extensionHelper from '../../../../src/utils/extension-helper' ;
88import * as taxonomiesHelper from '../../../../src/utils/taxonomies-helper' ;
9+ import { FsUtility } from '@contentstack/cli-utilities' ;
910
1011describe ( 'ImportContentTypes' , ( ) => {
1112 let importContentTypes : ImportContentTypes ;
@@ -16,6 +17,8 @@ describe('ImportContentTypes', () => {
1617 let lookupExtensionStub : sinon . SinonStub ;
1718 let lookUpTaxonomyStub : sinon . SinonStub ;
1819 let makeConcurrentCallStub : sinon . SinonStub ;
20+ let fsUtilityReaddirStub : sinon . SinonStub ;
21+ let fsUtilityReadFileStub : sinon . SinonStub ;
1922
2023 beforeEach ( ( ) => {
2124 fsUtilStub = {
@@ -27,6 +30,46 @@ describe('ImportContentTypes', () => {
2730 sinon . stub ( fsUtil , 'writeFile' ) . callsFake ( fsUtilStub . writeFile ) ;
2831 sinon . stub ( fsUtil , 'makeDirectory' ) . callsFake ( fsUtilStub . makeDirectory ) ;
2932
33+ // Stub FsUtility prototype to make readContentTypeSchemas work
34+ // readContentTypeSchemas reads individual JSON files (ignoring schema.json)
35+ // We'll check what fsUtilStub.readFile returns for schema.json and use that to populate individual files
36+ fsUtilityReaddirStub = sinon . stub ( FsUtility . prototype , 'readdir' ) . callsFake ( ( dirPath : string ) => {
37+ // Try to get mock CTs from the test's fsUtilStub setup for schema.json
38+ // This allows tests to continue using schema.json pattern
39+ try {
40+ const mockCTs = fsUtilStub . readFile ( dirPath + '/schema.json' ) ;
41+ if ( Array . isArray ( mockCTs ) && mockCTs . length > 0 ) {
42+ return mockCTs . map ( ( ct : any ) => `${ ct . uid } .json` ) ;
43+ }
44+ } catch ( e ) {
45+ // If schema.json isn't stubbed, return empty
46+ }
47+ return [ ] ;
48+ } ) ;
49+
50+ fsUtilityReadFileStub = sinon . stub ( FsUtility . prototype , 'readFile' ) . callsFake ( ( filePath : string ) => {
51+ // Extract the UID from the file path and return matching content type
52+ const match = filePath . match ( / ( [ ^ \/ ] + ) \. j s o n $ / ) ;
53+ if ( match ) {
54+ const uid = match [ 1 ] ;
55+ const dirPath = filePath . substring ( 0 , filePath . lastIndexOf ( '/' ) ) ;
56+ try {
57+ // Check if test has stubbed schema.json with mock content types
58+ const mockCTs = fsUtilStub . readFile ( dirPath + '/schema.json' ) ;
59+ if ( Array . isArray ( mockCTs ) ) {
60+ const ct = mockCTs . find ( ( ct : any ) => ct . uid === uid ) ;
61+ if ( ct ) {
62+ // FsUtility.readFile returns parsed JSON (object), not string
63+ return ct ;
64+ }
65+ }
66+ } catch ( e ) {
67+ // If schema.json isn't stubbed, return undefined (file doesn't exist)
68+ }
69+ }
70+ return undefined ;
71+ } ) ;
72+
3073 updateFieldRulesStub = sinon . stub ( contentTypeHelper , 'updateFieldRules' ) ;
3174 lookupExtensionStub = sinon . stub ( extensionHelper , 'lookupExtension' ) ;
3275 lookUpTaxonomyStub = sinon . stub ( taxonomiesHelper , 'lookUpTaxonomy' ) ;
@@ -1168,13 +1211,17 @@ describe('ImportContentTypes', () => {
11681211 describe ( 'analyzeImportData() with individual content type files' , ( ) => {
11691212 it ( 'should read content types from individual files' , async ( ) => {
11701213 const mockContentTypes = [
1171- { uid : 'ct-1' , title : 'CT 1' , schema : [ ] } ,
1172- { uid : 'ct-2' , title : 'CT 2' , schema : [ ] } ,
1214+ { uid : 'ct-1' , title : 'CT 1' , schema : [ ] as any } ,
1215+ { uid : 'ct-2' , title : 'CT 2' , schema : [ ] as any } ,
11731216 ] ;
11741217
1175- // Stub readContentTypeSchemas to return mock content types
1176- const readContentTypeSchemasStub = sinon . stub ( ) . returns ( mockContentTypes ) ;
1177- sinon . stub ( require ( '@contentstack/cli-utilities' ) , 'readContentTypeSchemas' ) . value ( readContentTypeSchemasStub ) ;
1218+ // Configure FsUtility stubs to make readContentTypeSchemas return mock content types
1219+ ( FsUtility . prototype . readdir as sinon . SinonStub ) . returns ( [ 'ct-1.json' , 'ct-2.json' ] ) ;
1220+ ( FsUtility . prototype . readFile as sinon . SinonStub ) . callsFake ( ( filePath : string ) => {
1221+ if ( filePath . includes ( 'ct-1.json' ) ) return JSON . stringify ( mockContentTypes [ 0 ] ) ;
1222+ if ( filePath . includes ( 'ct-2.json' ) ) return JSON . stringify ( mockContentTypes [ 1 ] ) ;
1223+ return '{}' ;
1224+ } ) ;
11781225
11791226 fsUtilStub . readFile . returns ( [ ] ) ;
11801227
@@ -1184,10 +1231,7 @@ describe('ImportContentTypes', () => {
11841231 } ) ;
11851232
11861233 it ( 'should return empty array when no individual files are found' , async ( ) => {
1187- // Stub readContentTypeSchemas to return empty array (no individual files)
1188- const readContentTypeSchemasStub = sinon . stub ( ) . returns ( [ ] ) ;
1189- sinon . stub ( require ( '@contentstack/cli-utilities' ) , 'readContentTypeSchemas' ) . value ( readContentTypeSchemasStub ) ;
1190-
1234+ // readdir returns [] by default, so readContentTypeSchemas will return []
11911235 fsUtilStub . readFile . returns ( [ ] ) ;
11921236
11931237 await ( importContentTypes as any ) . analyzeImportData ( ) ;
0 commit comments