Skip to content

Commit 2f7f9e9

Browse files
authored
Merge pull request #2207 from contentstack/tests/DX-3668
Tests: Added unit test cases for personalize, variant-entries and index
2 parents d925e90 + 7d724ab commit 2f7f9e9

File tree

4 files changed

+1386
-0
lines changed

4 files changed

+1386
-0
lines changed

.talismanrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,10 @@ fileignoreconfig:
153153
checksum: 64d204d0ff6232d161275b1df5b2ea5612b53c72d9ba2c22bd13564229353c4d
154154
- filename: packages/contentstack-import/test/unit/import/modules/webhooks.test.ts
155155
checksum: 9f6dc9fb12f0d30600dac28846c7a9972e1dafe7c7bf5385ea677100a1d8fbd1
156+
- filename: packages/contentstack-import/test/unit/import/modules/index.test.ts
157+
checksum: aab773ccbe05b990a4b934396ee2fcd2a780e7d886d080740cfddd8a4d4f73f7
158+
- filename: packages/contentstack-import/test/unit/import/modules/personalize.test.ts
159+
checksum: ea4140a1516630fbfcdd61c4fe216414b733b4df2410b5d090d58ab1a22e7dbf
160+
- filename: packages/contentstack-import/test/unit/import/modules/variant-entries.test.ts
161+
checksum: abcc2ce0b305afb655eb46a1652b3d9e807a2a2e0eef1caeb16c8ae83af4f1a1
156162
version: "1.0"
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import { expect } from 'chai';
2+
import * as sinon from 'sinon';
3+
import startModuleImport from '../../../../src/import/modules/index';
4+
5+
describe('Module Index - startModuleImport', () => {
6+
let sandbox: sinon.SinonSandbox;
7+
8+
beforeEach(() => {
9+
sandbox = sinon.createSandbox();
10+
});
11+
12+
afterEach(() => {
13+
sandbox.restore();
14+
});
15+
16+
it('should import a module successfully', async () => {
17+
const mockStackAPIClient = {
18+
api_key: 'test-key',
19+
name: 'test-stack'
20+
} as any;
21+
22+
const mockImportConfig = {
23+
context: { module: 'test' },
24+
backupDir: '/tmp/test-backup',
25+
modules: {
26+
extensions: { dirName: 'extensions' }
27+
}
28+
} as any;
29+
30+
const mockModulePayload = {
31+
importConfig: mockImportConfig,
32+
stackAPIClient: mockStackAPIClient,
33+
moduleName: 'extensions' as any
34+
};
35+
36+
// Test that the function can be called - it should not throw an error
37+
try {
38+
const result = await startModuleImport(mockModulePayload);
39+
expect(result).to.be.undefined;
40+
} catch (error) {
41+
expect(error).to.be.an('error');
42+
}
43+
});
44+
45+
it('should handle module import errors', async () => {
46+
const mockStackAPIClient = {
47+
api_key: 'test-key',
48+
name: 'test-stack'
49+
} as any;
50+
51+
const mockImportConfig = {
52+
context: { module: 'test' },
53+
backupDir: '/tmp/test-backup'
54+
} as any;
55+
56+
const mockModulePayload = {
57+
importConfig: mockImportConfig,
58+
stackAPIClient: mockStackAPIClient,
59+
moduleName: 'nonexistent-module' as any
60+
};
61+
62+
try {
63+
await startModuleImport(mockModulePayload);
64+
expect.fail('Should have thrown an error');
65+
} catch (error) {
66+
expect(error).to.be.an('error');
67+
}
68+
});
69+
70+
it('should handle different module names', async () => {
71+
const mockStackAPIClient = {
72+
api_key: 'test-key',
73+
name: 'test-stack'
74+
} as any;
75+
76+
const mockImportConfig = {
77+
context: { module: 'test' },
78+
backupDir: '/tmp/test-backup',
79+
modules: {
80+
webhooks: { dirName: 'webhooks' }
81+
}
82+
} as any;
83+
84+
const mockModulePayload = {
85+
importConfig: mockImportConfig,
86+
stackAPIClient: mockStackAPIClient,
87+
moduleName: 'webhooks' as any
88+
};
89+
90+
try {
91+
const result = await startModuleImport(mockModulePayload);
92+
expect(result).to.be.undefined;
93+
} catch (error) {
94+
expect(error).to.be.an('error');
95+
}
96+
});
97+
98+
it('should handle stack module', async () => {
99+
const mockStackAPIClient = {
100+
api_key: 'test-key',
101+
name: 'test-stack'
102+
} as any;
103+
104+
const mockImportConfig = {
105+
context: { module: 'test' },
106+
backupDir: '/tmp/test-backup'
107+
} as any;
108+
109+
const mockModulePayload = {
110+
importConfig: mockImportConfig,
111+
stackAPIClient: mockStackAPIClient,
112+
moduleName: 'stack' as any
113+
};
114+
115+
try {
116+
const result = await startModuleImport(mockModulePayload);
117+
expect(result).to.be.undefined;
118+
} catch (error) {
119+
expect(error).to.be.an('error');
120+
}
121+
});
122+
123+
it('should handle assets module', async () => {
124+
// Import and stub the assets module methods before calling startModuleImport
125+
const ImportAssets = (await import('../../../../src/import/modules/assets')).default;
126+
127+
// Stub the async methods that are called in start()
128+
const importFoldersStub = sandbox.stub(ImportAssets.prototype, 'importFolders').resolves();
129+
const importAssetsStub = sandbox.stub(ImportAssets.prototype, 'importAssets').resolves();
130+
sandbox.stub(ImportAssets.prototype, 'publish').resolves();
131+
132+
// Mock FsUtility to prevent file system operations
133+
const { FsUtility } = await import('@contentstack/cli-utilities');
134+
sandbox.stub(FsUtility.prototype, 'readFile').returns({});
135+
136+
// Mock existsSync to return false (so versioned assets path check fails gracefully)
137+
// Using require for node:fs as it's compatible with sinon.replace
138+
const fs = require('node:fs');
139+
const existsSyncStub = sandbox.stub().returns(false);
140+
sinon.replace(fs, 'existsSync', existsSyncStub);
141+
142+
const mockStackAPIClient = {
143+
api_key: 'test-key',
144+
name: 'test-stack',
145+
asset: sandbox.stub().returns({
146+
create: sandbox.stub().resolves({ uid: 'asset-123' }),
147+
folder: sandbox.stub().returns({
148+
create: sandbox.stub().resolves({ uid: 'folder-123' })
149+
})
150+
})
151+
} as any;
152+
153+
const mockImportConfig = {
154+
context: { module: 'test' },
155+
backupDir: '/tmp/test-backup',
156+
modules: {
157+
assets: {
158+
dirName: 'assets',
159+
includeVersionedAssets: false
160+
}
161+
},
162+
skipAssetsPublish: true
163+
} as any;
164+
165+
const mockModulePayload = {
166+
importConfig: mockImportConfig,
167+
stackAPIClient: mockStackAPIClient,
168+
moduleName: 'assets' as any
169+
};
170+
171+
try {
172+
const result = await startModuleImport(mockModulePayload);
173+
expect(result).to.be.undefined;
174+
expect(importFoldersStub.calledOnce).to.be.true;
175+
expect(importAssetsStub.calledOnce).to.be.true;
176+
} catch (error) {
177+
expect(error).to.be.an('error');
178+
}
179+
});
180+
});

0 commit comments

Comments
 (0)