Skip to content

Commit 903a4bd

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

3 files changed

Lines changed: 41 additions & 28 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ describe('ExportContentTypes', () => {
317317

318318
it('should handle empty content types', async () => {
319319
const writeFileStub = FsUtility.prototype.writeFile as sinon.SinonStub;
320+
const completeProgressStub = sinon.stub(exportContentTypes as any, 'completeProgress');
320321

321322
mockStackClient.contentType.returns({
322323
query: sinon.stub().returns({
@@ -330,8 +331,10 @@ describe('ExportContentTypes', () => {
330331
exportContentTypes.contentTypes = [];
331332
await exportContentTypes.start();
332333

333-
// Verify writeFile was called even with empty array
334-
expect(writeFileStub.called).to.be.true;
334+
// With empty content types, writeFile is not called (no files to write)
335+
// But completeProgress should be called to mark the process as complete
336+
expect(completeProgressStub.called).to.be.true;
337+
expect(completeProgressStub.calledWith(true)).to.be.true;
335338
});
336339

337340
it('should handle errors during export without throwing', async () => {

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,11 +1061,19 @@ describe('EntriesExport', () => {
10611061
it('should process all request objects and complete file writing', async () => {
10621062
const locales = [{ code: 'en-us' }];
10631063
const contentTypes = [
1064-
{ uid: 'ct-1', title: 'Content Type 1' },
1065-
{ uid: 'ct-2', title: 'Content Type 2' },
1064+
{ uid: 'ct-1', title: 'Content Type 1', schema: [] as any },
1065+
{ uid: 'ct-2', title: 'Content Type 2', schema: [] as any },
10661066
];
10671067

1068-
mockFsUtil.readFile.onFirstCall().returns(locales).onSecondCall().returns(contentTypes);
1068+
mockFsUtil.readFile.returns(locales); // For locales file
1069+
1070+
// Stub FsUtility.prototype for readContentTypeSchemas
1071+
(FsUtility.prototype.readdir as sinon.SinonStub).returns(['ct-1.json', 'ct-2.json']);
1072+
(FsUtility.prototype.readFile as sinon.SinonStub).callsFake((filePath: string) => {
1073+
if (filePath.includes('ct-1.json')) return contentTypes[0];
1074+
if (filePath.includes('ct-2.json')) return contentTypes[1];
1075+
return undefined;
1076+
});
10691077

10701078
const mockEntryQuery = {
10711079
query: sandbox.stub().returns({
@@ -1104,9 +1112,13 @@ describe('EntriesExport', () => {
11041112

11051113
it('should handle errors during entry processing gracefully', async () => {
11061114
const locales = [{ code: 'en-us' }];
1107-
const contentTypes = [{ uid: 'ct-1', title: 'Content Type 1' }];
1115+
const contentTypes = [{ uid: 'ct-1', title: 'Content Type 1', schema: [] as any }];
11081116

1109-
mockFsUtil.readFile.onFirstCall().returns(locales).onSecondCall().returns(contentTypes);
1117+
mockFsUtil.readFile.returns(locales); // For locales file
1118+
1119+
// Stub FsUtility.prototype for readContentTypeSchemas
1120+
(FsUtility.prototype.readdir as sinon.SinonStub).returns(['ct-1.json']);
1121+
(FsUtility.prototype.readFile as sinon.SinonStub).returns(contentTypes[0]);
11101122

11111123
const processingError = new Error('Entry processing failed');
11121124
const getEntriesStub = sandbox.stub(entriesExport, 'getEntries').rejects(processingError);

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,14 @@ describe('EntriesImport', () => {
578578
if (path.includes('schema.json')) {
579579
return [contentTypeWithoutFieldRules];
580580
}
581-
return {};
581+
if (path.includes('field_rules_ct.json')) {
582+
return contentTypeWithoutFieldRules;
583+
}
584+
return undefined;
582585
});
583586

584-
// Override FsUtility.readFile for this test
585-
(FsUtility.prototype.readFile as sinon.SinonStub).returns(contentTypeWithoutFieldRules);
587+
// Override FsUtility.readdir for this test
588+
(FsUtility.prototype.readdir as sinon.SinonStub).returns(['field_rules_ct.json']);
586589

587590
await entriesImport['updateFieldRules']();
588591

@@ -3124,25 +3127,17 @@ describe('EntriesImport', () => {
31243127
it('should handle content type fetch error', async () => {
31253128
const mockContentTypes = [mockData.simpleContentType, mockData.contentTypeWithReferences];
31263129

3127-
// Configure FsUtility.readFile to return the right content type based on file name
3128-
(FsUtility.prototype.readFile as sinon.SinonStub).callsFake((filePath: string) => {
3129-
if (filePath.includes('simple_ct.json')) return JSON.stringify(mockData.simpleContentType);
3130-
if (filePath.includes('ref_ct.json')) return JSON.stringify(mockData.contentTypeWithReferences);
3131-
return '{}';
3132-
});
3133-
3130+
// Configure FsUtility.readFile to handle all file reads (for both fsUtil and readContentTypeSchemas)
31343131
fsUtilityReadFileStub.callsFake((filePath) => {
3135-
console.log('fsUtil.readFile called with path:', filePath);
31363132
if (filePath.includes('field_rules_uid.json')) {
3137-
console.log('Returning field rules data');
31383133
return ['simple_ct', 'ref_ct']; // array of strings
31393134
}
3135+
if (filePath.includes('simple_ct.json')) return mockData.simpleContentType;
3136+
if (filePath.includes('ref_ct.json')) return mockData.contentTypeWithReferences;
31403137
if (filePath.includes('schema.json')) {
3141-
console.log('Returning schema data');
31423138
return mockContentTypes;
31433139
}
3144-
console.log('Returning empty array');
3145-
return [];
3140+
return undefined;
31463141
});
31473142

31483143
const mockContentType = {
@@ -3166,16 +3161,17 @@ describe('EntriesImport', () => {
31663161
it('should handle content type update error', async () => {
31673162
const mockContentTypes = [mockData.simpleContentType, mockData.contentTypeWithReferences];
31683163

3169-
(FsUtility.prototype.readFile as sinon.SinonStub).returns(JSON.stringify(mockData.simpleContentType));
3164+
(FsUtility.prototype.readdir as sinon.SinonStub).returns(['simple_ct.json']);
31703165

31713166
fsUtilityReadFileStub.callsFake((path) => {
31723167
if (path.includes('field_rules_uid.json')) {
31733168
return ['simple_ct']; // array of strings
31743169
}
3170+
if (path.includes('simple_ct.json')) return mockData.simpleContentType;
31753171
if (path.includes('schema.json')) {
31763172
return mockContentTypes;
31773173
}
3178-
return [];
3174+
return undefined;
31793175
});
31803176

31813177
const mockUpdate = sinon.stub().rejects(new Error('Update failed'));
@@ -3205,16 +3201,17 @@ describe('EntriesImport', () => {
32053201
it('should skip when content type not found', async () => {
32063202
const mockContentTypes = [mockData.simpleContentType, mockData.contentTypeWithReferences];
32073203

3208-
(FsUtility.prototype.readFile as sinon.SinonStub).returns(JSON.stringify(mockData.simpleContentType));
3204+
(FsUtility.prototype.readdir as sinon.SinonStub).returns(['simple_ct.json']);
32093205

32103206
fsUtilityReadFileStub.callsFake((path) => {
32113207
if (path.includes('field_rules_uid.json')) {
32123208
return ['simple_ct']; // array of strings
32133209
}
3210+
if (path.includes('simple_ct.json')) return mockData.simpleContentType;
32143211
if (path.includes('schema.json')) {
32153212
return mockContentTypes;
32163213
}
3217-
return [];
3214+
return undefined;
32183215
});
32193216

32203217
const mockContentType = {
@@ -3249,16 +3246,17 @@ describe('EntriesImport', () => {
32493246
delete contentTypeWithoutRules.field_rules;
32503247
const mockContentTypes = [contentTypeWithoutRules];
32513248

3252-
(FsUtility.prototype.readFile as sinon.SinonStub).returns(JSON.stringify(contentTypeWithoutRules));
3249+
(FsUtility.prototype.readdir as sinon.SinonStub).returns(['simple_ct.json']);
32533250

32543251
fsUtilityReadFileStub.callsFake((path) => {
32553252
if (path.includes('field_rules_uid.json')) {
32563253
return ['simple_ct']; // array of strings
32573254
}
3255+
if (path.includes('simple_ct.json')) return contentTypeWithoutRules;
32583256
if (path.includes('schema.json')) {
32593257
return mockContentTypes;
32603258
}
3261-
return [];
3259+
return undefined;
32623260
});
32633261

32643262
const mockLog = {

0 commit comments

Comments
 (0)