strapi/tests/api/core/content-manager/preview/preview.test.api.ts
Marc Roig 002fc78b3c
feat: preview endpoint (#21574)
* feat: base files for static preview

* feat: preview config

* Update packages/core/content-manager/server/src/preview/routes/index.ts

Co-authored-by: Rémi de Juvigny <8087692+remidej@users.noreply.github.com>

* chore: empty handler

* chore: comment controllers type

* fix: remove is enabled check from load

* feat: test preview config

* chore: refactor type

* feat: preview endpoint

* feat: preview test

* fix: tests

* fix: api test

* chore: comment

---------

Co-authored-by: Rémi de Juvigny <8087692+remidej@users.noreply.github.com>
2024-10-08 14:23:47 +02:00

134 lines
3.3 KiB
TypeScript

import { createStrapiInstance } from 'api-tests/strapi';
import { createAuthRequest } from 'api-tests/request';
import { describeOnCondition } from 'api-tests/utils';
import { createTestBuilder } from 'api-tests/builder';
const collectionTypeUid = 'api::product.product';
const collectionTypeModel = {
singularName: 'product',
pluralName: 'products',
displayName: 'Product',
kind: 'collectionType',
draftAndPublish: true,
pluginOptions: {
i18n: {
localized: true,
},
},
attributes: {
name: {
type: 'string',
},
},
};
const singleTypeUid = 'api::homepage.homepage';
const singleTypeModel = {
singularName: 'homepage',
pluralName: 'homepages',
displayName: 'Homepage',
kind: 'singleType',
draftAndPublish: true,
pluginOptions: {
i18n: {
localized: true,
},
},
attributes: {
title: {
type: 'string',
},
},
};
const edition = process.env.STRAPI_DISABLE_EE === 'true' ? 'CE' : 'EE';
// TODO: Remove skip when future flag is removed
// describeOnCondition(edition === 'EE')('Preview', () => {
describeOnCondition(false)('Preview', () => {
const builder = createTestBuilder();
let strapi;
let rq;
let singleTypeEntry;
const updateEntry = async ({ uid, documentId, data, locale }) => {
const type = documentId ? 'collection-types' : 'single-types';
const params = documentId ? `${type}/${uid}/${documentId}` : `${type}/${uid}`;
const { body } = await rq({
method: 'PUT',
url: `/content-manager/${params}`,
body: data,
qs: { locale },
});
return body.data;
};
const getPreviewUrl = async ({ uid, documentId, locale, status }) => {
return rq({
method: 'GET',
url: `/content-manager/preview/url/${uid}`,
qs: { documentId, locale, status },
});
};
beforeAll(async () => {
await builder.addContentTypes([collectionTypeModel, singleTypeModel]).build();
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
// Update the single type to create an initial history version
singleTypeEntry = await updateEntry({
uid: singleTypeUid,
documentId: undefined,
locale: 'en',
data: {
title: 'Welcome',
},
});
// Configure the preview URL handler
strapi.config.set('admin.preview', {
enabled: true,
config: {
handler: (uid, { documentId, locale, status }) => {
return `/preview/${uid}/${documentId}?locale=${locale}&status=${status}`;
},
},
});
});
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
test('Get preview URL for collection type', async () => {
const { body, statusCode } = await getPreviewUrl({
uid: collectionTypeUid,
documentId: '1',
locale: 'en',
status: 'draft',
});
expect(statusCode).toBe(200);
expect(body.data.url).toEqual(`/preview/${collectionTypeUid}/1?locale=en&status=draft`);
});
test('Get preview URL for single type', async () => {
const { body, statusCode } = await getPreviewUrl({
uid: singleTypeUid,
documentId: undefined,
locale: 'en',
status: 'draft',
});
expect(statusCode).toBe(200);
expect(body.data.url).toEqual(
`/preview/${singleTypeUid}/${singleTypeEntry.documentId}?locale=en&status=draft`
);
});
});