strapi/tests/api/plugins/graphql/dp-relations.test.api.js

315 lines
7.9 KiB
JavaScript
Raw Normal View History

2023-02-11 21:59:58 +01:00
'use strict';
// Helpers.
const { pick } = require('lodash/fp');
const { createTestBuilder } = require('api-tests/builder');
const { createStrapiInstance } = require('api-tests/strapi');
const { createAuthRequest } = require('api-tests/request');
2023-02-11 21:59:58 +01:00
const builder = createTestBuilder();
let strapi;
let rq;
let graphqlQuery;
// Utils
const selectFields = pick(['name']);
2024-03-05 19:33:27 +01:00
const articleModel = {
2023-02-11 21:59:58 +01:00
attributes: {
name: {
type: 'richtext',
},
},
2024-03-13 14:08:07 +01:00
draftAndPublish: true,
2024-03-05 19:33:27 +01:00
singularName: 'article',
pluralName: 'articles',
displayName: 'Article',
2023-02-11 21:59:58 +01:00
description: '',
collectionName: '',
};
const labelModel = {
attributes: {
name: {
type: 'richtext',
},
2024-03-05 19:33:27 +01:00
articles: {
2023-02-11 21:59:58 +01:00
type: 'relation',
relation: 'manyToMany',
2024-03-05 19:33:27 +01:00
target: 'api::article.article',
2023-02-11 21:59:58 +01:00
targetAttribute: 'labels',
},
},
2024-03-13 14:08:07 +01:00
draftAndPublish: true,
2023-02-11 21:59:58 +01:00
singularName: 'label',
pluralName: 'labels',
displayName: 'Label',
description: '',
collectionName: '',
};
const labels = [{ name: 'label 1' }, { name: 'label 2' }];
2024-03-05 19:33:27 +01:00
const articles = ({ label: labels }) => {
2023-02-11 21:59:58 +01:00
const labelIds = labels.map((label) => label.id);
return [
2024-03-05 19:33:27 +01:00
{ name: 'article 1', documentId: 'article-1', publishedAt: new Date(), labels: labelIds },
{ name: 'article 1', documentId: 'article-1', publishedAt: null, labels: labelIds },
{ name: 'article 2', documentId: 'article-2', publishedAt: new Date(), labels: labelIds },
{ name: 'article 2', documentId: 'article-2', publishedAt: null, labels: labelIds },
2023-02-11 21:59:58 +01:00
];
};
describe('Test Graphql Relations with Draft and Publish enabled', () => {
const data = {
labels: [],
2024-03-05 19:33:27 +01:00
articles: [],
2023-02-11 21:59:58 +01:00
};
beforeAll(async () => {
await builder
2024-03-05 19:33:27 +01:00
.addContentTypes([articleModel, labelModel])
2023-02-11 21:59:58 +01:00
.addFixtures(labelModel.singularName, labels)
2024-03-05 19:33:27 +01:00
.addFixtures(articleModel.singularName, articles)
2023-02-11 21:59:58 +01:00
.build();
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
graphqlQuery = (body) => {
return rq({
url: '/graphql',
method: 'POST',
body,
});
};
});
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
describe('Test relations features', () => {
test('List labels', async () => {
const res = await graphqlQuery({
query: /* GraphQL */ `
{
2024-03-05 19:33:27 +01:00
labels_connection {
2023-02-11 21:59:58 +01:00
data {
2024-03-05 19:33:27 +01:00
documentId
2023-02-11 21:59:58 +01:00
attributes {
name
}
}
}
}
`,
});
const { body } = res;
expect(res.statusCode).toBe(200);
expect(body).toMatchObject({
data: {
2024-03-05 19:33:27 +01:00
labels_connection: {
data: labels.map((label) => ({
documentId: expect.any(String),
attributes: pick('name', label),
})),
2023-02-11 21:59:58 +01:00
},
},
});
// assign for later use
2024-03-05 19:33:27 +01:00
data.labels = data.labels.concat(res.body.data.labels_connection.data);
2023-02-11 21:59:58 +01:00
});
2024-03-05 19:33:27 +01:00
test('List preview articles with labels', async () => {
2023-02-11 21:59:58 +01:00
const res = await graphqlQuery({
query: /* GraphQL */ `
{
2024-03-05 19:33:27 +01:00
articles_connection(status: DRAFT) {
2023-02-11 21:59:58 +01:00
data {
2024-03-05 19:33:27 +01:00
documentId
2023-02-11 21:59:58 +01:00
attributes {
name
labels {
data {
2024-03-05 19:33:27 +01:00
documentId
2023-02-11 21:59:58 +01:00
attributes {
name
}
}
}
}
}
}
}
`,
});
const { body } = res;
expect(res.statusCode).toBe(200);
expect(body).toMatchObject({
data: {
2024-03-05 19:33:27 +01:00
articles_connection: {
data: expect.arrayContaining(data.articles),
2023-02-11 21:59:58 +01:00
},
},
});
// assign for later use
2024-03-05 19:33:27 +01:00
data.articles = res.body.data.articles_connection.data;
2023-02-11 21:59:58 +01:00
});
2024-03-05 19:33:27 +01:00
test('Publish article', async () => {
2023-02-11 21:59:58 +01:00
const res = await graphqlQuery({
query: /* GraphQL */ `
2024-03-05 19:33:27 +01:00
mutation publishArticle($documentId: ID!, $data: ArticleInput!) {
updateArticle(documentId: $documentId, data: $data, status: PUBLISHED) {
2023-02-11 21:59:58 +01:00
data {
2024-03-05 19:33:27 +01:00
documentId
2023-02-11 21:59:58 +01:00
attributes {
name
publishedAt
}
}
}
}
`,
variables: {
2024-03-05 19:33:27 +01:00
documentId: data.articles[0].documentId,
data: {},
2023-02-11 21:59:58 +01:00
},
});
const { body } = res;
expect(res.statusCode).toBe(200);
expect(body).toMatchObject({
data: {
2024-03-05 19:33:27 +01:00
updateArticle: {
2023-02-11 21:59:58 +01:00
data: {
2024-03-05 19:33:27 +01:00
documentId: data.articles[0].documentId,
2023-02-11 21:59:58 +01:00
attributes: {
2024-03-05 19:33:27 +01:00
name: data.articles[0].attributes.name,
2023-02-11 21:59:58 +01:00
publishedAt: expect.any(String),
},
},
},
},
});
});
2024-03-05 19:33:27 +01:00
test('List labels with live articles', async () => {
2023-02-11 21:59:58 +01:00
const res = await graphqlQuery({
query: /* GraphQL */ `
{
2024-03-05 19:33:27 +01:00
labels_connection {
2023-02-11 21:59:58 +01:00
data {
2024-03-05 19:33:27 +01:00
documentId
2023-02-11 21:59:58 +01:00
attributes {
name
2024-03-05 19:33:27 +01:00
articles_connection {
2023-02-11 21:59:58 +01:00
data {
2024-03-05 19:33:27 +01:00
documentId
2023-02-11 21:59:58 +01:00
attributes {
name
publishedAt
}
}
}
}
}
}
}
`,
});
const { body } = res;
expect(res.statusCode).toBe(200);
expect(body).toMatchObject({
data: {
2024-03-05 19:33:27 +01:00
labels_connection: {
2023-02-11 21:59:58 +01:00
data: expect.arrayContaining(
data.labels.map((label) => ({
2024-03-05 19:33:27 +01:00
documentId: label.documentId,
2023-02-11 21:59:58 +01:00
attributes: {
...label.attributes,
2024-03-05 19:33:27 +01:00
articles_connection: {
2023-02-11 21:59:58 +01:00
data: expect.arrayContaining(
2024-03-05 19:33:27 +01:00
// Only the first article is published
data.articles.slice(0, 1).map((article) => ({
documentId: article.documentId,
2023-02-11 21:59:58 +01:00
attributes: {
2024-03-05 19:33:27 +01:00
...selectFields(article.attributes),
2023-02-11 21:59:58 +01:00
publishedAt: expect.any(String),
},
}))
),
},
},
}))
),
},
},
});
});
2024-03-05 19:33:27 +01:00
test('List labels with preview articles', async () => {
2023-02-11 21:59:58 +01:00
const res = await graphqlQuery({
query: /* GraphQL */ `
{
2024-03-05 19:33:27 +01:00
labels_connection {
2023-02-11 21:59:58 +01:00
data {
2024-03-05 19:33:27 +01:00
documentId
2023-02-11 21:59:58 +01:00
attributes {
name
2024-03-05 19:33:27 +01:00
articles_connection {
2023-02-11 21:59:58 +01:00
data {
2024-03-05 19:33:27 +01:00
documentId
2023-02-11 21:59:58 +01:00
attributes {
name
}
}
}
}
}
}
}
`,
});
const { body } = res;
expect(res.statusCode).toBe(200);
expect(body).toMatchObject({
data: {
2024-03-05 19:33:27 +01:00
labels_connection: {
2023-02-11 21:59:58 +01:00
data: expect.arrayContaining(
data.labels.map((label) => ({
2024-03-05 19:33:27 +01:00
documentId: label.documentId,
2023-02-11 21:59:58 +01:00
attributes: {
...label.attributes,
2024-03-05 19:33:27 +01:00
articles_connection: {
2023-02-11 21:59:58 +01:00
data: expect.arrayContaining(
2024-03-05 19:33:27 +01:00
// All articles should be returned, even if they are not published
data.articles.map((article) => ({
documentId: article.documentId,
attributes: selectFields(article.attributes),
2023-02-11 21:59:58 +01:00
}))
),
},
},
}))
),
},
},
});
});
});
});