From d4b8c00a5c17d6225547a01df67a34bfb627b78e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20No=C3=ABl?= Date: Thu, 13 Oct 2022 14:42:53 +0200 Subject: [PATCH] use data format for xToOne relations in findExisting --- .../server/controllers/relations.js | 5 +- .../tests/find-existing-relations.test.e2e.js | 10 ++- .../server/tests/index.test.e2e.js | 73 +++++++++---------- 3 files changed, 42 insertions(+), 46 deletions(-) diff --git a/packages/core/content-manager/server/controllers/relations.js b/packages/core/content-manager/server/controllers/relations.js index aabdf84b93..9a299146e3 100644 --- a/packages/core/content-manager/server/controllers/relations.js +++ b/packages/core/content-manager/server/controllers/relations.js @@ -198,11 +198,8 @@ module.exports = { ctx.body = res; } else { const result = await strapi.entityService.load(model, { id }, targetField, queryParams); - // const result = await strapi.db.query(sourceModelUid).load({ id }, targetField, queryParams); - // TODO: Temporary fix (use data instead) ctx.body = { - results: result ? [result] : [], - pagination: { page: 1, pageSize: 5, pageCount: 1, total: 1 }, + data: result, }; } }, diff --git a/packages/core/content-manager/server/tests/find-existing-relations.test.e2e.js b/packages/core/content-manager/server/tests/find-existing-relations.test.e2e.js index bb7503ab8b..48b1546934 100644 --- a/packages/core/content-manager/server/tests/find-existing-relations.test.e2e.js +++ b/packages/core/content-manager/server/tests/find-existing-relations.test.e2e.js @@ -203,7 +203,7 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) => }, ]); } else { - expect(res.body.results[0]).toMatchObject({ + expect(res.body.data).toMatchObject({ id: expect.any(Number), name: 'Skate', ...addPublishedAtCheck(expect.any(String)), @@ -218,7 +218,11 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) => }); expect(res.status).toBe(200); - expect(res.body.results).toHaveLength(0); + if (isManyRelation) { + expect(res.body.results).toHaveLength(0); + } else { + expect(res.body.data).toBe(null); + } }); if (isManyRelation) { @@ -274,7 +278,7 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) => }, ]); } else { - expect(res.body.results[0]).toMatchObject({ + expect(res.body.data).toMatchObject({ id: expect.any(Number), name: 'Skate', ...addPublishedAtCheck(expect.any(String)), diff --git a/packages/core/content-manager/server/tests/index.test.e2e.js b/packages/core/content-manager/server/tests/index.test.e2e.js index 865927588f..c116875e90 100644 --- a/packages/core/content-manager/server/tests/index.test.e2e.js +++ b/packages/core/content-manager/server/tests/index.test.e2e.js @@ -22,7 +22,8 @@ const getRelations = async (modelName, field, id) => { method: 'GET', url: `/content-manager/relations/api::${modelName}.${modelName}/${id}/${field}`, }); - return res.body.results || res.body; + + return res.body; }; const deleteFixtures = async () => { @@ -177,7 +178,7 @@ describe('Relations', () => { }); expect(body.publishedAt).toBeUndefined(); - const tags = await getRelations('article', 'tags', body.id); + const tags = (await getRelations('article', 'tags', body.id)).results; expect(tags.length).toBe(0); }); @@ -209,7 +210,7 @@ describe('Relations', () => { }); expect(body.publishedAt).toBeUndefined(); - const tags = await getRelations('article', 'tags', body.id); + const tags = (await getRelations('article', 'tags', body.id)).results; expect(tags.length).toBe(1); expect(tags[0].id).toBe(data.tags[0].id); }); @@ -240,7 +241,7 @@ describe('Relations', () => { }); expect(body.publishedAt).toBeUndefined(); - const tags = await getRelations('article', 'tags', body.id); + const tags = (await getRelations('article', 'tags', body.id)).results; expect(tags.length).toBe(1); expect(tags[0].id).toBe(data.tags[1].id); }); @@ -267,7 +268,7 @@ describe('Relations', () => { }); expect(body.publishedAt).toBeUndefined(); - const tags = await getRelations('article', 'tags', body.id); + const tags = (await getRelations('article', 'tags', body.id)).results; expect(tags.length).toBe(3); }); @@ -293,7 +294,7 @@ describe('Relations', () => { }); expect(body.publishedAt).toBeUndefined(); - const tags = await getRelations('article', 'tags', body.id); + const tags = (await getRelations('article', 'tags', body.id)).results; expect(tags.length).toBe(2); }); @@ -323,7 +324,7 @@ describe('Relations', () => { }); expect(body.publishedAt).toBeUndefined(); - const tags = await getRelations('article', 'tags', body.id); + const tags = (await getRelations('article', 'tags', body.id)).results; expect(tags.length).toBe(0); }); @@ -430,7 +431,7 @@ describe('Relations', () => { username: null, }); - const tags = await getRelations('articlewithtag', 'tags', body.id); + const tags = (await getRelations('articlewithtag', 'tags', body.id)).results; expect(tags.length).toBe(1); expect(tags[0].id).toBe(data.tags[0].id); }); @@ -471,7 +472,7 @@ describe('Relations', () => { }); expect(body.publishedAt).toBeUndefined(); - const articles = await getRelations('category', 'articles', body.id); + const articles = (await getRelations('category', 'articles', body.id)).results; expect(articles.length).toBe(0); }); @@ -497,7 +498,7 @@ describe('Relations', () => { username: null, }); expect(body.publishedAt).toBeUndefined(); - const articles = await getRelations('category', 'articles', body.id); + const articles = (await getRelations('category', 'articles', body.id)).results; expect(articles.length).toBe(0); }); @@ -529,11 +530,11 @@ describe('Relations', () => { }); expect(body.publishedAt).toBeUndefined(); - const tags = await getRelations('article', 'tags', body.id); + const tags = (await getRelations('article', 'tags', body.id)).results; expect(tags.length).toBe(0); - const category = await getRelations('article', 'category', body.id); - expect(category[0].name).toBe(data.categories[0].name); + const category = (await getRelations('article', 'category', body.id)).data; + expect(category.name).toBe(data.categories[0].name); }); test('Update article1 with cat2', async () => { @@ -559,11 +560,11 @@ describe('Relations', () => { username: null, }); - const tags = await getRelations('article', 'tags', body.id); + const tags = (await getRelations('article', 'tags', body.id)).results; expect(tags.length).toBe(0); - const category = await getRelations('article', 'category', body.id); - expect(category[0].name).toBe(data.categories[1].name); + const category = (await getRelations('article', 'category', body.id)).data; + expect(category.name).toBe(data.categories[1].name); }); test('Create article2', async () => { @@ -592,7 +593,7 @@ describe('Relations', () => { username: null, }); - const tags = await getRelations('article', 'tags', body.id); + const tags = (await getRelations('article', 'tags', body.id)).results; expect(tags.length).toBe(0); }); @@ -619,11 +620,11 @@ describe('Relations', () => { username: null, }); - const tags = await getRelations('article', 'tags', body.id); + const tags = (await getRelations('article', 'tags', body.id)).results; expect(tags.length).toBe(0); - const category = await getRelations('article', 'category', body.id); - expect(category[0].name).toBe(data.categories[1].name); + const category = (await getRelations('article', 'category', body.id)).data; + expect(category.name).toBe(data.categories[1].name); }); test('Update cat1 with article1', async () => { @@ -648,7 +649,7 @@ describe('Relations', () => { username: null, }); - const articles = await getRelations('category', 'articles', body.id); + const articles = (await getRelations('category', 'articles', body.id)).results; expect(articles.length).toBe(1); }); @@ -677,7 +678,7 @@ describe('Relations', () => { username: null, }); - const articles = await getRelations('category', 'articles', body.id); + const articles = (await getRelations('category', 'articles', body.id)).results; expect(articles.length).toBe(1); }); @@ -687,10 +688,7 @@ describe('Relations', () => { method: 'GET', }); - expect(body).toMatchObject({ - results: [{ name: 'cat3' }], - pagination: { page: 1, pageSize: 5, pageCount: 1, total: 1 }, - }); + expect(body).toMatchObject({ data: { name: 'cat3' } }); }); test('Get article2 with cat2', async () => { @@ -699,10 +697,7 @@ describe('Relations', () => { method: 'GET', }); - expect(body).toMatchObject({ - results: [{ name: 'cat2' }], - pagination: { page: 1, pageSize: 5, pageCount: 1, total: 1 }, - }); + expect(body).toMatchObject({ data: { name: 'cat2' } }); }); test('Get cat1 without relations', async () => { @@ -833,8 +828,8 @@ describe('Relations', () => { username: null, }); - const reference = await getRelations('article', 'reference', body.id); - expect(reference[0].id).toBe(data.references[0].id); + const reference = (await getRelations('article', 'reference', body.id)).data; + expect(reference.id).toBe(data.references[0].id); }); test('Create article2 with ref1', async () => { @@ -863,8 +858,8 @@ describe('Relations', () => { id: 1, username: null, }); - const reference = await getRelations('article', 'reference', body.id); - expect(reference[0].id).toBe(data.references[0].id); + const reference = (await getRelations('article', 'reference', body.id)).data; + expect(reference.id).toBe(data.references[0].id); }); }); @@ -889,8 +884,8 @@ describe('Relations', () => { expect(createdReference.id).toBeDefined(); - const tag = await getRelations('reference', 'tag', createdReference.id); - expect(tag[0].id).toBe(createdTag.id); + const tag = (await getRelations('reference', 'tag', createdReference.id)).data; + expect(tag.id).toBe(createdTag.id); }); test('Detach Tag to a Reference', async () => { @@ -911,8 +906,8 @@ describe('Relations', () => { }, }); - let tag = await getRelations('reference', 'tag', createdReference.id); - expect(tag[0].id).toBe(createdTag.id); + let tag = (await getRelations('reference', 'tag', createdReference.id)).data; + expect(tag.id).toBe(createdTag.id); const { body: referenceToUpdate } = await rq({ url: `/content-manager/collection-types/api::reference.reference/${createdReference.id}`, @@ -922,7 +917,7 @@ describe('Relations', () => { }, }); - tag = await getRelations('reference', 'tag', referenceToUpdate.id); + tag = (await getRelations('reference', 'tag', referenceToUpdate.id)).results; expect(isEmpty(tag)).toBe(true); });