@openapi-qraft/react 1.0.3 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +11 -0
  3. package/dist/ServiceOperation.d.ts +48 -40
  4. package/dist/ServiceOperation.d.ts.map +1 -1
  5. package/dist/callbacks/getInfiniteQueryData.js +3 -3
  6. package/dist/callbacks/getInfiniteQueryData.js.map +1 -1
  7. package/dist/callbacks/getInfiniteQueryKey.d.ts +1 -0
  8. package/dist/callbacks/getInfiniteQueryKey.d.ts.map +1 -1
  9. package/dist/callbacks/getInfiniteQueryKey.js +1 -1
  10. package/dist/callbacks/getInfiniteQueryKey.js.map +1 -1
  11. package/dist/callbacks/getMutationKey.d.ts +1 -1
  12. package/dist/callbacks/getMutationKey.d.ts.map +1 -1
  13. package/dist/callbacks/getQueryData.js +3 -3
  14. package/dist/callbacks/getQueryData.js.map +1 -1
  15. package/dist/callbacks/getQueryKey.d.ts +1 -0
  16. package/dist/callbacks/getQueryKey.d.ts.map +1 -1
  17. package/dist/callbacks/getQueryKey.js +4 -1
  18. package/dist/callbacks/getQueryKey.js.map +1 -1
  19. package/dist/callbacks/queryFn.d.ts +5 -13
  20. package/dist/callbacks/queryFn.d.ts.map +1 -1
  21. package/dist/callbacks/queryFn.js +3 -4
  22. package/dist/callbacks/queryFn.js.map +1 -1
  23. package/dist/callbacks/setInfiniteQueryData.js +2 -2
  24. package/dist/callbacks/setInfiniteQueryData.js.map +1 -1
  25. package/dist/callbacks/setQueryData.js +3 -3
  26. package/dist/callbacks/setQueryData.js.map +1 -1
  27. package/dist/callbacks/useInfiniteQuery.d.ts.map +1 -1
  28. package/dist/callbacks/useInfiniteQuery.js +5 -2
  29. package/dist/callbacks/useInfiniteQuery.js.map +1 -1
  30. package/dist/callbacks/useQueries.js +1 -1
  31. package/dist/callbacks/useQueries.js.map +1 -1
  32. package/dist/callbacks/useQuery.js +3 -3
  33. package/dist/callbacks/useQuery.js.map +1 -1
  34. package/dist/callbacks/useSuspenseInfiniteQuery.d.ts.map +1 -1
  35. package/dist/callbacks/useSuspenseInfiniteQuery.js +5 -2
  36. package/dist/callbacks/useSuspenseInfiniteQuery.js.map +1 -1
  37. package/dist/callbacks/useSuspenseQuery.js +3 -3
  38. package/dist/callbacks/useSuspenseQuery.js.map +1 -1
  39. package/dist/qraftAPIClient.d.ts +18 -7
  40. package/dist/qraftAPIClient.d.ts.map +1 -1
  41. package/dist/qraftAPIClient.js.map +1 -1
  42. package/package.json +7 -7
  43. package/src/ServiceOperation.ts +96 -49
  44. package/src/callbacks/getInfiniteQueryData.ts +3 -3
  45. package/src/callbacks/getInfiniteQueryKey.ts +1 -1
  46. package/src/callbacks/getQueryData.ts +3 -3
  47. package/src/callbacks/getQueryKey.ts +4 -4
  48. package/src/callbacks/queryFn.ts +12 -12
  49. package/src/callbacks/setInfiniteQueryData.ts +2 -2
  50. package/src/callbacks/setQueryData.ts +3 -3
  51. package/src/callbacks/useInfiniteQuery.ts +5 -2
  52. package/src/callbacks/useQueries.ts +1 -1
  53. package/src/callbacks/useQuery.ts +3 -3
  54. package/src/callbacks/useSuspenseInfiniteQuery.ts +5 -2
  55. package/src/callbacks/useSuspenseQuery.ts +3 -3
  56. package/src/qraftAPIClient.ts +27 -15
  57. package/src/lib/composeMutationKey.test.ts +0 -43
  58. package/src/lib/shelfMerge.test.ts +0 -94
  59. package/src/mocks/handlers.ts +0 -252
  60. package/src/mocks/node.ts +0 -5
  61. package/src/qraftAPIClient.test.tsx +0 -996
  62. package/src/setupTests.ts +0 -17
