strapi/packages/core/content-manager/server/tests/relations-dp.test.e2e.js

261 lines
6.1 KiB
JavaScript
Raw Normal View History

2022-08-09 19:35:48 +02:00
'use strict';
const { createTestBuilder } = require('../../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
const { createAuthRequest } = require('../../../../../test/helpers/request');
let strapi;
let rq;
2022-08-11 17:50:57 +02:00
const data = {
2022-08-09 19:35:48 +02:00
products: [],
shops: [],
};
const compo = {
displayName: 'compo',
attributes: {
name: {
type: 'string',
},
compoProducts: {
type: 'relation',
relation: 'manyToMany',
target: 'api::product.product',
},
},
};
const productModel = {
draftAndPublish: true,
attributes: {
name: {
type: 'string',
},
},
displayName: 'Product',
singularName: 'product',
pluralName: 'products',
description: '',
collectionName: '',
};
const shopModel = {
draftAndPublish: true,
attributes: {
name: {
type: 'string',
},
products: {
type: 'relation',
relation: 'manyToMany',
target: 'api::product.product',
targetAttribute: 'shops',
inversedBy: 'shops',
},
myCompo: {
type: 'component',
repeatable: false,
component: 'default.compo',
},
},
displayName: 'Shop',
singularName: 'shop',
pluralName: 'shops',
};
describe('Relations with Draft & Publish', () => {
const builder = createTestBuilder();
beforeAll(async () => {
await builder
.addContentTypes([productModel])
.addComponent(compo)
.addContentTypes([shopModel])
.build();
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
const { body: createdProduct1 } = await rq({
method: 'POST',
url: '/content-manager/collection-types/api::product.product',
body: { name: 'Skate' },
});
await rq({
url: `/content-manager/collection-types/api::product.product/${createdProduct1.id}/actions/publish`,
method: 'POST',
});
const { body: createdProduct2 } = await rq({
method: 'POST',
url: '/content-manager/collection-types/api::product.product',
body: { name: 'Candle' },
});
data.products.push(createdProduct1);
data.products.push(createdProduct2);
const { body: createdShop } = await rq({
method: 'POST',
url: '/content-manager/collection-types/api::shop.shop',
body: {
name: 'Cazotte Shop',
products: [createdProduct1.id],
myCompo: { compoProducts: [createdProduct2.id] },
},
});
data.shops.push(createdShop);
});
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
describe('findAvailable', () => {
2022-08-09 19:35:48 +02:00
test('relation not in a component && no entity', async () => {
let res = await rq({
method: 'GET',
url: '/content-manager/relations/api::shop.shop/products',
});
expect(res.status).toBe(200);
expect(res.body.results).toMatchObject([
{
id: expect.any(Number),
name: 'Candle',
publishedAt: null,
},
{
id: expect.any(Number),
name: 'Skate',
publishedAt: expect.any(String),
},
]);
// can omitIds
res = await rq({
method: 'GET',
url: '/content-manager/relations/api::shop.shop/products',
qs: {
idsToOmit: [data.products[0].id],
},
});
expect(res.body.results).toMatchObject([
{
id: expect.any(Number),
name: 'Candle',
publishedAt: null,
},
]);
});
test('relation not in a component && on an entity', async () => {
let res = await rq({
method: 'GET',
url: '/content-manager/relations/api::shop.shop/products',
qs: {
entityId: data.shops[0].id,
},
});
expect(res.status).toBe(200);
expect(res.body.results).toMatchObject([
{
id: expect.any(Number),
name: 'Candle',
publishedAt: null,
},
]);
// can omitIds
res = await rq({
method: 'GET',
url: '/content-manager/relations/api::shop.shop/products',
qs: {
entityId: data.shops[0].id,
idsToOmit: [data.products[1].id],
},
});
expect(res.body.results).toHaveLength(0);
});
test('relation in a component && no entity', async () => {
let res = await rq({
method: 'GET',
url: '/content-manager/relations/api::shop.shop/compoProducts',
qs: {
component: 'default.compo',
},
});
expect(res.status).toBe(200);
expect(res.body.results).toMatchObject([
{
id: expect.any(Number),
name: 'Candle',
publishedAt: null,
},
{
id: expect.any(Number),
name: 'Skate',
publishedAt: expect.any(String),
},
]);
// can omitIds
res = await rq({
method: 'GET',
url: '/content-manager/relations/api::shop.shop/compoProducts',
qs: {
component: 'default.compo',
idsToOmit: [data.products[0].id],
},
});
expect(res.body.results).toMatchObject([
{
id: expect.any(Number),
name: 'Candle',
publishedAt: null,
},
]);
});
test('relation in a component && on an entity', async () => {
let res = await rq({
method: 'GET',
url: '/content-manager/relations/api::shop.shop/compoProducts',
qs: {
entityId: data.shops[0].myCompo.id,
component: 'default.compo',
},
});
expect(res.status).toBe(200);
expect(res.body.results).toMatchObject([
{
id: expect.any(Number),
name: 'Skate',
publishedAt: expect.any(String),
},
]);
// can omitIds
res = await rq({
method: 'GET',
url: '/content-manager/relations/api::shop.shop/compoProducts',
qs: {
entityId: data.shops[0].myCompo.id,
component: 'default.compo',
idsToOmit: [data.products[0].id],
},
});
expect(res.body.results).toHaveLength(0);
});
});
});