-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.spec.ts
611 lines (532 loc) · 20.6 KB
/
index.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import { Prisma, PrismaClient, Profile, Todo, User } from '@prisma/client'
import { GraphQLResolveInfo } from 'graphql'
import graphqlFields from 'graphql-fields'
import { mocked } from 'jest-mock'
import { ConnectionArguments, findManyCursorConnection } from '../src'
import { PROFILE_FIXTURES, TODO_FIXTURES, USER_FIXTURES } from './fixtures'
function encodeCursor<Cursor>(prismaCursor: Cursor): string {
return Buffer.from(JSON.stringify(prismaCursor)).toString('base64')
}
function decodeCursor<Cursor>(cursor: string): Cursor {
return JSON.parse(Buffer.from(cursor, 'base64').toString('ascii'))
}
jest.mock('graphql-fields')
const mockGraphqlFields = mocked(graphqlFields)
describe('prisma-relay-cursor-connection', () => {
jest.setTimeout(10000)
let client: PrismaClient
beforeAll(() => {
client = new PrismaClient()
})
afterAll(async () => {
await client.$disconnect()
})
describe('string id', () => {
beforeAll(async () => {
await client.todo.deleteMany({})
// Build up the fixtures sequentially so they are in a consistent order
for (let i = 0; i !== TODO_FIXTURES.length; i++) {
await client.todo.create({ data: TODO_FIXTURES[i] })
}
})
test('returns all todos with the base client (sanity check)', async () => {
const result = await client.todo.findMany({})
expect(result).toEqual(TODO_FIXTURES)
})
test('returns the paginated todos with the base client (sanity check)', async () => {
const result = await client.todo.findMany({ cursor: { id: 'id_05' }, take: 5, skip: 1 })
expect(result).toMatchSnapshot()
})
const VALID_CASES: Array<[string, ConnectionArguments | undefined]> = [
['returns all todos', undefined],
['returns the first 5 todos', { first: 5 }],
['returns the first 5 todos after the 1st todo', { first: 5, after: 'id_01' }],
['returns the first 5 todos after the 5th todo', { first: 5, after: 'id_05' }],
['returns the first 5 todos after the 15th todo', { first: 5, after: 'id_15' }],
['returns the first 5 todos after the 16th todo', { first: 5, after: 'id_16' }],
['returns the first 5 todos after the 20th todo', { first: 5, after: 'id_20' }],
['returns the last 5 todos', { last: 5 }],
['returns the last 5 todos before the 1st todo', { last: 5, before: 'id_01' }],
['returns the last 5 todos before the 5th todo', { last: 5, before: 'id_05' }],
['returns the last 5 todos before the 6th todo', { last: 5, before: 'id_06' }],
['returns the last 5 todos before the 16th todo', { last: 5, before: 'id_16' }],
]
test.each(VALID_CASES)('%s', async (name, connectionArgs) => {
const result = await findManyCursorConnection(
(args) => client.todo.findMany(args),
() => client.todo.count(),
connectionArgs
)
expect(result).toMatchSnapshot()
})
test('returns the first 5 completed todos', async () => {
const baseArgs = {
select: { id: true, isCompleted: true },
where: { isCompleted: true },
}
const result = await findManyCursorConnection(
(args) => client.todo.findMany({ ...args, ...baseArgs }),
() => client.todo.count({ where: baseArgs.where }),
{ first: 5 }
)
expect(result).toMatchSnapshot()
// Test that the return types work via TS
expect(result.edges[0].node.isCompleted)
// @ts-expect-error Typo in selected field
expect(result.edges[0].node.isCompletedd)
// @ts-expect-error Not selected field
expect(result.edges[0].node.text)
// Test that the return types work via TS
expect(result.nodes[0].isCompleted)
// @ts-expect-error Typo in selected field
expect(result.nodes[0].isCompletedd)
// @ts-expect-error Not selected field
expect(result.nodes[0].text)
})
})
describe('number id', () => {
beforeAll(async () => {
await client.user.deleteMany({})
// Build up the fixtures sequentially so they are in a consistent order
for (let i = 0; i !== USER_FIXTURES.length; i++) {
await client.user.create({ data: USER_FIXTURES[i] })
}
})
test('returns all users with the base client (sanity check)', async () => {
const result = await client.user.findMany({})
expect(result).toEqual(USER_FIXTURES)
})
test('returns the paginated users with the base client (sanity check)', async () => {
const result = await client.user.findMany({ cursor: { id: 5 }, take: 5, skip: 1 })
expect(result).toMatchSnapshot()
})
const NUMBER_ID_VALID_CASES: Array<[string, ConnectionArguments | undefined]> = [
['returns all users', undefined],
['returns the first 5 users', { first: 5 }],
[
'returns the first 5 users after the 1st user',
{ first: 5, after: encodeCursor({ id: 1 }) },
],
[
'returns the first 5 users after the 5th user',
{ first: 5, after: encodeCursor({ id: 5 }) },
],
[
'returns the first 5 users after the 15th user',
{ first: 5, after: encodeCursor({ id: 15 }) },
],
[
'returns the first 5 users after the 16th user',
{ first: 5, after: encodeCursor({ id: 16 }) },
],
[
'returns the first 5 users after the 20th user',
{ first: 5, after: encodeCursor({ id: 20 }) },
],
['returns the last 5 users', { last: 5 }],
[
'returns the last 5 users before the 1st user',
{ last: 5, before: encodeCursor({ id: 1 }) },
],
[
'returns the last 5 users before the 5th user',
{ last: 5, before: encodeCursor({ id: 5 }) },
],
[
'returns the last 5 users before the 6th user',
{ last: 5, before: encodeCursor({ id: 6 }) },
],
[
'returns the last 5 users before the 16th user',
{ last: 5, before: encodeCursor({ id: 16 }) },
],
]
test.each(NUMBER_ID_VALID_CASES)('%s', async (name, connectionArgs) => {
const result = await findManyCursorConnection<User, { id: number }>(
(args) => client.user.findMany(args),
() => client.user.count(),
connectionArgs,
{
getCursor: (record) => ({ id: record.id }),
decodeCursor,
encodeCursor,
}
)
expect(result).toMatchSnapshot()
})
test('returns the first 5 completed todos', async () => {
const baseArgs = {
select: { id: true, email: true },
where: { email: { contains: '@email' } },
}
const result = await findManyCursorConnection<User, { id: number }>(
(args) => client.user.findMany({ ...args, ...baseArgs }),
() => client.user.count({ where: baseArgs.where }),
{ first: 5 }
)
expect(result).toMatchSnapshot()
// Test that the return types work via TS
expect(result.edges[0].node.email)
// @ts-expect-error Typo in selected field
expect(result.edges[0].node.emmail)
// @ts-expect-error Not selected field
expect(result.edges[0].node.text)
// Test that the return types work via TS
expect(result.nodes[0].email)
// @ts-expect-error Typo in selected field
expect(result.nodes[0].emmail)
// @ts-expect-error Not selected field
expect(result.nodes[0].text)
})
})
describe('unique field', () => {
beforeAll(async () => {
await client.user.deleteMany({})
// Build up the fixtures sequentially so they are in a consistent order
for (let i = 0; i !== USER_FIXTURES.length; i++) {
await client.user.create({ data: USER_FIXTURES[i] })
}
})
test('returns all users with the base client (sanity check)', async () => {
const result = await client.user.findMany({})
expect(result).toEqual(USER_FIXTURES)
})
test('returns the paginated users with the base client (sanity check)', async () => {
const result = await client.user.findMany({
cursor: { email: '[email protected]' },
take: 5,
skip: 1,
})
expect(result).toMatchSnapshot()
})
const UNIQUE_FIELD_VALID_CASES: Array<[string, ConnectionArguments | undefined]> = [
[
'returns the first 5 users after the 1st user',
{ first: 5, after: encodeCursor({ email: '[email protected]' }) },
],
[
'returns the first 5 users after the 5th user',
{ first: 5, after: encodeCursor({ email: '[email protected]' }) },
],
[
'returns the first 5 users after the 15th user',
{ first: 5, after: encodeCursor({ email: '[email protected]' }) },
],
[
'returns the first 5 users after the 16th user',
{ first: 5, after: encodeCursor({ email: '[email protected]' }) },
],
[
'returns the first 5 users after the 20th user',
{ first: 5, after: encodeCursor({ email: '[email protected]' }) },
],
[
'returns the last 5 users before the 1st user',
{ last: 5, before: encodeCursor({ email: '[email protected]' }) },
],
[
'returns the last 5 users before the 5th user',
{ last: 5, before: encodeCursor({ email: '[email protected]' }) },
],
[
'returns the last 5 users before the 6th user',
{ last: 5, before: encodeCursor({ email: '[email protected]' }) },
],
[
'returns the last 5 users before the 16th user',
{ last: 5, before: encodeCursor({ email: '[email protected]' }) },
],
]
test.each(UNIQUE_FIELD_VALID_CASES)('%s', async (name, connectionArgs) => {
const result = await findManyCursorConnection<User, { email: string }>(
(args) => client.user.findMany(args),
() => client.user.count(),
connectionArgs,
{
getCursor: (record) => ({ email: record.email }),
decodeCursor,
encodeCursor,
}
)
expect(result).toMatchSnapshot()
})
})
describe('multi field id', () => {
beforeAll(async () => {
await client.profile.deleteMany({})
// Build up the fixtures sequentially so they are in a consistent order
for (let i = 0; i !== PROFILE_FIXTURES.length; i++) {
await client.profile.create({ data: PROFILE_FIXTURES[i] })
}
})
test('returns all profiles with the base client (sanity check)', async () => {
const result = await client.profile.findMany({})
expect(result).toEqual(PROFILE_FIXTURES)
})
test('returns the paginated profiles with the base client (sanity check)', async () => {
const result = await client.profile.findMany({
cursor: { firstname_lastname: { firstname: 'foo5', lastname: 'bar1' } },
take: 5,
skip: 1,
})
expect(result).toMatchSnapshot()
})
const MULTI_FIELD_ID_VALID_CASES: Array<[string, ConnectionArguments | undefined]> = [
['returns the first 5 profiles', { first: 5 }],
[
'returns the first 5 profiles after the 1st profile',
{
first: 5,
after: encodeCursor({ firstname_lastname: { firstname: 'foo1', lastname: 'bar1' } }),
},
],
[
'returns the first 5 profiles after the 5th profile',
{
first: 5,
after: encodeCursor({ firstname_lastname: { firstname: 'foo5', lastname: 'bar1' } }),
},
],
[
'returns the first 5 profiles after the 15th profile',
{
first: 5,
after: encodeCursor({ firstname_lastname: { firstname: 'foo15', lastname: 'bar1' } }),
},
],
[
'returns the first 5 profiles after the 16th profile',
{
first: 5,
after: encodeCursor({ firstname_lastname: { firstname: 'foo16', lastname: 'bar1' } }),
},
],
[
'returns the first 5 profiles after the 20th profile',
{
first: 5,
after: encodeCursor({ firstname_lastname: { firstname: 'foo20', lastname: 'bar1' } }),
},
],
[
'returns the last 5 profiles before the 1st profile',
{
last: 5,
before: encodeCursor({ firstname_lastname: { firstname: 'foo1', lastname: 'bar1' } }),
},
],
[
'returns the last 5 profiles before the 5th profile',
{
last: 5,
before: encodeCursor({ firstname_lastname: { firstname: 'foo5', lastname: 'bar1' } }),
},
],
[
'returns the last 5 profiles before the 6th profile',
{
last: 5,
before: encodeCursor({ firstname_lastname: { firstname: 'foo6', lastname: 'bar1' } }),
},
],
[
'returns the last 5 profiles before the 16th profile',
{
last: 5,
before: encodeCursor({ firstname_lastname: { firstname: 'foo16', lastname: 'bar1' } }),
},
],
]
test.each(MULTI_FIELD_ID_VALID_CASES)('%s', async (name, connectionArgs) => {
const result = await findManyCursorConnection<
Profile,
Pick<Prisma.ProfileWhereUniqueInput, 'firstname_lastname'>
>(
(args) => client.profile.findMany(args),
() => client.profile.count(),
connectionArgs,
{
getCursor: (record) => ({
firstname_lastname: {
firstname: record.firstname,
lastname: record.lastname,
},
}),
decodeCursor,
encodeCursor,
}
)
expect(result).toMatchSnapshot()
})
})
describe('custom edge fields', () => {
beforeAll(async () => {
await client.todo.deleteMany({})
// Build up the fixtures sequentially so they are in a consistent order
for (let i = 0; i !== TODO_FIXTURES.length; i++) {
await client.todo.create({ data: TODO_FIXTURES[i] })
}
})
test('returns all todos with the base client (sanity check)', async () => {
const result = await client.todo.findMany({})
expect(result).toEqual(TODO_FIXTURES)
})
test('returns the paginated todos with the base client (sanity check)', async () => {
const result = await client.todo.findMany({ cursor: { id: 'id_05' }, take: 5, skip: 1 })
expect(result).toMatchSnapshot()
})
const VALID_CASES: Array<[string, ConnectionArguments | undefined]> = [
['returns all todos', undefined],
['returns the first 5 todos', { first: 5 }],
['returns the first 5 todos after the 1st todo', { first: 5, after: 'id_01' }],
['returns the first 5 todos after the 5th todo', { first: 5, after: 'id_05' }],
['returns the first 5 todos after the 15th todo', { first: 5, after: 'id_15' }],
['returns the first 5 todos after the 16th todo', { first: 5, after: 'id_16' }],
['returns the first 5 todos after the 20th todo', { first: 5, after: 'id_20' }],
['returns the last 5 todos', { last: 5 }],
['returns the last 5 todos before the 1st todo', { last: 5, before: 'id_01' }],
['returns the last 5 todos before the 5th todo', { last: 5, before: 'id_05' }],
['returns the last 5 todos before the 6th todo', { last: 5, before: 'id_06' }],
['returns the last 5 todos before the 16th todo', { last: 5, before: 'id_16' }],
]
test.each(VALID_CASES)('%s', async (name, connectionArgs) => {
const result = await findManyCursorConnection<
Todo,
{ id: string },
Todo & { extraNodeField: string },
{ extraEdgeField: string; cursor: string; node: Todo & { extraNodeField: string } }
>(
(args) => client.todo.findMany(args),
() => client.todo.count(),
connectionArgs,
{
recordToEdge: (record) => ({
node: { ...record, extraNodeField: 'Foo' },
extraEdgeField: 'Bar',
}),
}
)
expect(result).toMatchSnapshot()
// Test that the node.extraNodeField return types work via TS
expect(result.edges[0]?.node.extraNodeField)
// Test that the extraEdgeField return type work via TS
expect(result.edges[0]?.extraEdgeField)
// Test that the node.extraNodeField return types work via TS
expect(result.nodes[0]?.extraNodeField)
})
})
describe('requested fields via resolveInfo', () => {
beforeAll(async () => {
await client.todo.deleteMany({})
// Build up the fixtures sequentially so they are in a consistent order
for (let i = 0; i !== TODO_FIXTURES.length; i++) {
await client.todo.create({ data: TODO_FIXTURES[i] })
}
})
test('returns all todos with the base client (sanity check)', async () => {
const result = await client.todo.findMany({})
expect(result).toEqual(TODO_FIXTURES)
})
test('returns the paginated todos with the base client (sanity check)', async () => {
const result = await client.todo.findMany({ cursor: { id: 'id_05' }, take: 5, skip: 1 })
expect(result).toMatchSnapshot()
})
const VALID_CASES: Array<[string, ConnectionArguments | undefined, Record<string, unknown>]> = [
['returns all todos (no fields)', undefined, {}],
['returns all todos (edges field)', undefined, { edges: { node: { id: 1 } } }],
['returns all todos (nodes field)', undefined, { nodes: { id: 1 } }],
['returns all todos (totalCount field)', undefined, { totalCount: 1 }],
['returns the first 5 todos (no fields)', { first: 5 }, {}],
['returns the first 5 todos (totalCount field)', { first: 5 }, { totalCount: 1 }],
['returns the last 5 todos (no fields)', { last: 5 }, {}],
['returns the last 5 todos (totalCount field)', { last: 5 }, { totalCount: 1 }],
]
test.each(VALID_CASES)('%s', async (name, connectionArgs, mockFields) => {
mockGraphqlFields.mockReturnValue(mockFields)
const result = await findManyCursorConnection(
(args) => client.todo.findMany(args),
() => client.todo.count(),
connectionArgs,
{ resolveInfo: { some: 'fake', data: 'here' } as unknown as GraphQLResolveInfo }
)
expect(result).toMatchSnapshot()
})
})
describe('invalid arguments', () => {
const INVALID_CASES: Array<[string, ConnectionArguments]> = [
['errors for invalid arguments (negative first)', { first: -5 }],
['errors for invalid arguments (negative last)', { last: -5 }],
['errors for invalid arguments (both first & last)', { first: 5, last: 5 }],
['errors for invalid arguments (both after & before)', { after: 'id_05', before: 'id_15' }],
[
'errors for invalid arguments (both after & before with first)',
{ first: 5, after: 'id_05', before: 'id_15' },
],
['errors for invalid arguments (after without first)', { after: 'id_05' }],
['errors for invalid arguments (before without last)', { before: 'id_15' }],
['errors for invalid arguments (after with last)', { last: 5, after: 'id_05' }],
['errors for invalid arguments (before with first)', { first: 5, before: 'id_15' }],
[
'errors for invalid arguments (kitchensink)',
{ first: 5, after: 'id_05', last: 5, before: 'id_15' },
],
]
test.each(INVALID_CASES)('%s', async (name, connectionArgs) => {
let error
try {
await findManyCursorConnection(
(args) => client.todo.findMany(args),
() => client.todo.count(),
connectionArgs
)
} catch (err) {
error = err
}
expect(error).not.toEqual(undefined)
expect(error).toMatchSnapshot()
})
})
})
// These are not real tests which run, but rather a way to ensure that the types are correct
export const typecheckForInferredTypes = async (): Promise<void> => {
const client = new PrismaClient()
// Default will get the inferred types from prisma
;(await findManyCursorConnection(
(args) => client.todo.findMany(args),
() => client.todo.count(),
{}
)) satisfies {
edges: Array<{ cursor: string; node: { id: string; text: string; isCompleted: boolean } }>
}
// Handles edge type additions
;(await findManyCursorConnection(
(args) => client.todo.findMany(args),
() => client.todo.count(),
{},
{
recordToEdge: (record) => ({ node: record, extraEdgeField: 'Bar' }),
}
)) satisfies {
edges: Array<{
cursor: string
node: { id: string; text: string; isCompleted: boolean }
extraEdgeField: string
}>
}
// Handles edge type additions
;(await findManyCursorConnection(
(args) => client.todo.findMany(args),
() => client.todo.count(),
{},
{
recordToEdge: (record) => ({
node: { ...record, extraNodeField: 'a' },
}),
}
)) satisfies {
edges: Array<{
cursor: string
node: { id: string; text: string; isCompleted: boolean; extraNodeField: string }
}>
}
}