mirror of
https://github.com/strapi/strapi.git
synced 2025-07-22 16:37:13 +00:00
123 lines
3.5 KiB
JavaScript
123 lines
3.5 KiB
JavaScript
![]() |
'use strict';
|
||
|
|
||
|
const _ = require('lodash');
|
||
|
|
||
|
const { registerAndLogin } = require('../../../test/helpers/auth');
|
||
|
const { createAuthRequest } = require('../../../test/helpers/request');
|
||
|
const createModelsUtils = require('../../../test/helpers/models');
|
||
|
|
||
|
let rq;
|
||
|
let modelsUtils;
|
||
|
let data = {
|
||
|
dogs: [],
|
||
|
};
|
||
|
|
||
|
const dogModel = {
|
||
|
draftAndPublish: false,
|
||
|
attributes: {
|
||
|
name: {
|
||
|
type: 'string',
|
||
|
},
|
||
|
},
|
||
|
connection: 'default',
|
||
|
name: 'dog',
|
||
|
description: '',
|
||
|
collectionName: '',
|
||
|
};
|
||
|
|
||
|
const dogs = [
|
||
|
{
|
||
|
name: 'Nelson',
|
||
|
},
|
||
|
{
|
||
|
name: 'Atos',
|
||
|
},
|
||
|
];
|
||
|
|
||
|
describe('Migration - draft and publish', () => {
|
||
|
beforeAll(async () => {
|
||
|
const token = await registerAndLogin();
|
||
|
rq = createAuthRequest(token);
|
||
|
modelsUtils = createModelsUtils({ rq });
|
||
|
await modelsUtils.createContentTypes([dogModel]);
|
||
|
for (const dog of dogs) {
|
||
|
const res = await rq({
|
||
|
method: 'POST',
|
||
|
url: '/content-manager/explorer/application::dog.dog',
|
||
|
body: dog,
|
||
|
});
|
||
|
data.dogs.push(res.body);
|
||
|
}
|
||
|
}, 60000);
|
||
|
|
||
|
afterAll(async () => {
|
||
|
const queryString = data.dogs.map((p, i) => `${i}=${p.id}`).join('&');
|
||
|
await rq({
|
||
|
method: 'DELETE',
|
||
|
url: `/content-manager/explorer/deleteAll/application::dog.dog?${queryString}`,
|
||
|
});
|
||
|
await modelsUtils.deleteContentTypes(['dog']);
|
||
|
}, 60000);
|
||
|
|
||
|
describe('Enabling D&P on a content-type', () => {
|
||
|
test('No published_at before enabling the feature', async () => {
|
||
|
let { body } = await rq({
|
||
|
url: '/content-manager/explorer/application::dog.dog',
|
||
|
method: 'GET',
|
||
|
});
|
||
|
expect(body.length).toBe(2);
|
||
|
const expectDog = expectDog => dog =>
|
||
|
dog.name === expectDog.name && _.isUndefined(dog.published_at);
|
||
|
expect(body.find(expectDog(data.dogs[0])));
|
||
|
expect(body.find(expectDog(data.dogs[1])));
|
||
|
});
|
||
|
|
||
|
test('Published_at is equal to created_at after enabling the feature', async () => {
|
||
|
const schema = await modelsUtils.getContentTypeSchema('dog');
|
||
|
await modelsUtils.modifyContentType({
|
||
|
...schema,
|
||
|
draftAndPublish: true,
|
||
|
});
|
||
|
|
||
|
let { body } = await rq({
|
||
|
url: '/content-manager/explorer/application::dog.dog',
|
||
|
method: 'GET',
|
||
|
});
|
||
|
expect(body.length).toBe(2);
|
||
|
const expectDog = expectDog => dog =>
|
||
|
dog.name === expectDog.name &&
|
||
|
dog.published_at === (dog.createdAt || dog.created_at) &&
|
||
|
!isNaN(new Date(dog.published_at).valueOf());
|
||
|
expect(body.find(expectDog(data.dogs[0])));
|
||
|
expect(body.find(expectDog(data.dogs[1])));
|
||
|
data.dogs = body;
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe('Disabling D&P on a content-type', () => {
|
||
|
test('No published_at after disabling the feature + draft removed', async () => {
|
||
|
const res = await rq({
|
||
|
url: `/content-manager/explorer/application::dog.dog/unpublish/${data.dogs[1].id}`,
|
||
|
method: 'POST',
|
||
|
});
|
||
|
data.dogs[1] = res.body;
|
||
|
|
||
|
const schema = await modelsUtils.getContentTypeSchema('dog');
|
||
|
await modelsUtils.modifyContentType({
|
||
|
...schema,
|
||
|
draftAndPublish: false,
|
||
|
});
|
||
|
// drafts should have been deleted with the migration, so we removed
|
||
|
data.dogs = data.dogs.filter(dog => !_.isNil(dog.published_at));
|
||
|
|
||
|
let { body } = await rq({
|
||
|
url: '/content-manager/explorer/application::dog.dog',
|
||
|
method: 'GET',
|
||
|
});
|
||
|
expect(body.length).toBe(1);
|
||
|
expect(body[0]).toMatchObject(_.pick(data.dogs[0], ['name']));
|
||
|
expect(body[0].published_at).toBeUndefined();
|
||
|
});
|
||
|
});
|
||
|
});
|