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

437 lines
11 KiB
JavaScript
Raw Permalink 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;
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',
},
one_to_many_articles: {
type: 'relation',
relation: 'oneToMany',
target: 'api::article.article',
targetAttribute: 'label',
},
dz: {
type: 'dynamiczone',
components: ['default.component-with-relation'],
},
2023-02-11 21:59:58 +01:00
},
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 componentWithOneToManyRelation = {
displayName: 'component-with-relation',
attributes: {
name: {
type: 'string',
},
articles: {
type: 'relation',
relation: 'oneToMany',
target: 'api::article.article',
},
},
};
const labels = [
{ name: 'label 1', documentId: 'label-1', publishedAt: new Date() },
{ name: 'label 1', documentId: 'label-1', publishedAt: null },
{ name: 'label 2', documentId: 'label-2', publishedAt: new Date() },
{ name: 'label 2', documentId: 'label-2', publishedAt: null },
];
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);
2023-02-11 21:59:58 +01:00
return [
{
name: 'article 1',
documentId: 'article-1',
publishedAt: new Date(),
labels: labelIds,
label: labelIds[0],
},
{
name: 'article 1',
documentId: 'article-1',
publishedAt: null,
labels: labelIds,
label: labelIds[1],
},
{
name: 'article 2',
documentId: 'article-2',
publishedAt: new Date(),
labels: labelIds,
label: labelIds[0],
},
{
name: 'article 2',
documentId: 'article-2',
publishedAt: null,
labels: labelIds,
label: labelIds[1],
},
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
.addContentType(articleModel)
.addComponent(componentWithOneToManyRelation)
.addContentType(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 });
// Add dynamic zones to test data
await strapi.documents('api::label.label').update({
documentId: 'label-1',
data: {
dz: [
{
__component: 'default.component-with-relation',
articles: ['article-1'],
name: 'TEST DZ',
},
],
},
});
await strapi.documents('api::label.label').update({
documentId: 'label-2',
data: {
dz: [
{
__component: 'default.component-with-relation',
articles: ['article-2'],
name: 'TEST DZ',
},
],
},
});
await strapi.documents('api::label.label').publish({
documentId: 'label-1',
});
await strapi.documents('api::label.label').publish({
documentId: 'label-2',
});
2023-02-11 21:59:58 +01:00
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
publishedAt
2023-02-11 21:59:58 +01:00
}
}
}
}
`,
});
const { body } = res;
expect(res.statusCode).toBe(200);
expect(body.data.labels_connection.data.length).toBe(2);
expect(body.data.labels_connection.data.every((label) => label.attributes.publishedAt)).toBe(
true
);
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
publishedAt
2023-02-11 21:59:58 +01:00
}
}
}
}
}
}
}
`,
});
const { body } = res;
expect(res.statusCode).toBe(200);
expect(body.data.articles_connection.data.length).toBe(2);
expect(
body.data.articles_connection.data.every((article) => article.attributes.publishedAt)
).toBe(false);
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),
},
},
},
},
});
});
test('List labels with published 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
dz {
... on ComponentDefaultComponentWithRelation {
articles_connection {
data {
documentId
attributes {
name
publishedAt
}
}
}
}
}
one_to_many_articles_connection {
data {
documentId
attributes {
publishedAt
}
}
}
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);
// Check the manyToMany response
expect(body.data.labels_connection.data[0].attributes.articles_connection.data.length).toBe(
2
);
expect(
body.data.labels_connection.data[0].attributes.articles_connection.data.every(
(article) => article.attributes.publishedAt
)
).toBe(true);
// Check the oneToMany response
expect(
body.data.labels_connection.data[0].attributes.one_to_many_articles_connection.data.length
).toBe(2);
expect(
body.data.labels_connection.data[0].attributes.one_to_many_articles_connection.data.every(
(article) => article.attributes.publishedAt
)
).toBe(true);
// Check dynamic zone with relations
expect(body.data.labels_connection.data[0].attributes.dz.length).toBe(1);
expect(
body.data.labels_connection.data[0].attributes.dz[0].articles_connection.data.every(
(article) => article.attributes.publishedAt
)
).toBe(true);
2023-02-11 21:59:58 +01:00
});
test('List labels with draft articles', async () => {
2023-02-11 21:59:58 +01:00
const res = await graphqlQuery({
query: /* GraphQL */ `
{
labels_connection(status: DRAFT) {
2023-02-11 21:59:58 +01:00
data {
2024-03-05 19:33:27 +01:00
documentId
publishedAt
2023-02-11 21:59:58 +01:00
attributes {
name
dz {
... on ComponentDefaultComponentWithRelation {
articles_connection {
data {
documentId
attributes {
name
publishedAt
}
}
}
}
}
one_to_many_articles_connection {
data {
documentId
attributes {
publishedAt
}
}
}
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
2023-02-11 21:59:58 +01:00
}
}
}
}
}
}
}
`,
});
const { body } = res;
expect(res.statusCode).toBe(200);
// Check the manyToMany response
expect(body.data.labels_connection.data[0].attributes.articles_connection.data.length).toBe(
2
);
expect(
body.data.labels_connection.data[0].attributes.articles_connection.data.every(
(article) => article.attributes.publishedAt
)
).toBe(false);
// Check the oneToMany response
expect(
body.data.labels_connection.data[0].attributes.one_to_many_articles_connection.data.length
).toBe(2);
expect(
body.data.labels_connection.data[0].attributes.one_to_many_articles_connection.data.every(
(article) => article.attributes.publishedAt
)
).toBe(false);
// Check dynamic zone with relations
expect(body.data.labels_connection.data[0].attributes.dz.length).toBe(1);
expect(
body.data.labels_connection.data[0].attributes.dz[0].articles_connection.data.every(
(article) => article.attributes.publishedAt
)
).toBe(false);
2023-02-11 21:59:58 +01:00
});
});
});