strapi/tests/api/plugins/graphql/fields/datetime.test.api.js

151 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

2022-03-04 18:22:36 -03:00
'use strict';
// Helpers.
const { createTestBuilder } = require('api-tests/builder');
const { createStrapiInstance } = require('api-tests/strapi');
const { createAuthRequest } = require('api-tests/request');
2022-03-04 18:22:36 -03:00
const builder = createTestBuilder();
let strapi;
let rq;
let graphqlQuery;
const postModel = {
attributes: {
myDatetime: {
type: 'datetime',
},
},
singularName: 'post',
pluralName: 'posts',
displayName: 'Post',
description: '',
collectionName: '',
};
describe('Test Graphql API End to End', () => {
beforeAll(async () => {
await builder.addContentType(postModel).build();
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
2022-08-08 23:33:39 +02:00
graphqlQuery = (body) => {
2022-03-04 18:22:36 -03:00
return rq({
url: '/graphql',
method: 'POST',
body,
});
};
});
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
describe('GraphQL - Datetime field', () => {
test.each(['2022-03-17T15:06:57.000Z', null])(
'Can create an entity with datetime equals: %s',
2022-08-08 23:33:39 +02:00
async (value) => {
2022-03-04 18:22:36 -03:00
const res = await graphqlQuery({
query: /* GraphQL */ `
mutation createPost($data: PostInput!) {
createPost(data: $data) {
data {
attributes {
myDatetime
}
}
}
}
`,
variables: {
data: {
myDatetime: value,
},
},
});
const { body } = res;
expect(res.statusCode).toBe(200);
expect(body).toEqual({
data: {
createPost: {
data: {
attributes: { myDatetime: value },
},
},
},
});
}
);
test.each(['2022-03-17', {}, [], 'something'])(
'Cannot create an entity with datetime equals: %s',
2022-08-08 23:33:39 +02:00
async (value) => {
2022-03-04 18:22:36 -03:00
const res = await graphqlQuery({
query: /* GraphQL */ `
mutation createPost($data: PostInput!) {
createPost(data: $data) {
data {
attributes {
myDatetime
}
}
}
}
`,
variables: {
data: {
myDatetime: value,
},
},
});
const { body } = res;
expect(res.statusCode).toBe(400);
expect(body).toMatchObject({
errors: [
{
extensions: { code: 'BAD_USER_INPUT' },
},
],
});
}
);
2022-08-08 23:33:39 +02:00
test.each(['2022-03-17T15:06:57.878Z'])('Can filter query with datetime: %s', async (value) => {
2022-03-04 18:22:36 -03:00
const res = await graphqlQuery({
query: /* GraphQL */ `
query posts($myDatetime: DateTime!) {
2024-03-05 19:33:27 +01:00
posts_connection(filters: { myDatetime: { gt: $myDatetime } }) {
2022-03-04 18:22:36 -03:00
data {
attributes {
myDatetime
}
}
}
}
`,
variables: {
myDatetime: value,
2022-03-04 18:22:36 -03:00
},
});
const { body } = res;
expect(res.statusCode).toBe(200);
expect(body).toEqual({
data: {
2024-03-05 19:33:27 +01:00
posts_connection: {
data: [],
2022-03-04 18:22:36 -03:00
},
},
});
});
});
});