-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathjsonGraphqlExpress.spec.ts
115 lines (110 loc) · 3.4 KB
/
jsonGraphqlExpress.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import express from 'express';
import request from 'supertest';
import { beforeAll, describe, expect, test } from 'vitest';
import jsonGraphqlExpress from './jsonGraphqlExpress';
const data = {
posts: [
{
id: 1,
title: 'Lorem Ipsum',
views: 254,
user_id: 123,
},
{
id: 2,
title: 'Ut enim ad minim veniam',
views: 65,
user_id: 456,
},
{
id: 3,
title: 'Sic Dolor amet',
views: 76,
user_id: 123,
},
],
users: [
{ id: 123, name: 'John Doe' },
{ id: 456, name: 'Jane Doe' },
],
comments: [
{ id: 987, post_id: 1, body: 'Consectetur adipiscing elit' },
{ id: 995, post_id: 1, body: 'Nam molestie pellentesque dui' },
{ id: 998, post_id: 2, body: 'Sunt in culpa qui officia' },
],
};
let agent: any;
beforeAll(() => {
const app = express();
app.use('/', jsonGraphqlExpress(data));
agent = request(app);
});
const gqlAgent = (query: string, variables?: any) =>
agent.post('/').send({
query,
variables,
});
describe('integration tests', () => {
test('returns all entities by default', () =>
gqlAgent('{ allPosts { id } }').expect({
data: {
allPosts: [{ id: '1' }, { id: '2' }, { id: '3' }],
},
}));
test('filters by string using the q filter in a case-insensitive way', () =>
gqlAgent('{ allPosts(filter: { q: "lorem" }) { id } }').expect({
data: {
allPosts: [{ id: '1' }],
},
}));
test('gets an entity by id', () =>
gqlAgent('{ Post(id: 1) { id } }').expect({
data: {
Post: { id: '1' },
},
}));
test('gets all the entity fields', () =>
gqlAgent('{ Post(id: 1) { id title views user_id } }').expect({
data: {
Post: {
id: '1',
title: 'Lorem Ipsum',
views: 254,
user_id: '123',
},
},
}));
test('throws an error when asked for a non existent field', () =>
gqlAgent('{ Post(id: 1) { foo } }').expect({
errors: [
{
message: 'Cannot query field "foo" on type "Post".',
locations: [{ line: 1, column: 17 }],
},
],
}));
test('gets relationship fields', () =>
gqlAgent('{ Post(id: 1) { User { name } Comments { body }} }').expect({
data: {
Post: {
User: { name: 'John Doe' },
Comments: [
{ body: 'Consectetur adipiscing elit' },
{ body: 'Nam molestie pellentesque dui' },
],
},
},
}));
test('allows multiple mutations', () =>
gqlAgent(
'mutation{ updatePost(id:"2", title:"Foo bar", views: 200, user_id:"123") { id } }',
)
.then(() =>
gqlAgent(
'mutation{ updatePost(id:"2", title:"Foo bar", views: 200, user_id:"123") { id } }',
),
)
.then((res) =>
expect(res.body).toEqual({ data: { updatePost: { id: '2' } } }),
));
});