fix no relations return for find available when no relations are set

This commit is contained in:
Pierre Noël 2022-09-08 11:10:57 +02:00
parent 389a582e38
commit ea35195ea1
3 changed files with 81 additions and 8 deletions

View File

@ -104,7 +104,7 @@ module.exports = {
const alias = subQuery.getAlias();
const knexSubQuery = subQuery
.where({ id: entityId })
.where({ id: entityId, [`${alias}.id`]: { $notNull: true } })
.join({ alias, targetField })
.select(`${alias}.id`)
.getKnexQuery();
@ -197,7 +197,7 @@ module.exports = {
const alias = subQuery.getAlias();
const knexSubQuery = subQuery
.where({ id })
.where({ id, [`${alias}.id`]: { $notNull: true } })
.join({ alias, targetField })
.select(`${alias}.id`)
.getKnexQuery();

View File

@ -140,7 +140,7 @@ describe.each([[false], [true]])('Relations, with d&p: %p', (withDraftAndPublish
const id1 = createdProduct1.id;
const createdShop = await createEntry('api::shop.shop', {
const createdShop1 = await createEntry('api::shop.shop', {
name: 'Cazotte Shop',
products_ow: id1,
products_oo: id1,
@ -153,8 +153,16 @@ describe.each([[false], [true]])('Relations, with d&p: %p', (withDraftAndPublish
compo_products_mw: [id1],
},
});
const createdShop2 = await createEntry('api::shop.shop', {
name: 'Empty Shop',
myCompo: {
compo_products_ow: null,
compo_products_mw: [],
},
});
data.shops.push(createdShop);
data.shops.push(createdShop1);
data.shops.push(createdShop2);
});
afterAll(async () => {
@ -174,12 +182,14 @@ describe.each([[false], [true]])('Relations, with d&p: %p', (withDraftAndPublish
['compo_products_mw', true],
])('Relation not in a component (%s)', (fieldName, isComponent) => {
let entityId;
let entityIdEmptyShop;
beforeAll(() => {
entityId = isComponent ? data.shops[0].myCompo.id : data.shops[0].id;
entityIdEmptyShop = isComponent ? data.shops[1].myCompo.id : data.shops[1].id;
});
test('Can retrieve available relation(s) for an entity', async () => {
test('Can retrieve available relation(s) for an entity that have some relations', async () => {
let res = await rq({
method: 'GET',
url: `/content-manager/relations/api::shop.shop/${fieldName}`,
@ -213,6 +223,45 @@ describe.each([[false], [true]])('Relations, with d&p: %p', (withDraftAndPublish
expect(res.body.results).toHaveLength(0);
});
test("Can retrieve available relation(s) for an entity that don't have relations yet", async () => {
let res = await rq({
method: 'GET',
url: `/content-manager/relations/api::shop.shop/${fieldName}`,
qs: {
entityId: entityIdEmptyShop,
...(isComponent ? { component: 'default.compo' } : {}),
},
});
expect(res.status).toBe(200);
expect(res.body.results).toMatchObject([
{
id: expect.any(Number),
name: 'Candle',
...addPublishedAtCheck(null),
},
{
id: expect.any(Number),
name: 'Skate',
...addPublishedAtCheck(expect.any(String)),
},
]);
// can omitIds
res = await rq({
method: 'GET',
url: `/content-manager/relations/api::shop.shop/${fieldName}`,
qs: {
entityId,
idsToOmit: [data.products[1].id],
...(isComponent ? { component: 'default.compo' } : {}),
},
});
expect(res.body.results).toHaveLength(0);
});
test('Can retrieve available relation(s) without entity', async () => {
let res = await rq({
method: 'GET',

View File

@ -1,5 +1,6 @@
'use strict';
const { isEmpty } = require('lodash/fp');
const { createTestBuilder } = require('../../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
const { createAuthRequest } = require('../../../../../test/helpers/request');
@ -143,7 +144,7 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) =>
const id1 = createdProduct1.id;
const id2 = createdProduct2.id;
const createdShop = await createEntry('api::shop.shop', {
const createdShop1 = await createEntry('api::shop.shop', {
name: 'Cazotte Shop',
products_ow: id1,
products_oo: id1,
@ -156,8 +157,16 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) =>
compo_products_mw: [id1, id2],
},
});
const createdShop2 = await createEntry('api::shop.shop', {
name: 'Empty Shop',
myCompo: {
compo_products_ow: null,
compo_products_mw: [],
},
});
data.shops.push(createdShop);
data.shops.push(createdShop1);
data.shops.push(createdShop2);
});
afterAll(async () => {
@ -173,7 +182,7 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) =>
['products_mm', true],
['products_mw', true],
])('Relation not in a component (%s)', (fieldName, isManyRelation) => {
test('Can retrieve the relation(s)', async () => {
test('Can retrieve the relation(s) for an entity that have some relations', async () => {
let res = await rq({
method: 'GET',
url: `/content-manager/collection-types/api::shop.shop/${data.shops[0].id}/${fieldName}`,
@ -222,6 +231,21 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) =>
}
});
test("Can retrieve the relation(s) for an entity that don't have relations yet", async () => {
const res = await rq({
method: 'GET',
url: `/content-manager/collection-types/api::shop.shop/${data.shops[1].id}/${fieldName}`,
});
if (isManyRelation) {
expect(res.status).toBe(200);
expect(res.body.results).toHaveLength(0);
} else {
expect(res.status).toBe(204);
expect(isEmpty(res.body)).toBe(true);
}
});
if (isManyRelation) {
test("Can search ''", async () => {
const res = await rq({