strapi/test/helpers/builder/action-registry.js

101 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-01-04 11:32:43 +01:00
'use strict';
2021-01-05 14:24:53 +01:00
const { isFunction, map } = require('lodash/fp');
2021-01-04 11:32:43 +01:00
const modelsUtils = require('../models');
const stringifyDates = object =>
JSON.parse(
JSON.stringify(object, (key, value) => {
if (this[key] instanceof Date) {
return this[key].toUTCString();
}
return value;
})
);
const formatFixtures = map(stringifyDates);
module.exports = {
2021-01-05 14:24:53 +01:00
contentType: {
2021-01-04 11:32:43 +01:00
create: contentType => {
let createdModel;
return {
2021-01-05 14:24:53 +01:00
async build(ctx) {
2021-01-04 11:32:43 +01:00
createdModel = await modelsUtils.createContentType(contentType);
2021-01-05 14:24:53 +01:00
ctx.addModel(createdModel);
2021-01-04 11:32:43 +01:00
},
2021-07-06 14:18:03 +02:00
cleanup: () => modelsUtils.deleteContentType(createdModel.uid),
2021-01-04 11:32:43 +01:00
};
},
createBatch: contentTypes => {
let createdModels = [];
return {
2021-01-05 14:24:53 +01:00
async build(ctx) {
2021-01-04 11:32:43 +01:00
createdModels = await modelsUtils.createContentTypes(contentTypes);
2021-01-05 14:24:53 +01:00
createdModels.forEach(ctx.addModel);
2021-01-04 11:32:43 +01:00
},
async cleanup() {
for (const model of createdModels) {
2021-07-06 14:18:03 +02:00
await modelsUtils.deleteContentType(model.uid);
2021-01-04 11:32:43 +01:00
}
},
};
},
createMany: contentTypes => {
const createdModels = [];
return {
2021-01-05 14:24:53 +01:00
async build(ctx) {
2021-01-04 11:32:43 +01:00
for (const contentType of contentTypes) {
2021-01-05 14:24:53 +01:00
const model = await modelsUtils.createContentType(contentType);
2021-01-04 11:32:43 +01:00
2021-01-05 14:24:53 +01:00
createdModels.push(model);
ctx.addModel(model);
}
2021-01-04 11:32:43 +01:00
},
async cleanup() {
for (const model of createdModels) {
2021-07-06 14:18:03 +02:00
await modelsUtils.deleteContentType(model.uid);
2021-01-04 11:32:43 +01:00
}
},
};
},
},
2021-01-05 14:24:53 +01:00
component: {
2021-01-04 11:32:43 +01:00
create: component => {
let createdModel;
return {
2021-01-05 14:24:53 +01:00
async build(ctx) {
2021-01-04 11:32:43 +01:00
createdModel = await modelsUtils.createComponent(component);
2021-01-05 14:24:53 +01:00
ctx.addModel(createdModel);
2021-01-04 11:32:43 +01:00
},
cleanup: () => modelsUtils.deleteComponent(createdModel.uid),
};
},
},
fixtures: {
create(modelName, entries, getFixtures) {
let createdEntries = [];
return {
2021-01-05 14:24:53 +01:00
async build(ctx) {
2021-01-04 11:32:43 +01:00
createdEntries = formatFixtures(
await modelsUtils.createFixturesFor(
modelName,
isFunction(entries) ? entries(getFixtures()) : entries
)
);
2021-01-05 14:24:53 +01:00
ctx.addFixtures(modelName, createdEntries);
2021-01-04 11:32:43 +01:00
},
cleanup: () => modelsUtils.deleteFixturesFor(modelName, createdEntries),
};
},
},
};