test(review-workflow): add unit test for review workflows service

This commit is contained in:
nathan-pichon 2023-02-02 11:30:06 +01:00
parent 4f756d166d
commit dc671f61ce
No known key found for this signature in database
2 changed files with 89 additions and 1 deletions

View File

@ -0,0 +1,83 @@
'use strict';
const reviewWorkflowsServiceFactory = require('../review-workflows/review-workflows');
const workflowMock = {
id: 1,
};
const stagesMock = [
{
id: 1,
name: 'stage 1',
},
{
id: 2,
name: 'stage 2',
},
{
id: 3,
name: 'stage 3',
},
];
const workflowsServiceMock = {
count: jest.fn(() => 0),
create: jest.fn(() => workflowMock),
};
const stagesServiceMock = {
count: jest.fn(() => 0),
createMany: jest.fn(() => stagesMock),
};
const strapiMock = {
service(serviceName) {
switch (serviceName) {
case 'admin::stages':
return stagesServiceMock;
case 'admin::workflows':
return workflowsServiceMock;
default:
return null;
}
},
};
const reviewWorkflowsService = reviewWorkflowsServiceFactory({ strapi: strapiMock });
describe('Review workflows service', () => {
afterEach(() => {
jest.resetAllMocks();
});
describe('bootstrap', () => {
test('Without stages or workflows in DB', async () => {
await reviewWorkflowsService.bootstrap();
expect(workflowsServiceMock.count).toBeCalled();
expect(stagesServiceMock.count).toBeCalled();
expect(stagesServiceMock.createMany).toBeCalled();
expect(workflowsServiceMock.create).toBeCalled();
});
test('With a workflow in DB', async () => {
workflowsServiceMock.count.mockResolvedValue(1);
await reviewWorkflowsService.bootstrap();
expect(workflowsServiceMock.count).toBeCalled();
expect(stagesServiceMock.count).toBeCalled();
expect(stagesServiceMock.createMany).not.toBeCalled();
expect(workflowsServiceMock.create).not.toBeCalled();
});
test('With stages in DB', async () => {
stagesServiceMock.count.mockResolvedValue(5);
await reviewWorkflowsService.bootstrap();
expect(workflowsServiceMock.count).toBeCalled();
expect(stagesServiceMock.count).toBeCalled();
expect(stagesServiceMock.createMany).not.toBeCalled();
expect(workflowsServiceMock.create).not.toBeCalled();
});
});
});

View File

@ -2,7 +2,12 @@
const { getService } = require('../../utils');
// Map every stage in the array to be ordered in the relation
/**
* Map every stage in the array to be ordered in the relation
* @param {Object[]} stages
* @param {number} stages.id
* @return {Object[]}
*/
function buildStagesConnectArray(stages) {
return stages.map((stage, index) => {
const connect = {