From ee9df65e67d371bbb84857feab23312ecc07841f Mon Sep 17 00:00:00 2001 From: Convly Date: Fri, 24 Jan 2025 11:27:25 +0100 Subject: [PATCH] test: filtering on components and associations --- .../plugins/graphql/components.test.api.js | 26 +++++----- .../api/plugins/graphql/relations.test.api.js | 47 +++++++++++++++++++ 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/tests/api/plugins/graphql/components.test.api.js b/tests/api/plugins/graphql/components.test.api.js index f8b63d894c..affe350490 100644 --- a/tests/api/plugins/graphql/components.test.api.js +++ b/tests/api/plugins/graphql/components.test.api.js @@ -51,6 +51,13 @@ const labelModel = { collectionName: '', }; +const COLORS = { + tomato: { name: 'tomato', red: 255, green: 99, blue: 71 }, + red: { name: 'red', red: 255, green: 0, blue: 0 }, + green: { name: 'green', red: 0, green: 255, blue: 0 }, + blue: { name: 'blue', red: 0, green: 0, blue: 255 }, +}; + describe('Test Graphql Components API End to End', () => { beforeAll(async () => { await builder.addComponent(rgbColorComponent).addContentTypes([labelModel]).build(); @@ -76,11 +83,12 @@ describe('Test Graphql Components API End to End', () => { const data = { labels: [], }; + const labelsPayload = [ { name: 'label 3, repeatable and non-repeatable', - color: { name: 'tomato', red: 255, green: 99, blue: 71 }, - colors: [{ name: 'red', red: 255, green: 0, blue: 0 }], + color: COLORS.tomato, + colors: [COLORS.red, COLORS.green, COLORS.blue], }, ]; @@ -175,16 +183,8 @@ describe('Test Graphql Components API End to End', () => { { labels_connection { data { - id attributes { - name - color { - name - red - green - blue - } - colors(filters: { red: { eq: 255 } }) { + colors(filters: { red: { eq: ${COLORS.red.red} } }) { name red green @@ -200,10 +200,10 @@ describe('Test Graphql Components API End to End', () => { const { body } = res; expect(res.statusCode).toBe(200); - expect(body).toMatchObject({ + expect(body).toStrictEqual({ data: { labels_connection: { - data: expect.arrayContaining(data.labels), + data: [{ attributes: { colors: [COLORS.red] } }], }, }, }); diff --git a/tests/api/plugins/graphql/relations.test.api.js b/tests/api/plugins/graphql/relations.test.api.js index 895f70ec7d..f9c4ee85f8 100644 --- a/tests/api/plugins/graphql/relations.test.api.js +++ b/tests/api/plugins/graphql/relations.test.api.js @@ -533,6 +533,53 @@ describe('Test Graphql Relations API End to End', () => { }); }); + test('Relation filtering', async () => { + const res = await graphqlQuery({ + query: /* GraphQL */ ` + { + articles_connection { + data { + documentId + attributes { + name + labels_connection(filters: { name: { endsWith: "Color" } }) { + data { + documentId + attributes { + name + color { + name + red + green + blue + } + } + } + } + } + } + } + } + `, + }); + + const { body } = res; + + expect(res.statusCode).toBe(200); + expect(body.data?.articles_connection?.data).toBeInstanceOf(Array); + + body.data.articles_connection.data.forEach((article) => { + const { data: labels } = article.attributes.labels_connection; + expect(labels).toBeInstanceOf(Array); + + for (const label of labels) { + expect(label.attributes).toMatchObject( + expect.objectContaining({ name: expect.stringMatching(/.*Color$/) }) + ); + } + }); + }); + test('Update Article relations removes correctly a relation', async () => { const article = data.articles[0]; const labels = [data.labels[0]];