strapi/test/helpers/models.js

158 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict';
2019-05-06 15:33:25 +02:00
const waitRestart = require('./waitRestart');
module.exports = ({ rq }) => {
2019-10-22 18:01:03 +02:00
async function createComponent(data) {
await rq({
2019-10-22 18:01:03 +02:00
url: '/content-type-builder/components',
method: 'POST',
body: {
2019-11-14 16:37:57 +01:00
component: {
category: 'default',
icon: 'default',
connection: 'default',
...data,
},
},
});
await waitRestart();
}
2019-10-22 18:01:03 +02:00
async function deleteComponent(name) {
await rq({
2019-10-22 18:01:03 +02:00
url: `/content-type-builder/components/${name}`,
method: 'DELETE',
});
await waitRestart();
}
2019-12-12 10:15:25 +01:00
function createContentTypeWithType(name, type, opts = {}) {
return createContentType({
connection: 'default',
name,
2019-11-14 16:37:57 +01:00
attributes: {
field: {
type,
...opts,
},
2019-11-14 16:37:57 +01:00
},
});
}
2019-12-12 10:15:25 +01:00
async function createContentType(data) {
2019-05-06 15:33:25 +02:00
await rq({
2019-11-14 16:37:57 +01:00
url: '/content-type-builder/content-types',
2019-05-06 15:33:25 +02:00
method: 'POST',
body: {
2019-11-14 16:37:57 +01:00
contentType: {
connection: 'default',
...data,
},
},
2019-05-06 15:33:25 +02:00
});
await waitRestart();
}
2019-12-12 10:15:25 +01:00
async function createContentTypes(models) {
2019-05-06 15:33:25 +02:00
for (let model of models) {
2019-12-12 10:15:25 +01:00
await createContentType(model);
2019-05-06 15:33:25 +02:00
}
}
2020-09-07 15:04:20 +02:00
async function modifyContentType(data) {
const sanitizedData = { ...data };
delete sanitizedData.editable;
delete sanitizedData.restrictRelationsTo;
2020-09-07 15:04:20 +02:00
await rq({
url: `/content-type-builder/content-types/application::${sanitizedData.name}.${sanitizedData.name}`,
2020-09-07 15:04:20 +02:00
method: 'PUT',
body: {
contentType: {
connection: 'default',
...sanitizedData,
2020-09-07 15:04:20 +02:00
},
},
});
await waitRestart();
}
async function modifyContentTypes(models) {
for (let model of models) {
await modifyContentType(model);
}
}
async function getContentTypeSchema(model) {
const { body } = await rq({
url: '/content-type-builder/content-types',
method: 'GET',
});
const contentType = body.data.find(ct => ct.uid === `application::${model}.${model}`);
return (contentType || {}).schema;
}
2019-12-12 10:15:25 +01:00
async function deleteContentType(model) {
2019-05-06 15:33:25 +02:00
await rq({
2019-11-14 16:37:57 +01:00
url: `/content-type-builder/content-types/application::${model}.${model}`,
2019-05-06 15:33:25 +02:00
method: 'DELETE',
});
await waitRestart();
}
2019-12-12 10:15:25 +01:00
async function deleteContentTypes(models) {
2019-05-06 15:33:25 +02:00
for (let model of models) {
2019-12-12 10:15:25 +01:00
await deleteContentType(model);
2019-05-06 15:33:25 +02:00
}
}
async function cleanupContentTypes(models) {
for (const model of models) {
await cleanupContentType(model);
}
}
async function cleanupContentType(model) {
const { body } = await rq({
url: `/content-manager/collection-types/application::${model}.${model}`,
qs: {
pageSize: 1000,
},
method: 'GET',
});
if (Array.isArray(body.results) && body.results.length > 0) {
await rq({
url: `/content-manager/collection-types/application::${model}.${model}/actions/bulkDelete`,
method: 'POST',
body: {
ids: body.results.map(({ id }) => id),
},
});
}
}
2019-05-06 15:33:25 +02:00
return {
2019-10-22 18:01:03 +02:00
createComponent,
deleteComponent,
2019-12-12 10:15:25 +01:00
createContentType,
createContentTypes,
createContentTypeWithType,
deleteContentType,
deleteContentTypes,
2020-09-07 15:04:20 +02:00
modifyContentType,
modifyContentTypes,
getContentTypeSchema,
cleanupContentType,
cleanupContentTypes,
2019-05-06 15:33:25 +02:00
};
};