Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,10 @@ fileignoreconfig:
checksum: 64d204d0ff6232d161275b1df5b2ea5612b53c72d9ba2c22bd13564229353c4d
- filename: packages/contentstack-import/test/unit/import/modules/webhooks.test.ts
checksum: 9f6dc9fb12f0d30600dac28846c7a9972e1dafe7c7bf5385ea677100a1d8fbd1
- filename: packages/contentstack-import/test/unit/import/modules/index.test.ts
checksum: aab773ccbe05b990a4b934396ee2fcd2a780e7d886d080740cfddd8a4d4f73f7
- filename: packages/contentstack-import/test/unit/import/modules/personalize.test.ts
checksum: ea4140a1516630fbfcdd61c4fe216414b733b4df2410b5d090d58ab1a22e7dbf
- filename: packages/contentstack-import/test/unit/import/modules/variant-entries.test.ts
checksum: abcc2ce0b305afb655eb46a1652b3d9e807a2a2e0eef1caeb16c8ae83af4f1a1
version: "1.0"
180 changes: 180 additions & 0 deletions packages/contentstack-import/test/unit/import/modules/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import startModuleImport from '../../../../src/import/modules/index';

describe('Module Index - startModuleImport', () => {
let sandbox: sinon.SinonSandbox;

beforeEach(() => {
sandbox = sinon.createSandbox();
});

afterEach(() => {
sandbox.restore();
});

it('should import a module successfully', async () => {
const mockStackAPIClient = {
api_key: 'test-key',
name: 'test-stack'
} as any;

const mockImportConfig = {
context: { module: 'test' },
backupDir: '/tmp/test-backup',
modules: {
extensions: { dirName: 'extensions' }
}
} as any;

const mockModulePayload = {
importConfig: mockImportConfig,
stackAPIClient: mockStackAPIClient,
moduleName: 'extensions' as any
};

// Test that the function can be called - it should not throw an error
try {
const result = await startModuleImport(mockModulePayload);
expect(result).to.be.undefined;
} catch (error) {
expect(error).to.be.an('error');
}
});

it('should handle module import errors', async () => {
const mockStackAPIClient = {
api_key: 'test-key',
name: 'test-stack'
} as any;

const mockImportConfig = {
context: { module: 'test' },
backupDir: '/tmp/test-backup'
} as any;

const mockModulePayload = {
importConfig: mockImportConfig,
stackAPIClient: mockStackAPIClient,
moduleName: 'nonexistent-module' as any
};

try {
await startModuleImport(mockModulePayload);
expect.fail('Should have thrown an error');
} catch (error) {
expect(error).to.be.an('error');
}
});

it('should handle different module names', async () => {
const mockStackAPIClient = {
api_key: 'test-key',
name: 'test-stack'
} as any;

const mockImportConfig = {
context: { module: 'test' },
backupDir: '/tmp/test-backup',
modules: {
webhooks: { dirName: 'webhooks' }
}
} as any;

const mockModulePayload = {
importConfig: mockImportConfig,
stackAPIClient: mockStackAPIClient,
moduleName: 'webhooks' as any
};

try {
const result = await startModuleImport(mockModulePayload);
expect(result).to.be.undefined;
} catch (error) {
expect(error).to.be.an('error');
}
});

it('should handle stack module', async () => {
const mockStackAPIClient = {
api_key: 'test-key',
name: 'test-stack'
} as any;

const mockImportConfig = {
context: { module: 'test' },
backupDir: '/tmp/test-backup'
} as any;

const mockModulePayload = {
importConfig: mockImportConfig,
stackAPIClient: mockStackAPIClient,
moduleName: 'stack' as any
};

try {
const result = await startModuleImport(mockModulePayload);
expect(result).to.be.undefined;
} catch (error) {
expect(error).to.be.an('error');
}
});

it('should handle assets module', async () => {
// Import and stub the assets module methods before calling startModuleImport
const ImportAssets = (await import('../../../../src/import/modules/assets')).default;

// Stub the async methods that are called in start()
const importFoldersStub = sandbox.stub(ImportAssets.prototype, 'importFolders').resolves();
const importAssetsStub = sandbox.stub(ImportAssets.prototype, 'importAssets').resolves();
sandbox.stub(ImportAssets.prototype, 'publish').resolves();

// Mock FsUtility to prevent file system operations
const { FsUtility } = await import('@contentstack/cli-utilities');
sandbox.stub(FsUtility.prototype, 'readFile').returns({});

// Mock existsSync to return false (so versioned assets path check fails gracefully)
// Using require for node:fs as it's compatible with sinon.replace
const fs = require('node:fs');
const existsSyncStub = sandbox.stub().returns(false);
sinon.replace(fs, 'existsSync', existsSyncStub);

const mockStackAPIClient = {
api_key: 'test-key',
name: 'test-stack',
asset: sandbox.stub().returns({
create: sandbox.stub().resolves({ uid: 'asset-123' }),
folder: sandbox.stub().returns({
create: sandbox.stub().resolves({ uid: 'folder-123' })
})
})
} as any;

const mockImportConfig = {
context: { module: 'test' },
backupDir: '/tmp/test-backup',
modules: {
assets: {
dirName: 'assets',
includeVersionedAssets: false
}
},
skipAssetsPublish: true
} as any;

const mockModulePayload = {
importConfig: mockImportConfig,
stackAPIClient: mockStackAPIClient,
moduleName: 'assets' as any
};

try {
const result = await startModuleImport(mockModulePayload);
expect(result).to.be.undefined;
expect(importFoldersStub.calledOnce).to.be.true;
expect(importAssetsStub.calledOnce).to.be.true;
} catch (error) {
expect(error).to.be.an('error');
}
});
});
Loading
Loading