Skip to content

Commit d8d0ed1

Browse files
committed
test: fix export & import ct, entries test cases
1 parent bba60fc commit d8d0ed1

4 files changed

Lines changed: 46 additions & 33 deletions

File tree

packages/contentstack-export/test/unit/export/modules/content-types.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ describe('ExportContentTypes', () => {
8484
// Stub FsUtility methods
8585
sinon.stub(FsUtility.prototype, 'writeFile').resolves();
8686
sinon.stub(FsUtility.prototype, 'makeDirectory').resolves();
87+
// Stub FsUtility.prototype.readdir and readFile for readContentTypeSchemas support
88+
sinon.stub(FsUtility.prototype, 'readdir').returns([]);
89+
sinon.stub(FsUtility.prototype, 'readFile').returns(undefined);
8790
});
8891

8992
afterEach(() => {

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

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ describe('EntriesExport', () => {
140140
createFolderStub.callsFake(() => {
141141
// Do nothing - prevent actual directory creation
142142
});
143+
144+
// Stub FsUtility.prototype.readdir and readFile for readContentTypeSchemas support
145+
// readContentTypeSchemas creates its own FsUtility instance, so we need to stub the prototype
146+
sandbox.stub(FsUtility.prototype, 'readdir').returns([]);
147+
sandbox.stub(FsUtility.prototype, 'readFile').returns(undefined);
143148

144149
entriesExport = new EntriesExport({
145150
exportConfig: mockExportConfig,
@@ -173,16 +178,15 @@ describe('EntriesExport', () => {
173178
mockExportConfig.modules.locales.dirName,
174179
mockExportConfig.modules.locales.fileName,
175180
);
176-
const expectedSchemaPath = path.resolve(
181+
const expectedContentTypesDirPath = path.resolve(
177182
mockExportConfig.exportDir,
178183
mockExportConfig.branchName || '',
179184
mockExportConfig.modules.content_types.dirName,
180-
'schema.json',
181185
);
182186

183187
expect(entriesExport.entriesDirPath).to.equal(expectedEntriesPath);
184188
expect(entriesExport.localesFilePath).to.equal(expectedLocalesPath);
185-
expect(entriesExport.schemaFilePath).to.equal(expectedSchemaPath);
189+
expect(entriesExport.contentTypesDirPath).to.equal(expectedContentTypesDirPath);
186190
});
187191

188192
it('should initialize ExportProjects instance', () => {
@@ -197,27 +201,29 @@ describe('EntriesExport', () => {
197201

198202
describe('start() method - Early Returns', () => {
199203
it('should return early when no content types are found', async () => {
200-
mockFsUtil.readFile
201-
.onFirstCall()
202-
.returns([{ code: 'en-us' }]) // locales
203-
.onSecondCall()
204-
.returns([]); // content types
204+
// Stub mockFsUtil.readFile for locales
205+
mockFsUtil.readFile.returns([{ code: 'en-us' }]);
206+
207+
// Stub FsUtility.prototype for readContentTypeSchemas to return empty
208+
(FsUtility.prototype.readdir as sinon.SinonStub).returns([]); // No content type files
205209

206210
await entriesExport.start();
207211

208212
// Should not attempt to fetch entries
209213
expect(mockStackAPIClient.contentType.called).to.be.false;
210-
// Should read both locales and content types files
211-
expect(mockFsUtil.readFile.calledTwice).to.be.true;
214+
// Should read locales file
215+
expect(mockFsUtil.readFile.called).to.be.true;
212216
});
213217

214218
it('should handle empty locales array gracefully', async () => {
215-
const contentTypes = [{ uid: 'ct-1', title: 'Content Type 1' }];
216-
mockFsUtil.readFile
217-
.onFirstCall()
218-
.returns([]) // empty locales
219-
.onSecondCall()
220-
.returns(contentTypes);
219+
const contentTypes = [{ uid: 'ct-1', title: 'Content Type 1', schema: [] as any }];
220+
221+
// Stub mockFsUtil.readFile for locales
222+
mockFsUtil.readFile.returns([]); // empty locales
223+
224+
// Stub FsUtility.prototype for readContentTypeSchemas to return content types
225+
(FsUtility.prototype.readdir as sinon.SinonStub).returns(['ct-1.json']);
226+
(FsUtility.prototype.readFile as sinon.SinonStub).returns(contentTypes[0]);
221227

222228
await entriesExport.start();
223229

@@ -226,14 +232,14 @@ describe('EntriesExport', () => {
226232
});
227233

228234
it('should handle non-array locales gracefully', async () => {
229-
const contentTypes = [{ uid: 'ct-1', title: 'Content Type 1' }];
230-
// Use empty array instead of null to avoid Object.keys error
231-
// The code checks !Array.isArray first, so empty array will work
232-
mockFsUtil.readFile
233-
.onFirstCall()
234-
.returns([]) // empty locales array
235-
.onSecondCall()
236-
.returns(contentTypes);
235+
const contentTypes = [{ uid: 'ct-1', title: 'Content Type 1', schema: [] as any }];
236+
237+
// Stub mockFsUtil.readFile for locales
238+
mockFsUtil.readFile.returns([]); // empty locales array
239+
240+
// Stub FsUtility.prototype for readContentTypeSchemas to return content types
241+
(FsUtility.prototype.readdir as sinon.SinonStub).returns(['ct-1.json']);
242+
(FsUtility.prototype.readFile as sinon.SinonStub).returns(contentTypes[0]);
237243

238244
// Mock entry query for when entries are processed
239245
const mockEntryQuery = {

packages/contentstack-import/test/unit/import/modules/content-types.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,9 +1218,9 @@ describe('ImportContentTypes', () => {
12181218
// Configure FsUtility stubs to make readContentTypeSchemas return mock content types
12191219
(FsUtility.prototype.readdir as sinon.SinonStub).returns(['ct-1.json', 'ct-2.json']);
12201220
(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 '{}';
1221+
if (filePath.includes('ct-1.json')) return mockContentTypes[0];
1222+
if (filePath.includes('ct-2.json')) return mockContentTypes[1];
1223+
return {};
12241224
});
12251225

12261226
fsUtilStub.readFile.returns([]);

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ describe('EntriesImport', () => {
2929
});
3030

3131
// Stub FsUtility prototype to support readContentTypeSchemas
32-
// readContentTypeSchemas reads individual JSON files and returns parsed objects
32+
// readContentTypeSchemas creates its own FsUtility instance, so we need to stub the prototype
3333
sinon.stub(FsUtility.prototype, 'readdir').returns([]);
34-
sinon.stub(FsUtility.prototype, 'readFile').returns(undefined);
3534

36-
fsUtilityReadFileStub = sinon.stub(fsUtil, 'readFile');
35+
// Stub FsUtility.prototype.readFile for readContentTypeSchemas (returns parsed objects)
36+
// This also stubs fsUtil.readFile since fsUtil is an instance of FsUtility
37+
// Don't set a default return value - let individual tests configure it
38+
fsUtilityReadFileStub = sinon.stub(FsUtility.prototype, 'readFile');
3739
fsUtilityWriteFileStub = sinon.stub(fsUtil, 'writeFile').callsFake(() => {
3840
return Promise.resolve();
3941
});
@@ -509,12 +511,14 @@ describe('EntriesImport', () => {
509511
if (path.includes('schema.json')) {
510512
return [mockData.contentTypeWithFieldRules];
511513
}
512-
return {};
514+
if (path.includes('field_rules_ct.json')) {
515+
return mockData.contentTypeWithFieldRules;
516+
}
517+
return undefined;
513518
});
514519

515-
// Override FsUtility stubs to return field_rules_ct
520+
// Override FsUtility.prototype.readdir for readContentTypeSchemas
516521
(FsUtility.prototype.readdir as sinon.SinonStub).returns(['field_rules_ct.json']);
517-
(FsUtility.prototype.readFile as sinon.SinonStub).returns(mockData.contentTypeWithFieldRules);
518522
});
519523

520524
it('should update field rules with new UIDs', async () => {

0 commit comments

Comments
 (0)