2021-03-29 17:49:31 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { pick } = require('lodash/fp');
|
|
|
|
|
2023-04-05 10:32:20 +02:00
|
|
|
const { createTestBuilder } = require('api-tests/builder');
|
|
|
|
const { createStrapiInstance } = require('api-tests/strapi');
|
|
|
|
const { createAuthRequest } = require('api-tests/request');
|
2021-03-29 17:49:31 +02:00
|
|
|
|
|
|
|
let strapi;
|
|
|
|
let rq;
|
2022-08-08 15:50:34 +02:00
|
|
|
const data = {
|
2021-03-29 17:49:31 +02:00
|
|
|
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',
|
2021-03-29 17:49:31 +02:00
|
|
|
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',
|
2021-03-29 17:49:31 +02:00
|
|
|
targetAttribute: 'shops',
|
|
|
|
},
|
|
|
|
},
|
2021-09-13 16:57:04 +02:00
|
|
|
displayName: 'Shop',
|
|
|
|
singularName: 'shop',
|
|
|
|
pluralName: 'shops',
|
2021-03-29 17:49:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const shops = [
|
|
|
|
{
|
|
|
|
name: 'market',
|
|
|
|
locale: 'en',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const products = ({ shop }) => {
|
|
|
|
const shops = [shop[0].id];
|
|
|
|
|
|
|
|
const entries = [
|
|
|
|
{
|
|
|
|
name: 'pomodoro',
|
|
|
|
shops,
|
|
|
|
locale: 'it',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'apple',
|
|
|
|
shops,
|
|
|
|
locale: 'en',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
};
|
|
|
|
|
2022-09-01 17:24:34 +02:00
|
|
|
describe('i18n - Find available relations', () => {
|
2021-03-29 17:49:31 +02:00
|
|
|
const builder = createTestBuilder();
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await builder
|
|
|
|
.addContentTypes([productModel, shopModel])
|
2021-08-09 11:06:34 +02:00
|
|
|
.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)
|
2021-03-29 17:49:31 +02:00
|
|
|
.build();
|
|
|
|
|
|
|
|
strapi = await createStrapiInstance();
|
|
|
|
rq = await createAuthRequest({ strapi });
|
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
data.shops = await builder.sanitizedFixturesFor(shopModel.singularName, strapi);
|
|
|
|
data.products = await builder.sanitizedFixturesFor(productModel.singularName, strapi);
|
2021-03-29 17:49:31 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await strapi.destroy();
|
|
|
|
await builder.cleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can filter on default locale', async () => {
|
|
|
|
const res = await rq({
|
2022-08-10 19:14:04 +02:00
|
|
|
method: 'GET',
|
2021-08-06 18:09:49 +02:00
|
|
|
url: '/content-manager/relations/api::shop.shop/products',
|
2021-03-29 17:49:31 +02:00
|
|
|
});
|
|
|
|
|
2022-08-10 19:14:04 +02:00
|
|
|
expect(res.body.results).toHaveLength(1);
|
2023-10-24 10:57:31 +02:00
|
|
|
expect(res.body.results[0]).toStrictEqual(
|
|
|
|
pick(['id', 'name', 'publishedAt'], data.products[1])
|
|
|
|
);
|
2021-03-29 17:49:31 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Can filter on any locale', async () => {
|
|
|
|
const res = await rq({
|
2022-08-10 19:14:04 +02:00
|
|
|
method: 'GET',
|
2021-08-06 18:09:49 +02:00
|
|
|
url: '/content-manager/relations/api::shop.shop/products',
|
2021-07-19 16:47:24 +02:00
|
|
|
qs: { locale: 'it' },
|
2021-03-29 17:49:31 +02:00
|
|
|
});
|
|
|
|
|
2022-08-10 19:14:04 +02:00
|
|
|
expect(res.body.results).toHaveLength(1);
|
2023-10-24 10:57:31 +02:00
|
|
|
expect(res.body.results[0]).toStrictEqual(
|
|
|
|
pick(['id', 'name', 'publishedAt'], data.products[0])
|
|
|
|
);
|
2021-03-29 17:49:31 +02:00
|
|
|
});
|
|
|
|
});
|