2020-11-10 14:15:31 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-05 14:24:53 +01:00
|
|
|
const { get } = require('lodash/fp');
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
const _ = require('lodash');
|
|
|
|
const modelsUtils = require('../models');
|
2021-04-29 13:51:12 +02:00
|
|
|
const { sanitizeEntity } = require('../../../packages/core/utils');
|
2021-01-04 11:32:43 +01:00
|
|
|
const actionRegistry = require('./action-registry');
|
2021-01-05 14:24:53 +01:00
|
|
|
const { createContext } = require('./context');
|
2020-11-10 14:15:31 +01:00
|
|
|
|
|
|
|
const createTestBuilder = (options = {}) => {
|
|
|
|
const { initialState } = options;
|
2021-01-05 14:24:53 +01:00
|
|
|
const ctx = createContext(initialState);
|
2020-11-13 16:48:17 +01:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
return {
|
|
|
|
get models() {
|
2021-01-05 14:24:53 +01:00
|
|
|
return ctx.state.models;
|
2020-11-10 14:15:31 +01:00
|
|
|
},
|
|
|
|
|
2020-11-13 16:48:17 +01:00
|
|
|
get fixtures() {
|
2021-01-05 14:24:53 +01:00
|
|
|
return ctx.state.fixtures;
|
2020-11-13 16:48:17 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
sanitizedFixtures(strapi) {
|
|
|
|
return _.mapValues(this.fixtures, (value, key) => this.sanitizedFixturesFor(key, strapi));
|
|
|
|
},
|
|
|
|
|
|
|
|
sanitizedFixturesFor(modelName, strapi) {
|
2021-07-05 18:35:16 +02:00
|
|
|
const model = strapi.getModel(`application::${modelName}.${modelName}`);
|
2020-11-13 16:48:17 +01:00
|
|
|
const fixtures = this.fixturesFor(modelName);
|
|
|
|
|
|
|
|
return sanitizeEntity(fixtures, { model });
|
|
|
|
},
|
|
|
|
|
|
|
|
fixturesFor(modelName) {
|
|
|
|
return this.fixtures[modelName];
|
2020-11-10 14:15:31 +01:00
|
|
|
},
|
|
|
|
|
2021-01-05 14:24:53 +01:00
|
|
|
addAction(code, ...params) {
|
|
|
|
const actionCreator = get(code, actionRegistry);
|
|
|
|
|
|
|
|
ctx.addAction(actionCreator(...params));
|
|
|
|
|
2020-11-13 16:48:17 +01:00
|
|
|
return this;
|
2020-11-10 14:15:31 +01:00
|
|
|
},
|
|
|
|
|
2021-01-05 14:24:53 +01:00
|
|
|
addContentType(contentType) {
|
|
|
|
return this.addAction('contentType.create', contentType);
|
|
|
|
},
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
addContentTypes(contentTypes, { batch = true } = {}) {
|
2021-01-05 14:24:53 +01:00
|
|
|
return this.addAction(
|
|
|
|
batch ? 'contentType.createBatch' : 'contentType.createMany',
|
|
|
|
contentTypes
|
|
|
|
);
|
2020-11-10 14:15:31 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
addComponent(component) {
|
2021-01-05 14:24:53 +01:00
|
|
|
return this.addAction('component.create', component);
|
2020-11-10 14:15:31 +01:00
|
|
|
},
|
|
|
|
|
2021-01-04 11:32:43 +01:00
|
|
|
addFixtures(model, entries) {
|
2021-01-05 14:24:53 +01:00
|
|
|
return this.addAction('fixtures.create', model, entries, () => this.fixtures);
|
2020-11-10 14:15:31 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
async build() {
|
2021-01-05 14:24:53 +01:00
|
|
|
for (const action of ctx.state.actions) {
|
|
|
|
await action.build(ctx);
|
2020-11-10 14:15:31 +01:00
|
|
|
}
|
2021-01-05 14:24:53 +01:00
|
|
|
|
|
|
|
return this;
|
2020-11-10 14:15:31 +01:00
|
|
|
},
|
|
|
|
|
2021-01-04 11:32:43 +01:00
|
|
|
async cleanup(options = {}) {
|
|
|
|
const { enableTestDataAutoCleanup = true } = options;
|
2021-01-05 14:24:53 +01:00
|
|
|
const { models, actions } = ctx.state;
|
2021-01-04 11:32:43 +01:00
|
|
|
|
|
|
|
if (enableTestDataAutoCleanup) {
|
2021-01-05 14:24:53 +01:00
|
|
|
for (const model of models.reverse()) {
|
2021-07-06 14:18:03 +02:00
|
|
|
await modelsUtils.cleanupModel(model.uid);
|
2021-01-04 11:32:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 14:24:53 +01:00
|
|
|
for (const action of actions.reverse()) {
|
|
|
|
await action.cleanup(ctx);
|
2020-11-10 14:15:31 +01:00
|
|
|
}
|
2021-01-05 14:24:53 +01:00
|
|
|
|
|
|
|
ctx.resetState();
|
|
|
|
|
|
|
|
return this;
|
2020-11-10 14:15:31 +01:00
|
|
|
},
|
|
|
|
};
|
2020-11-13 16:48:17 +01:00
|
|
|
};
|
2020-11-10 14:15:31 +01:00
|
|
|
|
2021-01-04 11:32:43 +01:00
|
|
|
module.exports = { createTestBuilder };
|