mirror of
https://github.com/strapi/strapi.git
synced 2025-08-16 04:34:40 +00:00
add tests
This commit is contained in:
parent
1c5c3eb6b9
commit
00e77e77b0
57
packages/core/strapi/lib/commands/__tests__/export.test.js
Normal file
57
packages/core/strapi/lib/commands/__tests__/export.test.js
Normal file
@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {});
|
||||
jest.spyOn(console, 'error').mockImplementation(() => {});
|
||||
|
||||
const mockDataTransfer = {
|
||||
createLocalFileDestinationProvider: jest.fn(),
|
||||
createLocalStrapiSourceProvider: jest.fn(() => {}),
|
||||
createTransferEngine: jest.fn().mockReturnValue({
|
||||
transfer: jest.fn().mockReturnValue(Promise.resolve({})),
|
||||
}),
|
||||
};
|
||||
jest.mock('@strapi/data-transfer', () => {
|
||||
return mockDataTransfer;
|
||||
});
|
||||
|
||||
const defaultFileName = 'defaultFilename';
|
||||
const mockUtils = {
|
||||
getDefaultExportBackupName: jest.fn().mockReturnValue(defaultFileName),
|
||||
};
|
||||
jest.mock('../transfer/utils', () => {
|
||||
return mockUtils;
|
||||
});
|
||||
|
||||
const exportCommand = require('../transfer/export');
|
||||
const { getDefaultExportBackupName } = require('../transfer/utils');
|
||||
|
||||
describe('export', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('uses path provided by user', async () => {
|
||||
const filename = 'testfile';
|
||||
await exportCommand({ output: filename });
|
||||
expect(mockDataTransfer.createLocalFileDestinationProvider).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
file: { path: filename },
|
||||
})
|
||||
);
|
||||
|
||||
expect(getDefaultExportBackupName).not.toHaveBeenCalled();
|
||||
expect(exit).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uses default path if not provided by user', async () => {
|
||||
await exportCommand({});
|
||||
expect(mockDataTransfer.createLocalFileDestinationProvider).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
file: { path: defaultFileName },
|
||||
})
|
||||
);
|
||||
|
||||
expect(getDefaultExportBackupName).toHaveBeenCalled();
|
||||
expect(exit).toHaveBeenCalled();
|
||||
});
|
||||
});
|
@ -8,8 +8,7 @@ const {
|
||||
// eslint-disable-next-line import/no-unresolved, node/no-missing-require
|
||||
} = require('@strapi/data-transfer');
|
||||
const strapi = require('../../Strapi');
|
||||
|
||||
const getDefaultExportBackupName = () => `strapi-backup`;
|
||||
const { getDefaultExportBackupName } = require('./utils');
|
||||
|
||||
const logger = console;
|
||||
|
||||
@ -25,7 +24,7 @@ module.exports = async (args) => {
|
||||
// To file
|
||||
const outputOptions = {
|
||||
file: {
|
||||
path: args.output || getDefaultExportBackupName(),
|
||||
path: args.output ?? getDefaultExportBackupName(),
|
||||
},
|
||||
encryption: {
|
||||
enabled: args.encrypt,
|
||||
@ -36,7 +35,7 @@ module.exports = async (args) => {
|
||||
},
|
||||
};
|
||||
const destination = createLocalFileDestinationProvider(outputOptions);
|
||||
|
||||
console.log('outputoptions', outputOptions);
|
||||
// create transfer engine
|
||||
const engine = createTransferEngine(source, destination, {
|
||||
strategy: 'restore',
|
||||
@ -45,6 +44,7 @@ module.exports = async (args) => {
|
||||
|
||||
try {
|
||||
const result = await engine.transfer();
|
||||
|
||||
if (!result?.destination?.path) throw new Error('Export file not created');
|
||||
logger.log(
|
||||
'Export process has been completed successfully! Export archive is in %s',
|
||||
|
9
packages/core/strapi/lib/commands/transfer/utils.js
Normal file
9
packages/core/strapi/lib/commands/transfer/utils.js
Normal file
@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const getDefaultExportBackupName = () => {
|
||||
return 'strapi-backup';
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getDefaultExportBackupName,
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user