|
1 |
| -import { concatAll, concatMap, delay, from, map, of, toArray } from 'rxjs'; |
| 1 | +import { concatAll, delay, from, map, of } from 'rxjs'; |
2 | 2 | import { TestScheduler } from 'rxjs/testing';
|
3 |
| -import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; |
| 3 | +import { afterAll, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest'; |
4 | 4 |
|
5 |
| -import { log } from '../log'; |
| 5 | +import { mockAsync } from '../../../mock/async'; |
| 6 | +import { mockResponse } from '../../../mock/response'; |
| 7 | +import { log, logResult } from '../log'; |
6 | 8 | import { resolveJSON } from './response';
|
7 | 9 |
|
8 |
| -describe('auto pagination - mocked', function () { |
9 |
| - const testScheduler = new TestScheduler((actual, expected) => { |
10 |
| - expect(actual).to.eql(expected); |
11 |
| - }); |
| 10 | +describe('auto pagination', () => { |
| 11 | + let testScheduler; |
12 | 12 |
|
13 |
| - beforeEach(function () { |
14 |
| - vi.doMock('./request', importOriginal => ({ |
15 |
| - request: () => source => source.pipe(concatMap(({ v, t }) => of(v).pipe(delay(t)))) |
16 |
| - })); |
| 13 | + beforeAll(() => { |
| 14 | + vi.spyOn(global, 'fetch').mockImplementation(({ v, t }) => mockAsync(v).pipe(delay(t))); |
17 | 15 |
|
18 |
| - Object.prototype.clone = vi.fn(); |
19 |
| - vi.spyOn(Object.prototype, 'clone').mockImplementation(function (e) { |
20 |
| - return { ...JSON.parse(JSON.stringify(this)) }; |
21 |
| - }); |
| 16 | + global.Response = mockResponse(); |
22 | 17 | });
|
23 | 18 |
|
24 |
| - afterEach(() => { |
25 |
| - vi.doUnmock('./request'); |
| 19 | + beforeEach(() => { |
| 20 | + testScheduler = new TestScheduler((actual, expected) => expect(actual).to.eql(expected)); |
26 | 21 | });
|
27 | 22 |
|
28 |
| - test('classic testing', async () => { |
29 |
| - const { autoPagination } = await import('./autoPagination'); |
30 |
| - |
31 |
| - const triggerVal = [ |
32 |
| - { t: 2, v: { value: 'a', next: 1 } }, |
33 |
| - { t: 5, v: { value: 'b', next: 2 } }, |
34 |
| - { t: 3, v: { value: 'c', next: 3 } }, |
35 |
| - { t: 1, v: { value: 'd', next: 4 } }, |
36 |
| - { t: 4, v: { value: 'e', next: null } } |
37 |
| - ]; |
38 |
| - |
39 |
| - const expectedVal = triggerVal.map(({ v }) => v); |
40 |
| - |
41 |
| - await new Promise((done, error) => { |
42 |
| - of(triggerVal[0]) |
43 |
| - .pipe( |
44 |
| - autoPagination({ |
45 |
| - resolveRoute: (conf, resp) => |
46 |
| - ((!resp || resp.next) && [triggerVal[resp?.next || 0]]) || [] |
47 |
| - }), |
48 |
| - toArray() |
49 |
| - ) |
50 |
| - .subscribe({ |
51 |
| - next: e => expect(e).toStrictEqual(expectedVal), |
52 |
| - complete: () => done(), |
53 |
| - error: () => error() |
54 |
| - }); |
55 |
| - }); |
| 23 | + afterAll(() => { |
| 24 | + vi.restoreAllMocks(); |
56 | 25 | });
|
57 | 26 |
|
58 |
| - test('marble testing', async () => { |
| 27 | + test('default', async () => { |
59 | 28 | const { autoPagination } = await import('./autoPagination');
|
60 | 29 |
|
61 |
| - const triggerVal = { |
62 |
| - a: { t: 2, v: { value: 'a', next: 'b' } }, |
63 |
| - b: { t: 5, v: { value: 'b', next: 'c' } }, |
64 |
| - c: { t: 3, v: { value: 'c', next: 'd' } }, |
65 |
| - d: { t: 1, v: { value: 'd', next: 'e' } }, |
66 |
| - e: { t: 4, v: { value: 'e', next: null } } |
| 30 | + const expectedVal = { |
| 31 | + a: { value: 'a', next: 'b' }, |
| 32 | + b: { value: 'b', next: 'c' }, |
| 33 | + c: { value: 'c', next: 'd' }, |
| 34 | + d: { value: 'd', next: 'e' }, |
| 35 | + e: { value: 'e', next: null } |
67 | 36 | };
|
68 | 37 |
|
69 |
| - const expectedVal = Object.fromEntries( |
70 |
| - Array.from(Object.entries(triggerVal)).map(([k, { v }]) => [k, v]) |
71 |
| - ); |
| 38 | + const triggerVal = { |
| 39 | + a: { t: 2, v: new Response(expectedVal.a) }, |
| 40 | + b: { t: 5, v: new Response(expectedVal.b) }, |
| 41 | + c: { t: 3, v: new Response(expectedVal.c) }, |
| 42 | + d: { t: 1, v: new Response(expectedVal.d) }, |
| 43 | + e: { t: 4, v: new Response(expectedVal.e) } |
| 44 | + }; |
72 | 45 |
|
73 | 46 | testScheduler.run(({ cold, expectObservable }) => {
|
74 | 47 | expectObservable(
|
75 |
| - cold('-a-------------------', triggerVal).pipe( |
| 48 | + cold('-a-------------------', { a: 'a' }).pipe( |
76 | 49 | autoPagination({
|
77 |
| - resolveRoute: (conf, resp) => |
78 |
| - ((!resp || resp.next) && [triggerVal[resp?.next || 'a']]) || [] |
79 |
| - }) |
| 50 | + resolveRoute: (url, resp) => { |
| 51 | + if (resp) { |
| 52 | + return from(resp.json()).pipe(map(({ next }) => triggerVal[String(next)])); |
| 53 | + } |
| 54 | + return of(triggerVal[String(url)]); |
| 55 | + } |
| 56 | + }), |
| 57 | + resolveJSON(), |
| 58 | + log('marble:result') |
80 | 59 | )
|
81 | 60 | ).toBe('---a----b--cd---e----', expectedVal);
|
82 | 61 | });
|
83 | 62 | });
|
84 | 63 | });
|
85 | 64 |
|
86 |
| -describe.skip('auto pagination - demo', function () { |
87 |
| - beforeEach(function () { |
88 |
| - vi.resetModules(); |
89 |
| - }); |
90 |
| - |
91 |
| - test('sample testing', async function () { |
| 65 | +describe('auto pagination - demo', () => { |
| 66 | + test('sample', async () => { |
92 | 67 | const { autoPagination } = await import('./autoPagination');
|
93 | 68 |
|
94 |
| - return new Promise(done => { |
95 |
| - return of(new URL('https://dummyjson.com/products')) |
96 |
| - .pipe( |
97 |
| - autoPagination({ |
98 |
| - resolveRoute: async (url, resp) => { |
99 |
| - const data = (await resp?.json()) || { skip: -10, limit: 10 }; |
100 |
| - |
101 |
| - if (!data.total || data.total > data.skip + data.limit) { |
102 |
| - const newUrl = new URL(`${url}`); |
103 |
| - newUrl.searchParams.set('skip', data.skip + data.limit); |
104 |
| - newUrl.searchParams.set('limit', data.limit); |
105 |
| - newUrl.searchParams.set('select', 'title,price'); |
106 |
| - return newUrl; |
107 |
| - } |
108 |
| - } |
109 |
| - }), |
110 |
| - log(false), |
111 |
| - resolveJSON(), |
112 |
| - log(false), |
113 |
| - map(({ products }) => products), |
114 |
| - concatAll() |
115 |
| - ) |
116 |
| - .subscribe({ |
117 |
| - next: e => console.log(e), |
118 |
| - complete: () => done() |
119 |
| - }); |
120 |
| - }); |
| 69 | + await logResult( |
| 70 | + 'demo', |
| 71 | + of(new URL('https://dummyjson.com/products')).pipe( |
| 72 | + autoPagination({ |
| 73 | + resolveRoute: (url, resp) => { |
| 74 | + return from(resp?.json() || of({ skip: -10, limit: 10 })).pipe( |
| 75 | + map(data => { |
| 76 | + if (!data.total || data.total > data.skip + data.limit) { |
| 77 | + const newUrl = new URL(`${url}`); |
| 78 | + newUrl.searchParams.set('skip', data.skip + data.limit); |
| 79 | + newUrl.searchParams.set('limit', data.limit); |
| 80 | + newUrl.searchParams.set('select', 'title,price'); |
| 81 | + return newUrl; |
| 82 | + } |
| 83 | + }) |
| 84 | + ); |
| 85 | + } |
| 86 | + }), |
| 87 | + log('demo:response'), |
| 88 | + resolveJSON(), |
| 89 | + log('demo:response:json'), |
| 90 | + map(({ products }) => products), |
| 91 | + log('demo:response:result'), |
| 92 | + concatAll() |
| 93 | + ) |
| 94 | + ); |
121 | 95 | });
|
122 | 96 | });
|
0 commit comments