@@ -1,996 +0,0 @@
1
- import React, { createContext, ReactNode } from 'react';
2
-
3
- import { QraftContext as QraftContextDist } from '@openapi-qraft/react';
4
- import {
5
- QueryClient,
6
- QueryClientProvider,
7
- useQueryClient,
8
- } from '@tanstack/react-query';
9
- import { act, renderHook, waitFor } from '@testing-library/react';
10
-
11
- import { createAPIClient } from './fixtures/api/index.js';
12
- import { bodySerializer, request, urlSerializer } from './lib/request.js';
13
- import { QraftContextValue } from './QraftContext.js';
14
- import type { RequestClient } from './RequestClient.js';
15
-
16
- const qraft = createAPIClient();
17
-
18
- const requestClient: RequestClient = async (schema, options) => {
19
- return request(
20
- {
21
- baseUrl: 'https://api.sandbox.monite.com/v1',
22
- },
23
- { ...schema, ...options },
24
- { urlSerializer, bodySerializer }
25
- );
26
- };
27
-
28
- describe('Qraft uses singular Query', () => {
29
- it('supports useQuery', async () => {
30
- const { result } = renderHook(
31
- () =>
32
- qraft.approvalPolicies.getApprovalPoliciesId.useQuery({
33
- header: {
34
- 'x-monite-version': '1.0.0',
35
- },
36
- path: {
37
- approval_policy_id: '1',
38
- },
39
- query: {
40
- items_order: ['asc', 'desc'],
41
- },
42
- }),
43
- {
44
- wrapper: Providers,
45
- }
46
- );
47
-
48
- await waitFor(() => {
49
- expect(result.current.data).toEqual({
50
- header: {
51
- 'x-monite-version': '1.0.0',
52
- },
53
- path: {
54
- approval_policy_id: '1',
55
- },
56
- query: {
57
- items_order: ['asc', 'desc'],
58
- },
59
- });
60
- });
61
- });
62
-
63
- it('supports custom context', async () => {
64
- const QraftCustomContext = createContext<QraftContextValue>(undefined);
65
-
66
- const customQraft = createAPIClient({ context: QraftCustomContext });
67
-
68
- const { result } = renderHook(
69
- () =>
70
- customQraft.approvalPolicies.getApprovalPoliciesId.useQuery({
71
- header: {
72
- 'x-monite-version': '1.0.0',
73
- },
74
- path: {
75
- approval_policy_id: '1',
76
- },
77
- query: {
78
- items_order: ['asc', 'desc'],
79
- },
80
- }),
81
- {
82
- wrapper: (props) => (
83
- <QraftCustomContext.Provider
84
- value={{
85
- requestClient(schema, options) {
86
- return request(
87
- {
88
- baseUrl: 'https://api.sandbox.monite.com/v1',
89
- },
90
- {
91
- headers: { 'x-custom-provider': 'true' },
92
- ...schema,
93
- ...options,
94
- },
95
- { urlSerializer, bodySerializer }
96
- );
97
- },
98
- }}
99
- >
100
- <Providers {...props} />
101
- </QraftCustomContext.Provider>
102
- ),
103
- }
104
- );
105
-
106
- await waitFor(() => {
107
- expect(result.current.data).toEqual({
108
- header: {
109
- 'x-monite-version': '1.0.0',
110
- 'x-custom-provider': 'true',
111
- },
112
- path: {
113
- approval_policy_id: '1',
114
- },
115
- query: {
116
- items_order: ['asc', 'desc'],
117
- },
118
- });
119
- });
120
- });
121
-
122
- it('supports useQuery with optional params', async () => {
123
- const { result } = renderHook(() => qraft.files.getFileList.useQuery({}), {
124
- wrapper: Providers,
125
- });
126
-
127
- await waitFor(() => {
128
- expect(result.current.data).toEqual(undefined);
129
- });
130
- });
131
- });
132
-
133
- describe('Qraft uses Suspense Queries', () => {
134
- it('supports useSuspenseQuery', async () => {
135
- const hook = () => {
136
- try {
137
- return qraft.approvalPolicies.getApprovalPoliciesId.useSuspenseQuery({
138
- header: {
139
- 'x-monite-version': '1.0.0',
140
- },
141
- path: {
142
- approval_policy_id: '1',
143
- },
144
- query: {
145
- items_order: ['asc', 'desc'],
146
- },
147
- });
148
- } catch (error) {
149
- return error as Promise<unknown>;
150
- }
151
- };
152
-
153
- const queryClient = new QueryClient();
154
-
155
- const { result: resultWithErrorPromise } = renderHook(hook, {
156
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
157
- });
158
-
159
- expect(resultWithErrorPromise.current).toBeInstanceOf(Promise); // Suspense throws a promise
160
-
161
- await resultWithErrorPromise.current;
162
-
163
- const { result: resultWithData } = renderHook(hook, {
164
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
165
- });
166
-
167
- if (resultWithData.current instanceof Promise) {
168
- throw new Error('Promise should be resolved');
169
- }
170
-
171
- expect(resultWithData.current.data).toEqual({
172
- header: {
173
- 'x-monite-version': '1.0.0',
174
- },
175
- path: {
176
- approval_policy_id: '1',
177
- },
178
- query: {
179
- items_order: ['asc', 'desc'],
180
- },
181
- });
182
- });
183
- });
184
-
185
- describe('Qraft uses Queries', () => {
186
- it('supports useQueries', async () => {
187
- const { result } = renderHook(
188
- () =>
189
- qraft.approvalPolicies.getApprovalPoliciesId.useQueries({
190
- queries: [
191
- {
192
- parameters: {
193
- header: {
194
- 'x-monite-version': '1.0.0',
195
- },
196
- path: {
197
- approval_policy_id: '1',
198
- },
199
- query: {
200
- items_order: ['asc', 'desc'],
201
- },
202
- },
203
- },
204
- ],
205
- combine: (results) => results.map((result) => result.data),
206
- }),
207
- {
208
- wrapper: Providers,
209
- }
210
- );
211
-
212
- await waitFor(() => {
213
- expect(result.current).toEqual([
214
- {
215
- header: {
216
- 'x-monite-version': '1.0.0',
217
- },
218
- path: {
219
- approval_policy_id: '1',
220
- },
221
- query: {
222
- items_order: ['asc', 'desc'],
223
- },
224
- },
225
- ]);
226
- });
227
- });
228
- });
229
-
230
- describe('Qraft uses Infinite Queries', () => {
231
- it('supports useInfiniteQuery', async () => {
232
- const { result } = renderHook(
233
- () =>
234
- qraft.files.getFiles.useInfiniteQuery(
235
- {
236
- header: {
237
- 'x-monite-version': '1.0.0',
238
- },
239
- query: {
240
- id__in: ['1', '2'],
241
- },
242
- },
243
- {
244
- getNextPageParam: (lastPage, allPages, params) => {
245
- return {
246
- query: {
247
- page: params.query?.page
248
- ? String(Number(params.query.page) + 1)
249
- : undefined,
250
- },
251
- };
252
- },
253
- initialPageParam: {
254
- query: {
255
- page: '1',
256
- },
257
- },
258
- }
259
- ),
260
- {
261
- wrapper: Providers,
262
- }
263
- );
264
-
265
- await waitFor(() => {
266
- expect(result.current.data).toEqual({
267
- pageParams: [
268
- {
269
- query: {
270
- page: '1',
271
- },
272
- },
273
- ],
274
- pages: [
275
- {
276
- header: {
277
- 'x-monite-version': '1.0.0',
278
- },
279
- query: {
280
- id__in: ['1', '2'],
281
- page: '1',
282
- },
283
- },
284
- ],
285
- });
286
- });
287
- });
288
-
289
- it('supports useInfiniteQuery with next page', async () => {
290
- const queryClient = new QueryClient();
291
-
292
- const { result } = renderHook(
293
- () =>
294
- qraft.files.getFiles.useInfiniteQuery(
295
- {
296
- header: {
297
- 'x-monite-version': '1.0.0',
298
- },
299
- query: {
300
- id__in: ['1', '2'],
301
- },
302
- },
303
- {
304
- getNextPageParam: (lastPage, allPages, params) => {
305
- return {
306
- query: {
307
- page: params.query?.page
308
- ? String(Number(params.query.page) + 1)
309
- : undefined,
310
- },
311
- };
312
- },
313
- initialPageParam: {
314
- query: {
315
- page: '1',
316
- },
317
- },
318
- }
319
- ),
320
- {
321
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
322
- }
323
- );
324
-
325
- await waitFor(() => {
326
- expect(result.current.isSuccess).toBeTruthy();
327
- });
328
-
329
- await act(async () => {
330
- await result.current.fetchNextPage();
331
- });
332
-
333
- const { result: result2 } = renderHook(
334
- () => {
335
- const queryClient = useQueryClient();
336
-
337
- return queryClient.getQueryData(
338
- qraft.files.getFiles.getInfiniteQueryKey({
339
- header: {
340
- 'x-monite-version': '1.0.0',
341
- },
342
- query: {
343
- id__in: ['1', '2'],
344
- },
345
- })
346
- );
347
- },
348
- {
349
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
350
- }
351
- );
352
-
353
- await waitFor(() => {
354
- expect(result2.current).toEqual({
355
- pageParams: [
356
- {
357
- query: {
358
- page: '1',
359
- },
360
- },
361
- {
362
- query: {
363
- page: '2',
364
- },
365
- },
366
- ],
367
- pages: [
368
- {
369
- header: {
370
- 'x-monite-version': '1.0.0',
371
- },
372
- query: {
373
- id__in: ['1', '2'],
374
- page: '1',
375
- },
376
- },
377
- {
378
- header: {
379
- 'x-monite-version': '1.0.0',
380
- },
381
- query: {
382
- id__in: ['1', '2'],
383
- page: '2',
384
- },
385
- },
386
- ],
387
- });
388
- });
389
- });
390
- });
391
-
392
- describe('Qraft uses Suspense Infinite Queries', () => {
393
- it('supports useSuspenseInfiniteQuery', async () => {
394
- const hook = () => {
395
- try {
396
- return qraft.files.getFiles.useSuspenseInfiniteQuery(
397
- {
398
- header: {
399
- 'x-monite-version': '1.0.0',
400
- },
401
- query: {
402
- id__in: ['1', '2'],
403
- },
404
- },
405
- {
406
- getNextPageParam: (lastPage, allPages, params) => {
407
- return {
408
- query: {
409
- page: params.query?.page
410
- ? String(Number(params.query.page) + 1)
411
- : undefined,
412
- },
413
- };
414
- },
415
- initialPageParam: {
416
- query: {
417
- page: '1',
418
- },
419
- },
420
- }
421
- );
422
- } catch (error) {
423
- return error as Promise<unknown>;
424
- }
425
- };
426
-
427
- const queryClient = new QueryClient();
428
-
429
- const { result } = renderHook(hook, {
430
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
431
- });
432
-
433
- const { result: resultWithErrorPromise } = renderHook(hook, {
434
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
435
- });
436
-
437
- expect(resultWithErrorPromise.current).toBeInstanceOf(Promise); // Suspense throws a promise
438
-
439
- await resultWithErrorPromise.current;
440
-
441
- const { result: resultWithData } = renderHook(hook, {
442
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
443
- });
444
-
445
- if (resultWithData.current instanceof Promise) {
446
- throw new Error('Promise should be resolved');
447
- }
448
-
449
- expect(resultWithData.current.data).toEqual({
450
- pageParams: [
451
- {
452
- query: {
453
- page: '1',
454
- },
455
- },
456
- ],
457
- pages: [
458
- {
459
- header: {
460
- 'x-monite-version': '1.0.0',
461
- },
462
- query: {
463
- id__in: ['1', '2'],
464
- page: '1',
465
- },
466
- },
467
- ],
468
- });
469
- });
470
- });
471
-
472
- describe('Qraft uses Mutations', () => {
473
- it('supports useMutation without predefined parameters', async () => {
474
- const { result } = renderHook(
475
- () => qraft.entities.postEntitiesIdDocuments.useMutation(),
476
- {
477
- wrapper: Providers,
478
- }
479
- );
480
-
481
- act(() => {
482
- result.current.mutate({
483
- header: {
484
- 'x-monite-version': '1.0.0',
485
- },
486
- path: {
487
- entity_id: '1',
488
- },
489
- query: {
490
- referer: 'https://example.com',
491
- },
492
- body: {
493
- verification_document_back: 'back',
494
- verification_document_front: 'front',
495
- },
496
- });
497
- });
498
-
499
- await waitFor(() => {
500
- expect(result.current.data).toEqual({
501
- header: {
502
- 'x-monite-version': '1.0.0',
503
- },
504
- path: {
505
- entity_id: '1',
506
- },
507
- query: {
508
- referer: 'https://example.com',
509
- },
510
- body: {
511
- verification_document_back: 'back',
512
- verification_document_front: 'front',
513
- },
514
- });
515
- });
516
- });
517
-
518
- it('supports useMutation with predefined parameters', async () => {
519
- const { result } = renderHook(
520
- () =>
521
- qraft.entities.postEntitiesIdDocuments.useMutation({
522
- header: {
523
- 'x-monite-version': '1.0.0',
524
- },
525
- path: {
526
- entity_id: '1',
527
- },
528
- query: {
529
- referer: 'https://example.com',
530
- },
531
- }),
532
- {
533
- wrapper: Providers,
534
- }
535
- );
536
-
537
- act(() => {
538
- result.current.mutate({
539
- verification_document_back: 'back',
540
- verification_document_front: 'front',
541
- });
542
- });
543
-
544
- await waitFor(() => {
545
- expect(result.current.data).toEqual({
546
- header: {
547
- 'x-monite-version': '1.0.0',
548
- },
549
- path: {
550
- entity_id: '1',
551
- },
552
- query: {
553
- referer: 'https://example.com',
554
- },
555
- body: {
556
- verification_document_back: 'back',
557
- verification_document_front: 'front',
558
- },
559
- });
560
- });
561
- });
562
-
563
- it('supports useMutation with form data', async () => {
564
- const { result } = renderHook(() => qraft.files.postFiles.useMutation(), {
565
- wrapper: Providers,
566
- });
567
-
568
- act(() => {
569
- result.current.mutate({
570
- body: {
571
- file: new File([''], 'file.png'),
572
- file_description: 'my file',
573
- },
574
- });
575
- });
576
-
577
- await waitFor(() => {
578
- expect(result.current.data).toEqual({
579
- body: {
580
- file: 'file.png',
581
- file_description: 'my file',
582
- },
583
- });
584
- });
585
- });
586
- });
587
-
588
- describe('Qraft uses Mutation State', () => {
589
- const parameters = {
590
- header: {
591
- 'x-monite-version': '1.0.0',
592
- },
593
- path: {
594
- entity_id: '1',
595
- },
596
- query: {
597
- referer: 'https://example.com',
598
- },
599
- } as const;
600
-
601
- it('supports useMutationState with filter', async () => {
602
- const queryClient = new QueryClient();
603
-
604
- const { result: mutationResult } = renderHook(
605
- () => qraft.entities.postEntitiesIdDocuments.useMutation(parameters),
606
- {
607
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
608
- }
609
- );
610
-
611
- act(() => {
612
- mutationResult.current.mutate({
613
- verification_document_back: 'back',
614
- verification_document_front: 'front',
615
- });
616
- });
617
-
618
- await waitFor(() => {
619
- expect(mutationResult.current.data).toBeDefined();
620
- });
621
-
622
- const { result: mutationStateResult } = renderHook(
623
- () =>
624
- qraft.entities.postEntitiesIdDocuments.useMutationState({
625
- filters: {
626
- parameters,
627
- },
628
- }),
629
- {
630
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
631
- }
632
- );
633
-
634
- expect(mutationStateResult.current.at(0)?.data).toEqual({
635
- body: {
636
- verification_document_back: 'back',
637
- verification_document_front: 'front',
638
- },
639
- ...parameters,
640
- });
641
- });
642
-
643
- it('supports useMutationState with filter and select', async () => {
644
- const queryClient = new QueryClient();
645
-
646
- const { result: mutationResult } = renderHook(
647
- () => qraft.entities.postEntitiesIdDocuments.useMutation(parameters),
648
- {
649
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
650
- }
651
- );
652
-
653
- act(() => {
654
- mutationResult.current.mutate({
655
- verification_document_back: 'back',
656
- verification_document_front: 'front',
657
- });
658
- });
659
-
660
- await waitFor(() => {
661
- expect(mutationResult.current.data).toBeDefined();
662
- });
663
-
664
- const { result: mutationStateResult } = renderHook(
665
- () =>
666
- qraft.entities.postEntitiesIdDocuments.useMutationState({
667
- filters: {
668
- parameters,
669
- },
670
- select(mutation) {
671
- return (
672
- mutation.state.data?.header?.['x-monite-version'] ===
673
- parameters.header['x-monite-version']
674
- );
675
- },
676
- }),
677
- {
678
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
679
- }
680
- );
681
-
682
- expect(mutationStateResult.current.at(0)).toEqual(true);
683
- });
684
-
685
- it('supports useMutationState with without filter', async () => {
686
- const queryClient = new QueryClient();
687
-
688
- const { result: mutationResult } = renderHook(
689
- () => qraft.entities.postEntitiesIdDocuments.useMutation(parameters),
690
- {
691
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
692
- }
693
- );
694
-
695
- act(() => {
696
- mutationResult.current.mutate({
697
- verification_document_back: 'back',
698
- verification_document_front: 'front',
699
- });
700
- });
701
-
702
- await waitFor(() => {
703
- expect(mutationResult.current.data).toBeDefined();
704
- });
705
-
706
- const { result: mutationStateResult } = renderHook(
707
- () => qraft.entities.postEntitiesIdDocuments.useMutationState(),
708
- {
709
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
710
- }
711
- );
712
-
713
- expect(mutationStateResult.current.at(0)?.data).toEqual({
714
- body: {
715
- verification_document_back: 'back',
716
- verification_document_front: 'front',
717
- },
718
- ...parameters,
719
- });
720
- });
721
-
722
- it('supports useMutationState with mutationKey and select', async () => {
723
- const queryClient = new QueryClient();
724
-
725
- const { result: mutationHookResultToMatch } = renderHook(
726
- () => qraft.entities.postEntitiesIdDocuments.useMutation(parameters),
727
- {
728
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
729
- }
730
- );
731
-
732
- act(() => {
733
- mutationHookResultToMatch.current.mutate({
734
- verification_document_back: 'back',
735
- verification_document_front: 'front',
736
- });
737
- });
738
-
739
- await waitFor(() => {
740
- expect(mutationHookResultToMatch.current.data).toBeDefined();
741
- });
742
-
743
- const { result: mutationStateHookResult } = renderHook(
744
- () =>
745
- qraft.entities.postEntitiesIdDocuments.useMutationState({
746
- filters: {
747
- mutationKey:
748
- qraft.entities.postEntitiesIdDocuments.getMutationKey(parameters),
749
- },
750
- select(mutation) {
751
- return (
752
- mutation.state.data?.header?.['x-monite-version'] ===
753
- parameters.header['x-monite-version']
754
- );
755
- },
756
- }),
757
- {
758
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
759
- }
760
- );
761
-
762
- expect(mutationStateHookResult.current.length).toEqual(1);
763
- expect(mutationStateHookResult.current.at(0)).toEqual(true);
764
- });
765
-
766
- it('supports useMutationState with not partial parameters', async () => {
767
- const queryClient = new QueryClient();
768
-
769
- const { result: mutationHookResultToMatch } = renderHook(
770
- () => qraft.entities.postEntitiesIdDocuments.useMutation(parameters),
771
- {
772
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
773
- }
774
- );
775
-
776
- act(() => {
777
- mutationHookResultToMatch.current.mutate({
778
- verification_document_back: 'back',
779
- verification_document_front: 'front',
780
- });
781
- });
782
-
783
- await waitFor(() => {
784
- expect(mutationHookResultToMatch.current.data).toBeDefined();
785
- });
786
-
787
- const { result: mutationStateResult } = renderHook(
788
- () =>
789
- qraft.entities.postEntitiesIdDocuments.useMutationState({
790
- filters: {
791
- parameters: {
792
- header: parameters.header,
793
- },
794
- },
795
- select(mutation) {
796
- return (
797
- mutation.state.data?.header?.['x-monite-version'] ===
798
- parameters.header['x-monite-version']
799
- );
800
- },
801
- }),
802
- {
803
- wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
804
- }
805
- );
806
-
807
- expect(mutationStateResult.current.length).toEqual(1);
808
- expect(mutationStateResult.current.at(0)).toEqual(true);
809
- });
810
- });
811
-
812
- describe('Qraft uses Query Function', () => {
813
- it('uses queryFn', async () => {
814
- const result = await qraft.approvalPolicies.getApprovalPoliciesId.queryFn(
815
- {
816
- parameters: {
817
- header: {
818
- 'x-monite-version': '1.0.0',
819
- },
820
- path: {
821
- approval_policy_id: '1',
822
- },
823
- query: {
824
- items_order: ['asc', 'desc'],
825
- },
826
- },
827
- },
828
- requestClient
829
- );
830
-
831
- expect(result).toEqual({
832
- header: {
833
- 'x-monite-version': '1.0.0',
834
- },
835
- path: {
836
- approval_policy_id: '1',
837
- },
838
- query: {
839
- items_order: ['asc', 'desc'],
840
- },
841
- });
842
- });
843
- });
844
-
845
- describe('Qraft uses utils', () => {
846
- it('returns _def', () => {
847
- // @ts-ignore
848
- expect(qraft.counterparts.postCounterpartsIdAddresses._def()).toEqual({
849
- path: ['counterparts', 'postCounterpartsIdAddresses'],
850
- });
851
- });
852
-
853
- it('throws an error when calling an unsupported method ', () => {
854
- expect(() =>
855
- // @ts-ignore
856
- qraft.counterparts.postCounterpartsIdAddresses.unsupportedMethod()
857
- ).toThrowError(/Function unsupportedMethod is not supported/i);
858
- });
859
- });
860
-
861
- describe('Qraft uses setQueryData', () => {
862
- it('uses setQueryData & getQueryData', async () => {
863
- const queryClient = new QueryClient();
864
-
865
- qraft.files.getFiles.setQueryData(
866
- {
867
- header: {
868
- 'x-monite-version': '1.0.0',
869
- },
870
- query: {
871
- id__in: ['1', '2'],
872
- },
873
- },
874
- {
875
- header: {
876
- 'x-monite-version': '1.0.0',
877
- },
878
- query: {
879
- id__in: ['1', '2'],
880
- },
881
- },
882
- queryClient
883
- );
884
-
885
- expect(
886
- qraft.files.getFiles.getQueryData(
887
- {
888
- header: {
889
- 'x-monite-version': '1.0.0',
890
- },
891
- query: {
892
- id__in: ['1', '2'],
893
- },
894
- },
895
- queryClient
896
- )
897
- ).toEqual({
898
- header: {
899
- 'x-monite-version': '1.0.0',
900
- },
901
- query: {
902
- id__in: ['1', '2'],
903
- },
904
- });
905
- });
906
- });
907
-
908
- describe('Qraft uses setInfiniteQueryData', () => {
909
- it('uses setInfiniteQueryData & getInfiniteQueryData', async () => {
910
- const queryClient = new QueryClient();
911
-
912
- qraft.files.getFiles.setInfiniteQueryData(
913
- {
914
- header: {
915
- 'x-monite-version': '1.0.0',
916
- },
917
- query: {
918
- id__in: ['1', '2'],
919
- },
920
- },
921
- {
922
- pages: [
923
- {
924
- header: {
925
- 'x-monite-version': '1.0.0',
926
- },
927
- query: {
928
- id__in: ['1', '2'],
929
- },
930
- },
931
- ],
932
- pageParams: [
933
- {
934
- query: {
935
- page: '1',
936
- },
937
- },
938
- ],
939
- },
940
- queryClient
941
- );
942
-
943
- expect(
944
- qraft.files.getFiles.getInfiniteQueryData(
945
- {
946
- header: {
947
- 'x-monite-version': '1.0.0',
948
- },
949
- query: {
950
- id__in: ['1', '2'],
951
- },
952
- },
953
- queryClient
954
- )
955
- ).toEqual({
956
- pages: [
957
- {
958
- header: {
959
- 'x-monite-version': '1.0.0',
960
- },
961
- query: {
962
- id__in: ['1', '2'],
963
- },
964
- },
965
- ],
966
- pageParams: [
967
- {
968
- query: {
969
- page: '1',
970
- },
971
- },
972
- ],
973
- });
974
- });
975
- });
976
-
977
- function Providers({
978
- children,
979
- queryClient,
980
- }: {
981
- children: ReactNode;
982
- queryClient?: QueryClient;
983
- }) {
984
- queryClient = React.useState(() => queryClient ?? new QueryClient())[0];
985
-
986
- return (
987
- <QueryClientProvider client={queryClient}>
988
- {/* We should use precompiled `QraftContextDist`,
989
- * because callbacks are imported from `@openapi-qraft` package `/dist` folder
990
- */}
991
- <QraftContextDist.Provider value={{ requestClient }}>
992
- {children}
993
- </QraftContextDist.Provider>
994
- </QueryClientProvider>
995
- );
996
- }