Skip to content

Commit e84265d

Browse files
committed
add entity route tests
1 parent 5b29803 commit e84265d

File tree

1 file changed

+102
-18
lines changed

1 file changed

+102
-18
lines changed

src/jsonGraphqlExpress.spec.js

Lines changed: 102 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ const data = {
1212
},
1313
{
1414
id: 2,
15-
title: 'Sic Dolor amet',
15+
title: 'Ut enim ad minim veniam',
1616
views: 65,
1717
user_id: 456,
1818
},
19-
],
20-
users: [
21-
{
22-
id: 123,
23-
name: 'John Doe',
24-
},
2519
{
26-
id: 456,
27-
name: 'Jane Doe',
20+
id: 3,
21+
title: 'Sic Dolor amet',
22+
views: 76,
23+
user_id: 123,
2824
},
2925
],
26+
users: [{ id: 123, name: 'John Doe' }, { id: 456, name: 'Jane Doe' }],
27+
comments: [
28+
{ id: 987, post_id: 1, body: 'Consectetur adipiscing elit' },
29+
{ id: 995, post_id: 1, body: 'Nam molestie pellentesque dui' },
30+
{ id: 998, post_id: 2, body: 'Sunt in culpa qui officia' },
31+
],
3032
};
3133

3234
let agent;
@@ -47,21 +49,21 @@ describe('all* route', () => {
4749
it('returns all entities by default', () =>
4850
gqlAgent('{ allPosts { id } }').expect({
4951
data: {
50-
allPosts: [{ id: 1 }, { id: 2 }],
52+
allPosts: [{ id: 1 }, { id: 2 }, { id: 3 }],
5153
},
5254
}));
5355
describe('pagination', () => {
5456
it('does not paginate when page is not set', () =>
5557
gqlAgent('{ allPosts(perPage: 1) { id } }').expect({
5658
data: {
57-
allPosts: [{ id: 1 }, { id: 2 }],
59+
allPosts: [{ id: 1 }, { id: 2 }, { id: 3 }],
5860
},
5961
}));
6062
it('uses page to set page number', () =>
6163
Promise.all([
6264
gqlAgent('{ allPosts(page: 0) { id } }').expect({
6365
data: {
64-
allPosts: [{ id: 1 }, { id: 2 }],
66+
allPosts: [{ id: 1 }, { id: 2 }, { id: 3 }],
6567
},
6668
}),
6769
gqlAgent('{ allPosts(page: 1) { id } }').expect({
@@ -82,19 +84,44 @@ describe('all* route', () => {
8284
allPosts: [{ id: 2 }],
8385
},
8486
}),
87+
gqlAgent('{ allPosts(page: 2, perPage: 1) { id } }').expect({
88+
data: {
89+
allPosts: [{ id: 3 }],
90+
},
91+
}),
92+
gqlAgent('{ allPosts(page: 3, perPage: 1) { id } }').expect({
93+
data: {
94+
allPosts: [],
95+
},
96+
}),
97+
gqlAgent('{ allPosts(page: 0, perPage: 2) { id } }').expect({
98+
data: {
99+
allPosts: [{ id: 1 }, { id: 2 }],
100+
},
101+
}),
102+
gqlAgent('{ allPosts(page: 1, perPage: 2) { id } }').expect({
103+
data: {
104+
allPosts: [{ id: 3 }],
105+
},
106+
}),
107+
gqlAgent('{ allPosts(page: 2, perPage: 2) { id } }').expect({
108+
data: {
109+
allPosts: [],
110+
},
111+
}),
85112
]));
86113
});
87114
describe('sort', () => {
88115
it('sorts data using sortField for the field', () =>
89116
Promise.all([
90117
gqlAgent('{ allPosts(sortField: "views") { id } }').expect({
91118
data: {
92-
allPosts: [{ id: 2 }, { id: 1 }],
119+
allPosts: [{ id: 2 }, { id: 3 }, { id: 1 }],
93120
},
94121
}),
95122
gqlAgent('{ allPosts(sortField: "title") { id } }').expect({
96123
data: {
97-
allPosts: [{ id: 1 }, { id: 2 }],
124+
allPosts: [{ id: 1 }, { id: 3 }, { id: 2 }],
98125
},
99126
}),
100127
]));
@@ -104,19 +131,19 @@ describe('all* route', () => {
104131
'{ allPosts(sortField: "views", sortOrder: "asc") { id } }',
105132
).expect({
106133
data: {
107-
allPosts: [{ id: 2 }, { id: 1 }],
134+
allPosts: [{ id: 2 }, { id: 3 }, { id: 1 }],
108135
},
109136
}),
110137
gqlAgent(
111138
'{ allPosts(sortField: "views", sortOrder: "desc") { id } }',
112139
).expect({
113140
data: {
114-
allPosts: [{ id: 1 }, { id: 2 }],
141+
allPosts: [{ id: 1 }, { id: 3 }, { id: 2 }],
115142
},
116143
}),
117144
]));
118145
});
119-
describe('filters', () => {
146+
describe('filter', () => {
120147
it('filters by string on all text fields using the q filter', () =>
121148
gqlAgent('{ allPosts(filter: { q: "Lorem" }) { id } }').expect({
122149
data: {
@@ -140,7 +167,7 @@ describe('all* route', () => {
140167
'{ allPosts(filter: { title: "Sic Dolor amet" }) { id } }',
141168
).expect({
142169
data: {
143-
allPosts: [{ id: 2 }],
170+
allPosts: [{ id: 3 }],
144171
},
145172
}),
146173
gqlAgent('{ allPosts(filter: { views: 65 }) { id } }').expect({
@@ -158,3 +185,60 @@ describe('all* route', () => {
158185
]));
159186
});
160187
});
188+
189+
describe('Entity route', () => {
190+
it('gets an entity by id', () =>
191+
Promise.all([
192+
gqlAgent('{ Post(id: 1) { id } }').expect({
193+
data: {
194+
Post: { id: 1 },
195+
},
196+
}),
197+
gqlAgent('{ Post(id: 2) { id } }').expect({
198+
data: {
199+
Post: { id: 2 },
200+
},
201+
}),
202+
]));
203+
it('gets all the entity fields', () =>
204+
gqlAgent('{ Post(id: 1) { id title views user_id } }').expect({
205+
data: {
206+
Post: { id: 1, title: 'Lorem Ipsum', views: 254, user_id: 123 },
207+
},
208+
}));
209+
it('throws an error when asked for a non existent field', () =>
210+
gqlAgent('{ Post(id: 1) { foo } }').expect({
211+
errors: [
212+
{
213+
message: 'Cannot query field "foo" on type "Post".',
214+
locations: [{ line: 1, column: 17 }],
215+
},
216+
],
217+
}));
218+
it('gets one to many relationship fields', () =>
219+
gqlAgent('{ Post(id: 1) { User { name } } }').expect({
220+
data: {
221+
Post: { User: { name: 'John Doe' } },
222+
},
223+
}));
224+
it('gets many to one relationship fields', () =>
225+
Promise.all([
226+
gqlAgent('{ Post(id: 1) { Comments { body } } }').expect({
227+
data: {
228+
Post: {
229+
Comments: [
230+
{ body: 'Consectetur adipiscing elit' },
231+
{ body: 'Nam molestie pellentesque dui' },
232+
],
233+
},
234+
},
235+
}),
236+
gqlAgent('{ Post(id: 2) { Comments { body } } }').expect({
237+
data: {
238+
Post: {
239+
Comments: [{ body: 'Sunt in culpa qui officia' }],
240+
},
241+
},
242+
}),
243+
]));
244+
});

0 commit comments

Comments
 (0)