-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-resolver.test.ts
115 lines (106 loc) · 2.65 KB
/
user-resolver.test.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 { gql } from "apollo-server";
import nock from "nock";
import { print } from "graphql";
import { createServer } from "../create-server";
describe("UserResolver", () => {
it("returns all users", async () => {
const usersQuery = gql`
query Users {
users {
id
name
}
}
`;
nock("https://jsonplaceholder.typicode.com/users")
.get("/")
.reply(200, [
{
id: 1,
name: "Leanne Graham",
},
{
id: 2,
name: "Ervin Howell",
},
]);
const server = await createServer();
const response = await server.executeOperation({
query: print(usersQuery),
});
expect(response.errors).toBeUndefined();
expect(response.data).toEqual({
users: [
{ id: "1", name: "Leanne Graham" },
{ id: "2", name: "Ervin Howell" },
],
});
});
it("returns user with the given id", async () => {
const userQuery = gql`
query User($userId: ID!) {
user(userId: $userId) {
id
name
}
}
`;
nock("https://jsonplaceholder.typicode.com/users").get("/1").reply(200, {
id: 1,
name: "Leanne Graham",
});
const server = await createServer();
const response = await server.executeOperation({
query: print(userQuery),
variables: { userId: "1" },
});
expect(response.errors).toBeUndefined();
expect(response.data).toEqual({ user: { id: "1", name: "Leanne Graham" } });
});
it("returns user with the given id and the posts of that user", async () => {
const userQueryWithPosts = gql`
query UserWithPosts($userId: ID!) {
user(userId: $userId) {
id
name
posts {
id
userId
title
}
}
}
`;
nock("https://jsonplaceholder.typicode.com/users").get("/1").reply(200, {
id: 1,
name: "Leanne Graham",
});
nock("https://jsonplaceholder.typicode.com/posts")
.get("/")
.reply(200, [
{
userId: 1,
id: 1,
title: "Post title",
},
{
userId: 2,
id: 2,
title: "Some unrelated post title",
},
]);
const server = await createServer();
const response = await server.executeOperation({
query: print(userQueryWithPosts),
variables: { userId: "1" },
});
expect(response.errors).toBeUndefined();
expect(response.data).toEqual({
user: {
id: "1",
name: "Leanne Graham",
posts: [{ id: "1", userId: "1", title: "Post title" }],
},
});
});
});