strapi/packages/plugins/i18n/tests/content-manager/find-existing-relations.test.api.js

132 lines
2.8 KiB
JavaScript
Raw Normal View History

'use strict';
const { pick } = require('lodash/fp');
2021-04-29 11:11:46 +02:00
const { createTestBuilder } = require('../../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
const { createAuthRequest } = require('../../../../../test/helpers/request');
let strapi;
let rq;
2022-08-08 15:50:34 +02:00
const data = {
products: [],
shops: [],
};
const productModel = {
pluginOptions: {
i18n: {
localized: true,
},
},
attributes: {
name: {
type: 'string',
},
},
2021-09-13 16:57:04 +02:00
displayName: 'Product',
singularName: 'product',
pluralName: 'products',
description: '',
collectionName: '',
};
const shopModel = {
pluginOptions: {
i18n: {
localized: true,
},
},
attributes: {
name: {
type: 'string',
},
products: {
2021-07-08 18:15:32 +02:00
type: 'relation',
relation: 'manyToMany',
2021-08-06 18:09:49 +02:00
target: 'api::product.product',
targetAttribute: 'shops',
},
},
2021-09-13 16:57:04 +02:00
displayName: 'Shop',
singularName: 'shop',
pluralName: 'shops',
};
const shops = [
2022-10-04 10:54:18 +02:00
{
name: 'mercato',
locale: 'it',
},
{
name: 'market',
locale: 'en',
},
];
2022-10-04 10:54:18 +02:00
const products = ({ shop: shops }) => {
const entries = [
{
name: 'pomodoro',
2022-10-04 10:54:18 +02:00
shops: [shops[0].id],
locale: 'it',
},
{
name: 'apple',
2022-10-04 10:54:18 +02:00
shops: [shops[1].id],
locale: 'en',
},
];
return entries;
};
2022-09-01 17:24:34 +02:00
describe('i18n - Find existing relations', () => {
const builder = createTestBuilder();
beforeAll(async () => {
await builder
.addContentTypes([productModel, shopModel])
.addFixtures('plugin::i18n.locale', [
2021-08-06 18:46:30 +02:00
{
name: 'It',
code: 'it',
},
])
2021-09-13 16:57:04 +02:00
.addFixtures(shopModel.singularName, shops)
.addFixtures(productModel.singularName, products)
.build();
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
data.shops = await builder.sanitizedFixturesFor(shopModel.singularName, strapi);
data.products = await builder.sanitizedFixturesFor(productModel.singularName, strapi);
});
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
2022-10-04 10:54:18 +02:00
test('Get Italian product for italian shop filter on any locale', async () => {
const res = await rq({
2022-08-10 19:14:04 +02:00
method: 'GET',
url: `/content-manager/relations/api::shop.shop/${data.shops[0].id}/products`,
});
2022-08-10 19:14:04 +02:00
expect(res.body.results).toHaveLength(1);
2022-10-04 10:54:18 +02:00
expect(res.body.results[0]).toStrictEqual(pick(['id', 'name'], data.products[0]));
});
2022-10-04 10:54:18 +02:00
test('Get english product for english shop', async () => {
const res = await rq({
2022-08-10 19:14:04 +02:00
method: 'GET',
url: `/content-manager/relations/api::shop.shop/${data.shops[1].id}/products`,
});
2022-08-10 19:14:04 +02:00
expect(res.body.results).toHaveLength(1);
2022-10-04 10:54:18 +02:00
expect(res.body.results[0]).toStrictEqual(pick(['id', 'name'], data.products[1]));
});
});