Skip to content

Commit 8153396

Browse files
committed
Fix: Update the unit test cases
1 parent 2b38bee commit 8153396

File tree

5 files changed

+21
-227
lines changed

5 files changed

+21
-227
lines changed

packages/contentstack-import/test/unit/import/module-importer.test.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ describe('ModuleImporter', () => {
509509

510510
await importer.start();
511511

512-
expect(importer['stackAPIClient'].locale.calledOnce).to.be.true;
512+
expect(masterLocalDetailsStub.calledOnce).to.be.true;
513+
expect(masterLocalDetailsStub.firstCall.args[0]).to.equal(importer['stackAPIClient']);
513514
expect(importer['importConfig'].master_locale).to.deep.equal({ code: 'en-us' });
514515
expect(importer['importConfig'].masterLocale).to.deep.equal({ code: 'en-us' });
515516
});
@@ -534,13 +535,7 @@ describe('ModuleImporter', () => {
534535

535536
it('should set both master_locale and masterLocale', async () => {
536537
mockImportConfig.master_locale = undefined;
537-
538-
const localeMock = {
539-
query: sandbox.stub().returnsThis(),
540-
find: sandbox.stub().resolves({ items: [{ code: 'de-de' }] }),
541-
};
542-
mockStackClient.locale = sandbox.stub().returns(localeMock);
543-
mockStackClient._localeMock = localeMock;
538+
masterLocalDetailsStub.resolves({ code: 'de-de' });
544539

545540
const importer = new ModuleImporter(mockManagementClient as any, mockImportConfig);
546541

@@ -552,23 +547,19 @@ describe('ModuleImporter', () => {
552547

553548
it('should handle error when masterLocalDetails fails', async () => {
554549
mockImportConfig.master_locale = undefined;
555-
556-
const localeMock = {
557-
query: sandbox.stub().returnsThis(),
558-
find: sandbox.stub().rejects(new Error('Master locale fetch failed')),
559-
};
560-
mockStackClient.locale = sandbox.stub().returns(localeMock);
561-
mockStackClient._localeMock = localeMock;
550+
masterLocalDetailsStub.rejects(new Error('Master locale fetch failed'));
562551

563552
const importer = new ModuleImporter(mockManagementClient as any, mockImportConfig);
564553

554+
let caught: unknown;
565555
try {
566556
await importer.start();
567-
expect.fail('Should have thrown an error');
568-
} catch (error: any) {
569-
expect(error).to.be.an('error');
570-
expect(error.message).to.equal('Master locale fetch failed');
557+
} catch (e) {
558+
caught = e;
571559
}
560+
561+
expect(caught).to.be.instanceOf(Error);
562+
expect((caught as Error).message).to.equal('Master locale fetch failed');
572563
});
573564
});
574565

packages/contentstack-import/test/unit/utils/backup-handler.test.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ describe('Backup Handler', () => {
1414
let cliuxStub: any;
1515
let tempDir: string;
1616
let sourceDir: string;
17-
let backupDir: string;
1817
let originalCwd: string;
1918
let processCwdStub: sinon.SinonStub;
2019

@@ -27,7 +26,6 @@ describe('Backup Handler', () => {
2726
// In CI, os.tmpdir() returns a safe temp directory that's cleaned up automatically
2827
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'backup-handler-test-'));
2928
sourceDir = path.join(tempDir, 'source');
30-
backupDir = path.join(tempDir, 'backup');
3129

3230
// Stub process.cwd() to return tempDir so backups are created there, not in actual working directory
3331
// This is critical for CI - prevents creating files in the workspace during tests
@@ -47,7 +45,6 @@ describe('Backup Handler', () => {
4745
module: 'all',
4846
},
4947
masterLocale: { code: 'en-us' },
50-
backupDir: backupDir,
5148
region: 'us',
5249
modules: {} as any,
5350
host: 'https://api.contentstack.io',
@@ -132,25 +129,7 @@ describe('Backup Handler', () => {
132129
expect(logStub.debug.calledWith(`Using existing backup directory: ${existingBackupPath}`)).to.be.true;
133130
});
134131

135-
it('should use branchDir over contentDir when both are provided', async () => {
136-
const branchDir = path.join(tempDir, 'branch');
137-
fs.mkdirSync(branchDir);
138-
fs.writeFileSync(path.join(branchDir, 'branch-file.json'), '{}');
139-
140-
const config = {
141-
...mockImportConfig,
142-
branchDir: branchDir,
143-
contentDir: sourceDir,
144-
};
145-
146-
const result = await backupHandler(config);
147-
148-
expect(result).to.be.a('string');
149-
expect(fs.existsSync(result)).to.be.true;
150-
expect(logStub.debug.called).to.be.true;
151-
});
152-
153-
it('should use contentDir when branchDir is not provided', async () => {
132+
it('should copy from contentDir', async () => {
154133
const config = {
155134
...mockImportConfig,
156135
contentDir: sourceDir,

packages/contentstack-import/test/unit/utils/import-config-handler.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ describe('Import Config Handler', () => {
332332
expect(result.branchAlias).to.equal('my-branch');
333333
});
334334

335-
it('should set branchName and branchDir from branch flag', async () => {
335+
it('should set branchName from branch flag', async () => {
336336
const importCmdFlags = {
337337
data: '/test/content',
338338
branch: 'my-branch',
@@ -341,7 +341,7 @@ describe('Import Config Handler', () => {
341341
const result = await setupConfig(importCmdFlags);
342342

343343
expect(result.branchName).to.equal('my-branch');
344-
expect(result.branchDir).to.equal(path.resolve('/test/content'));
344+
expect(result.contentDir).to.equal(path.resolve('/test/content'));
345345
});
346346

347347
it('should set moduleName and singleModuleImport from module flag', async () => {

packages/contentstack-import/test/unit/utils/import-path-resolver.test.ts

Lines changed: 8 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,25 @@ import { expect } from 'chai';
22
import sinon from 'sinon';
33
import * as path from 'path';
44
import {
5-
selectBranchFromDirectory,
65
resolveImportPath,
76
updateImportConfigWithResolvedPath,
87
executeImportPathLogic,
98
} from '../../../src/utils/import-path-resolver';
109
import { ImportConfig } from '../../../src/types';
1110
import * as fileHelper from '../../../src/utils/file-helper';
12-
import * as interactive from '../../../src/utils/interactive';
1311
import * as cliUtilities from '@contentstack/cli-utilities';
1412
import defaultConfig from '../../../src/config';
1513

1614
describe('Import Path Resolver', () => {
1715
let sandbox: sinon.SinonSandbox;
1816
let fileExistsSyncStub: sinon.SinonStub;
19-
let readFileStub: sinon.SinonStub;
20-
let askBranchSelectionStub: sinon.SinonStub;
2117
let logStub: any;
2218

2319
beforeEach(() => {
2420
sandbox = sinon.createSandbox();
2521

26-
// Mock file-helper
2722
fileExistsSyncStub = sandbox.stub(fileHelper, 'fileExistsSync');
28-
readFileStub = sandbox.stub(fileHelper, 'readFile');
2923

30-
// Mock interactive
31-
askBranchSelectionStub = sandbox.stub(interactive, 'askBranchSelection');
32-
33-
// Mock log
3424
logStub = {
3525
debug: sandbox.stub(),
3626
warn: sandbox.stub(),
@@ -43,95 +33,6 @@ describe('Import Path Resolver', () => {
4333
sandbox.restore();
4434
});
4535

46-
describe('selectBranchFromDirectory', () => {
47-
const contentDir = '/test/content';
48-
49-
it('should return null when branches.json does not exist', async () => {
50-
const branchesJsonPath = path.join(contentDir, 'branches.json');
51-
fileExistsSyncStub.withArgs(branchesJsonPath).returns(false);
52-
53-
const result = await selectBranchFromDirectory(contentDir);
54-
55-
expect(result).to.be.null;
56-
expect(fileExistsSyncStub.calledWith(branchesJsonPath)).to.be.true;
57-
expect(readFileStub.called).to.be.false;
58-
});
59-
60-
it('should return null when branches.json is empty array', async () => {
61-
const branchesJsonPath = path.join(contentDir, 'branches.json');
62-
fileExistsSyncStub.withArgs(branchesJsonPath).returns(true);
63-
readFileStub.withArgs(branchesJsonPath).resolves([]);
64-
65-
const result = await selectBranchFromDirectory(contentDir);
66-
67-
expect(result).to.be.null;
68-
});
69-
70-
it('should return null when branches.json is not an array', async () => {
71-
const branchesJsonPath = path.join(contentDir, 'branches.json');
72-
fileExistsSyncStub.withArgs(branchesJsonPath).returns(true);
73-
readFileStub.withArgs(branchesJsonPath).resolves({ invalid: 'data' });
74-
75-
const result = await selectBranchFromDirectory(contentDir);
76-
77-
expect(result).to.be.null;
78-
});
79-
80-
it('should return null when branches.json is null', async () => {
81-
const branchesJsonPath = path.join(contentDir, 'branches.json');
82-
fileExistsSyncStub.withArgs(branchesJsonPath).returns(true);
83-
readFileStub.withArgs(branchesJsonPath).resolves(null);
84-
85-
const result = await selectBranchFromDirectory(contentDir);
86-
87-
expect(result).to.be.null;
88-
});
89-
90-
it('should return branchName for single branch (content at root)', async () => {
91-
const branchesJsonPath = path.join(contentDir, 'branches.json');
92-
const branchesData = [{ uid: 'branch1' }];
93-
94-
fileExistsSyncStub.withArgs(branchesJsonPath).returns(true);
95-
readFileStub.withArgs(branchesJsonPath).resolves(branchesData);
96-
97-
const result = await selectBranchFromDirectory(contentDir);
98-
99-
expect(result).to.deep.equal({ branchName: 'branch1' });
100-
expect(askBranchSelectionStub.called).to.be.false;
101-
});
102-
103-
it('should prompt user when multiple branches exist and return selected branchName', async () => {
104-
const branchesJsonPath = path.join(contentDir, 'branches.json');
105-
const branchesData = [{ uid: 'branch1' }, { uid: 'branch2' }, { uid: 'branch3' }];
106-
107-
fileExistsSyncStub.withArgs(branchesJsonPath).returns(true);
108-
readFileStub.withArgs(branchesJsonPath).resolves(branchesData);
109-
askBranchSelectionStub.withArgs(['branch1', 'branch2', 'branch3']).resolves('branch2');
110-
111-
const result = await selectBranchFromDirectory(contentDir);
112-
113-
expect(result).to.deep.equal({ branchName: 'branch2' });
114-
expect(askBranchSelectionStub.calledOnce).to.be.true;
115-
expect(askBranchSelectionStub.calledWith(['branch1', 'branch2', 'branch3'])).to.be.true;
116-
});
117-
118-
it('should throw error when readFile fails', async () => {
119-
const branchesJsonPath = path.join(contentDir, 'branches.json');
120-
const error = new Error('Read file error');
121-
122-
fileExistsSyncStub.withArgs(branchesJsonPath).returns(true);
123-
readFileStub.withArgs(branchesJsonPath).rejects(error);
124-
125-
try {
126-
await selectBranchFromDirectory(contentDir);
127-
expect.fail('Should have thrown an error');
128-
} catch (err: any) {
129-
expect(err).to.equal(error);
130-
expect(logStub.error.called).to.be.true;
131-
}
132-
});
133-
});
134-
13536
describe('resolveImportPath', () => {
13637
let mockConfig: ImportConfig;
13738
let mockStackAPIClient: any;
@@ -155,83 +56,26 @@ describe('Import Path Resolver', () => {
15556
}
15657
});
15758

158-
it('should use contentDir from importConfig when set', async () => {
159-
mockConfig.contentDir = '/test/data';
160-
fileExistsSyncStub.withArgs('/test/data').returns(true);
161-
162-
// Mock module types check
163-
defaultConfig.modules.types.forEach((moduleType) => {
164-
fileExistsSyncStub.withArgs(path.join('/test/data', moduleType)).returns(false);
165-
});
166-
167-
const result = await resolveImportPath(mockConfig, mockStackAPIClient);
168-
169-
expect(result).to.equal('/test/data');
170-
});
171-
172-
it('should return contentDir when branchName matches current directory name', async () => {
173-
mockConfig.branchName = 'content';
174-
fileExistsSyncStub.withArgs('/test/content').returns(true);
175-
176-
const result = await resolveImportPath(mockConfig, mockStackAPIClient);
177-
178-
expect(result).to.equal('/test/content');
179-
});
180-
181-
it('should return contentDir when branchName is specified (content always at root)', async () => {
182-
mockConfig.branchName = 'branch1';
59+
it('should return contentDir when module folders exist at export root', async () => {
18360
fileExistsSyncStub.withArgs('/test/content').returns(true);
184-
185-
const result = await resolveImportPath(mockConfig, mockStackAPIClient);
186-
187-
expect(result).to.equal('/test/content');
188-
});
189-
190-
it('should return contentDir when module folders exist', async () => {
19161
const modulePath = path.join('/test/content', defaultConfig.modules.types[0]);
192-
193-
fileExistsSyncStub.withArgs('/test/content').returns(true);
19462
fileExistsSyncStub.withArgs(modulePath).returns(true);
19563

19664
const result = await resolveImportPath(mockConfig, mockStackAPIClient);
19765

19866
expect(result).to.equal('/test/content');
19967
});
20068

201-
it('should use contentDir and set branchName from selectBranchFromDirectory when no branch name', async () => {
202-
fileExistsSyncStub.withArgs('/test/content').returns(true);
203-
204-
// Mock module types check - all return false
205-
defaultConfig.modules.types.forEach((moduleType) => {
206-
fileExistsSyncStub.withArgs(path.join('/test/content', moduleType)).returns(false);
207-
});
208-
209-
// Mock branches.json - single branch
210-
const branchesJsonPath = path.join('/test/content', 'branches.json');
211-
fileExistsSyncStub.withArgs(branchesJsonPath).returns(true);
212-
readFileStub.withArgs(branchesJsonPath).resolves([{ uid: 'branch1' }]);
213-
214-
const result = await resolveImportPath(mockConfig, mockStackAPIClient);
215-
216-
expect(result).to.equal('/test/content');
217-
expect(mockConfig.branchName).to.equal('branch1');
218-
});
219-
220-
it('should return contentDir when selectBranchFromDirectory returns null', async () => {
221-
fileExistsSyncStub.withArgs('/test/content').returns(true);
222-
223-
// Mock module types check - all return false
69+
it('should return contentDir when no module folders exist', async () => {
70+
mockConfig.contentDir = '/test/data';
71+
fileExistsSyncStub.withArgs('/test/data').returns(true);
22472
defaultConfig.modules.types.forEach((moduleType) => {
225-
fileExistsSyncStub.withArgs(path.join('/test/content', moduleType)).returns(false);
73+
fileExistsSyncStub.withArgs(path.join('/test/data', moduleType)).returns(false);
22674
});
22775

228-
// Mock branches.json not found
229-
const branchesJsonPath = path.join('/test/content', 'branches.json');
230-
fileExistsSyncStub.withArgs(branchesJsonPath).returns(false);
231-
23276
const result = await resolveImportPath(mockConfig, mockStackAPIClient);
23377

234-
expect(result).to.equal('/test/content');
78+
expect(result).to.equal('/test/data');
23579
});
23680
});
23781

@@ -251,18 +95,15 @@ describe('Import Path Resolver', () => {
25195

25296
await updateImportConfigWithResolvedPath(mockConfig, resolvedPath);
25397

254-
expect(mockConfig.branchDir).to.be.undefined;
25598
expect(mockConfig.contentDir).to.equal('/test/content');
25699
});
257100

258-
it('should update config with resolved path', async () => {
101+
it('should update contentDir with resolved path when it exists', async () => {
259102
const resolvedPath = '/test/resolved';
260-
261103
fileExistsSyncStub.withArgs(resolvedPath).returns(true);
262104

263105
await updateImportConfigWithResolvedPath(mockConfig, resolvedPath);
264106

265-
expect(mockConfig.branchDir).to.equal(resolvedPath);
266107
expect(mockConfig.contentDir).to.equal(resolvedPath);
267108
});
268109
});
@@ -279,25 +120,16 @@ describe('Import Path Resolver', () => {
279120
} as ImportConfig;
280121
});
281122

282-
it('should execute complete path resolution logic (content at root)', async () => {
123+
it('should resolve path and set contentDir on config', async () => {
283124
fileExistsSyncStub.withArgs('/test/content').returns(true);
284-
285-
// Mock module types check - no module folders at root
286125
defaultConfig.modules.types.forEach((moduleType) => {
287126
fileExistsSyncStub.withArgs(path.join('/test/content', moduleType)).returns(false);
288127
});
289128

290-
// Mock branches.json - single branch
291-
const branchesJsonPath = path.join('/test/content', 'branches.json');
292-
fileExistsSyncStub.withArgs(branchesJsonPath).returns(true);
293-
readFileStub.withArgs(branchesJsonPath).resolves([{ uid: 'branch1' }]);
294-
295129
const result = await executeImportPathLogic(mockConfig, mockStackAPIClient);
296130

297131
expect(result).to.equal('/test/content');
298-
expect(mockConfig.branchDir).to.equal('/test/content');
299132
expect(mockConfig.contentDir).to.equal('/test/content');
300-
expect(mockConfig.branchName).to.equal('branch1');
301133
});
302134
});
303135
});

0 commit comments

Comments
 (0)