mirror of
https://github.com/strapi/strapi.git
synced 2025-07-31 12:55:08 +00:00

* add params to relation route to omit existing relations * small refacto * use omit approach * Fix relation select (Front) Signed-off-by: HichamELBSI <elabbassih@gmail.com> * Fix relation select review Signed-off-by: HichamELBSI <elabbassih@gmail.com> * update doc aos + remove useless cast to string Co-authored-by: HichamELBSI <elabbassih@gmail.com>
192 lines
4.8 KiB
JavaScript
192 lines
4.8 KiB
JavaScript
'use strict';
|
|
|
|
// Test a simple default API with no relations
|
|
|
|
const { omit, pick } = require('lodash/fp');
|
|
|
|
const { createTestBuilder } = require('../../../test/helpers/builder');
|
|
const { createStrapiInstance } = require('../../../test/helpers/strapi');
|
|
const { createAuthRequest } = require('../../../test/helpers/request');
|
|
|
|
let strapi;
|
|
let rq;
|
|
let data = {
|
|
products: [],
|
|
shops: [],
|
|
};
|
|
|
|
const productModel = {
|
|
attributes: {
|
|
name: {
|
|
type: 'string',
|
|
},
|
|
},
|
|
connection: 'default',
|
|
name: 'product',
|
|
description: '',
|
|
collectionName: '',
|
|
};
|
|
|
|
const productWithDPModel = {
|
|
attributes: {
|
|
name: {
|
|
type: 'string',
|
|
},
|
|
},
|
|
connection: 'default',
|
|
name: 'product',
|
|
draftAndPublish: true,
|
|
description: '',
|
|
collectionName: '',
|
|
};
|
|
|
|
const shopModel = {
|
|
attributes: {
|
|
name: {
|
|
type: 'string',
|
|
},
|
|
products: {
|
|
dominant: true,
|
|
nature: 'manyToMany',
|
|
target: 'application::product.product',
|
|
targetAttribute: 'shops',
|
|
},
|
|
},
|
|
connection: 'default',
|
|
name: 'shop',
|
|
};
|
|
|
|
const shops = [
|
|
{
|
|
name: 'market',
|
|
},
|
|
];
|
|
|
|
const products = ({ withPublished = false }) => ({ shop }) => {
|
|
const shops = [shop[0].id];
|
|
|
|
const entries = [
|
|
{
|
|
name: 'tomato',
|
|
shops,
|
|
published_at: new Date(),
|
|
},
|
|
{
|
|
name: 'apple',
|
|
shops,
|
|
published_at: null,
|
|
},
|
|
];
|
|
|
|
if (withPublished) {
|
|
return entries;
|
|
}
|
|
|
|
return entries.map(omit('published_at'));
|
|
};
|
|
|
|
describe('Relation-list route', () => {
|
|
describe('without draftAndPublish', () => {
|
|
const builder = createTestBuilder();
|
|
|
|
beforeAll(async () => {
|
|
await builder
|
|
.addContentTypes([productModel, shopModel])
|
|
.addFixtures(shopModel.name, shops)
|
|
.addFixtures(productModel.name, products({ withPublished: false }))
|
|
.build();
|
|
|
|
strapi = await createStrapiInstance();
|
|
rq = await createAuthRequest({ strapi });
|
|
|
|
data.shops = builder.sanitizedFixturesFor(shopModel.name, strapi);
|
|
data.products = builder.sanitizedFixturesFor(productModel.name, strapi);
|
|
}, 60000);
|
|
|
|
afterAll(async () => {
|
|
await strapi.destroy();
|
|
await builder.cleanup();
|
|
}, 60000);
|
|
|
|
test('Can get relation-list for products of a shop', async () => {
|
|
const res = await rq({
|
|
method: 'POST',
|
|
url: '/content-manager/relations/application::shop.shop/products',
|
|
});
|
|
|
|
expect(res.body).toHaveLength(data.products.length);
|
|
data.products.forEach((product, index) => {
|
|
expect(res.body[index]).toStrictEqual(pick(['_id', 'id', 'name'], product));
|
|
});
|
|
});
|
|
|
|
test('Can get relation-list for products of a shop and omit some results', async () => {
|
|
const res = await rq({
|
|
method: 'POST',
|
|
url: '/content-manager/relations/application::shop.shop/products',
|
|
body: {
|
|
idsToOmit: [data.products[0].id],
|
|
},
|
|
});
|
|
|
|
expect(res.body).toHaveLength(1);
|
|
expect(res.body[0]).toStrictEqual(pick(['_id', 'id', 'name'], data.products[1]));
|
|
});
|
|
});
|
|
|
|
describe('with draftAndPublish', () => {
|
|
const builder = createTestBuilder();
|
|
|
|
beforeAll(async () => {
|
|
await builder
|
|
.addContentTypes([productWithDPModel, shopModel])
|
|
.addFixtures(shopModel.name, shops)
|
|
.addFixtures(productWithDPModel.name, products({ withPublished: true }))
|
|
.build();
|
|
|
|
strapi = await createStrapiInstance();
|
|
rq = await createAuthRequest({ strapi });
|
|
|
|
data.shops = builder.sanitizedFixturesFor(shopModel.name, strapi);
|
|
data.products = builder.sanitizedFixturesFor(productWithDPModel.name, strapi);
|
|
}, 60000);
|
|
|
|
afterAll(async () => {
|
|
await strapi.destroy();
|
|
await builder.cleanup();
|
|
}, 60000);
|
|
|
|
test('Can get relation-list for products of a shop', async () => {
|
|
const res = await rq({
|
|
method: 'POST',
|
|
url: '/content-manager/relations/application::shop.shop/products',
|
|
});
|
|
|
|
expect(res.body).toHaveLength(data.products.length);
|
|
|
|
const tomatoProductRes = res.body.find(p => p.name === 'tomato');
|
|
const appleProductRes = res.body.find(p => p.name === 'apple');
|
|
|
|
expect(tomatoProductRes).toMatchObject(pick(['_id', 'id', 'name'], data.products[0]));
|
|
expect(tomatoProductRes.published_at).toBeISODate();
|
|
expect(appleProductRes).toStrictEqual({
|
|
...pick(['_id', 'id', 'name'], data.products[1]),
|
|
published_at: null,
|
|
});
|
|
});
|
|
|
|
test('Can get relation-list for products of a shop and omit some results', async () => {
|
|
const res = await rq({
|
|
method: 'POST',
|
|
url: '/content-manager/relations/application::shop.shop/products',
|
|
body: {
|
|
idsToOmit: [data.products[1].id],
|
|
},
|
|
});
|
|
|
|
expect(res.body).toHaveLength(1);
|
|
expect(res.body[0]).toMatchObject(pick(['_id', 'id', 'name'], data.products[0]));
|
|
});
|
|
});
|
|
});
|