-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.spec.ts
334 lines (311 loc) · 10.7 KB
/
client.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { afterAll, describe, expect, it, vi } from 'vitest';
import { createClient } from '@/client';
import { scalar } from '@/types';
import { trimIndent } from '@/utils';
const request = vi.fn().mockResolvedValue({});
vi.mock('graphql-request');
Object.defineProperty(await import('graphql-request'), 'GraphQLClient', {
value: vi.fn(() => ({ request })),
});
describe('client', () => {
const { mutation, query } = createClient('').withSchema({
User: {
id: 'Int!',
username: 'String!',
email: 'String',
posts: '[Post!]!',
registeredAt: 'DateTime!',
},
Post: {
id: 'Int!',
title: 'String!',
content: 'String!',
authorId: 'Int!',
},
CreateUserInput: {
username: 'String!',
password: 'String!',
registeredAt: 'DateTime!',
},
UpdateUserInput: {
'username?': 'String!',
'password?': 'String!',
},
CreatePostInput: {
title: 'String!',
content: 'String!',
authorId: 'Int!',
},
UpdatePostInput: {
'title?': 'String!',
'content?': 'String!',
},
LoginInput: {
username: 'String!',
password: 'String!',
},
LoginOutput: {
token: 'String!',
},
DateTime: scalar<string>()({
parse: (value) => new Date(value),
serialize: (value) => value.toISOString(),
}),
Query: {
userExists: [{ username: 'String!' }, '=>', 'Boolean!'],
user: [{ id: 'Int!' }, '=>', 'User'],
users: ['=>', '[User!]!'],
post: [{ id: 'Int!' }, '=>', 'Post'],
posts: ['=>', '[Post!]!'],
},
Mutation: {
login: [{ input: 'LoginInput!' }, '=>', 'LoginOutput!'],
logout: ['=>', 'Boolean!'],
createUser: [{ input: 'CreateUserInput!' }, '=>', 'User!'],
updateUser: [{ id: 'Int!', input: 'UpdateUserInput!' }, '=>', 'User!'],
removeUser: [{ id: 'Int!' }, '=>', 'User!'],
createPost: [{ input: 'CreatePostInput!' }, '=>', 'Post!'],
updatePost: [{ id: 'Int!', input: 'UpdatePostInput!' }, '=>', 'Post!'],
removePost: [{ id: 'Int!' }, '=>', 'Post!'],
},
Subscription: { postAdded: ['=>', 'Post!'] },
});
afterAll(() => {
vi.unmock('graphql-request');
});
it('should generate correct request body for simple return type with variables', async () => {
const expectedQueryString = /* GraphQL */ `
query userExists($username: String!) {
userExists(username: $username)
}
`;
const expectedVariables = { username: 'admin' };
const promise1 = query('userExists', { username: 'admin' });
const { query: queryString1, variables: vars1 } = promise1.toRequestBody();
expect(queryString1).toBe(trimIndent(expectedQueryString));
expect(vars1).toEqual(expectedVariables);
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise1;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
const promise2 = query('userExists').by({ username: 'admin' });
const { query: queryString2, variables: vars2 } = promise2.toRequestBody();
expect(queryString2).toBe(trimIndent(expectedQueryString));
expect(vars2).toEqual(expectedVariables);
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise2;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
// Abbreviated syntax when there is only one variable
const promise3 = query('userExists').byUsername('admin');
const { query: queryString3, variables: vars3 } = promise3.toRequestBody();
expect(queryString3).toBe(trimIndent(expectedQueryString));
expect(vars3).toEqual(expectedVariables);
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise3;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
});
it('should generate correct request body for object return type with variables', async () => {
const expectedQueryString1 = /* GraphQL */ `
query user($id: Int!) {
user(id: $id) {
username
email
posts {
title
}
}
}
`;
const expectedQueryString2 = /* GraphQL */ `
query user($id: Int!) {
user(id: $id) {
id
username
email
posts {
id
title
content
authorId
}
registeredAt
}
}
`;
const expectedVariables1 = { id: 1 };
const expectedQueryString3 = /* GraphQL */ `
mutation createUser($input: CreateUserInput!) {
createUser(input: $input) {
id
username
email
posts {
title
}
}
}
`;
const expectedVariables3 = {
input: {
username: 'admin',
password: 'password',
registeredAt: '2024-03-23T17:43:07.206Z',
},
};
const promise1 = query('user', { id: 1 }).select((user) => [
user.username,
user.email,
user.posts((post) => [post.title]),
]);
const { query: queryString1, variables: vars1 } = promise1.toRequestBody();
expect(queryString1).toBe(trimIndent(expectedQueryString1));
expect(vars1).toEqual(expectedVariables1);
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise1;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
const promise2 = query('user')
.select((user) => [user.username, user.email, user.posts((post) => [post.title])])
.by({ id: 1 });
const { query: queryString2, variables: vars2 } = promise2.toRequestBody();
expect(queryString2).toBe(trimIndent(expectedQueryString1));
expect(vars2).toEqual(expectedVariables1);
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise2;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
const promise3 = query('user')
.select((user) => [user.username, user.email, user.posts((post) => [post.title])])
// Abbreviated syntax when there is only one variable
.byId(1);
const { query: queryString3, variables: vars3 } = promise3.toRequestBody();
expect(queryString3).toBe(trimIndent(expectedQueryString1));
expect(vars3).toEqual(expectedVariables1);
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise3;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
// Abbreviated syntax for fetching all fields using the 2nd argument as input
const promise4 = query('user', { id: 1 });
const { query: queryString4, variables: vars4 } = promise4.toRequestBody();
expect(queryString4).toBe(trimIndent(expectedQueryString2));
expect(vars4).toEqual(expectedVariables1);
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise4;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
// Abbreviated syntax for fetching all fields using `.by` method to pass input
const promise5 = query('user').by({ id: 1 });
const { query: queryString5, variables: vars5 } = promise5.toRequestBody();
expect(queryString5).toBe(trimIndent(expectedQueryString2));
expect(vars5).toEqual(expectedVariables1);
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise5;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
// Abbreviated syntax for fetching all fields when there is only one variable
const promise6 = query('user').byId(1);
const { query: queryString6, variables: vars6 } = promise6.toRequestBody();
expect(queryString6).toBe(trimIndent(expectedQueryString2));
expect(vars6).toEqual(expectedVariables1);
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise6;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
// Test scalar serialization
const promise7 = mutation('createUser')
.select((user) => [user.id, user.username, user.email, user.posts((post) => [post.title])])
.byInput({
username: 'admin',
password: 'password',
registeredAt: new Date('2024-03-23T17:43:07.206Z'),
});
const { query: queryString7, variables: vars7 } = promise7.toRequestBody();
expect(queryString7).toBe(trimIndent(expectedQueryString3));
expect(vars7).toEqual(expectedVariables3);
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise7;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
});
it('should generate correct request body for simple return type without variables', async () => {
const expectedQueryString = /* GraphQL */ `
mutation logout {
logout
}
`;
const promise = mutation('logout');
const { query: queryString, variables: vars } = promise.toRequestBody();
expect(queryString).toBe(trimIndent(expectedQueryString));
expect(vars).toEqual({});
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
});
it('should generate correct request body for object return type without variables', async () => {
const expectedQueryString1 = /* GraphQL */ `
query users {
users {
username
email
posts {
title
}
}
}
`;
const expectedQueryString2 = /* GraphQL */ `
query users {
users {
id
username
email
posts {
id
title
content
authorId
}
registeredAt
}
}
`;
const promise1 = query('users').select((user) => [
user.username,
user.email,
user.posts((post) => [post.title]),
]);
const { query: queryString1, variables: vars1 } = promise1.toRequestBody();
expect(queryString1).toBe(trimIndent(expectedQueryString1));
expect(vars1).toEqual({});
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise1;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
// Abbreviated syntax for fetching all fields
const promise2 = query('users');
const { query: queryString2, variables: vars2 } = promise2.toRequestBody();
expect(queryString2).toBe(trimIndent(expectedQueryString2));
expect(vars2).toEqual({});
// Make sure the request is only sent once
expect(request).not.toHaveBeenCalled();
await promise2;
expect(request).toHaveBeenCalledTimes(1);
request.mockClear();
});
});