strapi/test/helpers/models.js

238 lines
5.7 KiB
JavaScript
Raw Normal View History

'use strict';
2021-01-04 11:32:43 +01:00
const { isFunction, isNil, prop } = require('lodash/fp');
const { createStrapiInstance } = require('./strapi');
2021-08-06 18:46:30 +02:00
const toUID = name => {
2021-08-11 10:05:20 +02:00
return name.includes('::') ? name : `api::${name}.${name}`;
2021-08-06 18:46:30 +02:00
};
const createHelpers = async ({ strapi: strapiInstance = null, ...options } = {}) => {
const strapi = strapiInstance || (await createStrapiInstance(options));
2021-08-19 22:27:00 +02:00
const contentTypeService = strapi.plugin('content-type-builder').service('content-types');
const componentsService = strapi.plugin('content-type-builder').service('components');
const cleanup = async () => {
if (isNil(strapiInstance)) {
await strapi.destroy();
}
};
return {
strapi,
contentTypeService,
componentsService,
cleanup,
};
};
const createContentType = async (model, { strapi } = {}) => {
const { contentTypeService, cleanup } = await createHelpers({ strapi });
const contentType = await contentTypeService.createContentType({
contentType: {
...model,
},
});
await cleanup();
2019-05-06 15:33:25 +02:00
return contentType;
};
const createContentTypes = async (models, { strapi } = {}) => {
const { contentTypeService, cleanup } = await createHelpers({ strapi });
const contentTypes = await contentTypeService.createContentTypes(
models.map(model => ({
contentType: {
...model,
},
}))
);
2019-05-06 15:33:25 +02:00
await cleanup();
2020-09-07 15:04:20 +02:00
return contentTypes;
};
const createComponent = async (component, { strapi } = {}) => {
const { componentsService, cleanup } = await createHelpers({ strapi });
const createdComponent = await componentsService.createComponent({
component: {
category: 'default',
icon: 'default',
...component,
},
});
await cleanup();
2020-09-07 15:04:20 +02:00
return createdComponent;
};
2020-09-07 15:04:20 +02:00
const createComponents = async (components, { strapi } = {}) => {
const createdComponents = [];
2020-09-07 15:04:20 +02:00
for (const component of components) {
createdComponents.push(await createComponent(component, { strapi }));
2020-09-07 15:04:20 +02:00
}
return createdComponents;
};
const deleteComponent = async (componentUID, { strapi } = {}) => {
const { componentsService, cleanup } = await createHelpers({ strapi });
const component = await componentsService.deleteComponent(componentUID);
await cleanup();
return component;
};
const deleteComponents = async (componentsUID, { strapi } = {}) => {
const deletedComponents = [];
2019-05-06 15:33:25 +02:00
for (const componentUID of componentsUID) {
deletedComponents.push(await deleteComponent(componentUID, { strapi }));
2019-05-06 15:33:25 +02:00
}
return deletedComponents;
};
2021-07-06 14:18:03 +02:00
const deleteContentType = async (uid, { strapi } = {}) => {
const { contentTypeService, cleanup } = await createHelpers({ strapi });
const contentType = await contentTypeService.deleteContentType(uid);
await cleanup();
return contentType;
};
2021-07-06 14:18:03 +02:00
const deleteContentTypes = async (modelsUIDs, { strapi } = {}) => {
const { contentTypeService, cleanup } = await createHelpers({ strapi });
2021-07-06 14:18:03 +02:00
const contentTypes = await contentTypeService.deleteContentTypes(modelsUIDs);
await cleanup();
return contentTypes;
};
async function cleanupModels(models, { strapi } = {}) {
for (const model of models) {
2021-07-05 18:35:16 +02:00
await cleanupModel(model, { strapi });
2019-05-06 15:33:25 +02:00
}
}
2021-07-06 14:18:03 +02:00
async function cleanupModel(uid, { strapi: strapiIst } = {}) {
const { strapi, cleanup } = await createHelpers({ strapi: strapiIst });
2021-07-06 14:18:03 +02:00
await strapi.query(uid).deleteMany();
await cleanup();
}
async function createFixtures(dataMap, { strapi: strapiIst } = {}) {
const { strapi, cleanup } = await createHelpers({ strapi: strapiIst });
const models = Object.keys(dataMap);
const resultMap = {};
2019-05-06 15:33:25 +02:00
for (const model of models) {
const entries = [];
for (const data of dataMap[model]) {
2021-08-06 18:46:30 +02:00
entries.push(await strapi.entityService.create(toUID(model), { data }));
}
resultMap[model] = entries;
}
await cleanup();
return resultMap;
}
async function createFixturesFor(model, entries, { strapi: strapiIst } = {}) {
const { strapi, cleanup } = await createHelpers({ strapi: strapiIst });
const results = [];
for (const entry of entries) {
const dataToCreate = isFunction(entry) ? entry(results) : entry;
2021-08-06 18:46:30 +02:00
results.push(await strapi.entityService.create(toUID(model), { data: dataToCreate }));
}
await cleanup();
return results;
}
2021-01-04 11:32:43 +01:00
async function deleteFixturesFor(model, entries, { strapi: strapiIst } = {}) {
const { strapi, cleanup } = await createHelpers({ strapi: strapiIst });
2021-08-06 18:46:30 +02:00
await strapi.query(toUID(model)).deleteMany({ where: { id: entries.map(prop('id')) } });
2021-01-04 11:32:43 +01:00
await cleanup();
}
async function modifyContentType(data, { strapi } = {}) {
const { contentTypeService, cleanup } = await createHelpers({ strapi });
const sanitizedData = { ...data };
delete sanitizedData.editable;
delete sanitizedData.restrictRelationsTo;
2021-08-06 18:46:30 +02:00
const uid = toUID(sanitizedData.name);
const ct = await contentTypeService.editContentType(uid, {
contentType: {
...sanitizedData,
},
});
await cleanup();
return ct;
}
async function getContentTypeSchema(modelName, { strapi: strapiIst } = {}) {
const { strapi, contentTypeService, cleanup } = await createHelpers({ strapi: strapiIst });
2021-08-06 18:46:30 +02:00
const uid = toUID(modelName);
const ct = contentTypeService.formatContentType(strapi.contentTypes[uid]);
await cleanup();
return (ct || {}).schema;
}
module.exports = {
2021-08-06 18:46:30 +02:00
toUID,
// Create Content-Types
createContentType,
createContentTypes,
// Delete Content-Types
deleteContentType,
deleteContentTypes,
// Cleanup Models
cleanupModel,
cleanupModels,
// Create Components
createComponent,
createComponents,
// Delete Components
deleteComponent,
deleteComponents,
// Fixtures
createFixtures,
createFixturesFor,
2021-01-04 11:32:43 +01:00
deleteFixturesFor,
// Update Content-Types
modifyContentType,
// Misc
getContentTypeSchema,
2019-05-06 15:33:25 +02:00
};