Skip to content

Commit a685ce2

Browse files
feat(json): add demo test for requestJSON with user posts and todos resolution
1 parent 5dcaad5 commit a685ce2

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

packages/operators/src/json.test.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { mockPromise } from '#mocks/Promise.js';
22
import { Buffer } from 'node:buffer';
33
import { readFile } from 'node:fs/promises';
4-
import { lastValueFrom, map, of } from 'rxjs';
4+
import { concatMap, firstValueFrom, lastValueFrom, map, of, share, tap } from 'rxjs';
55
import { TestScheduler } from 'rxjs/testing';
66
import { afterAll, beforeAll, beforeEach, afterEach, describe, expect, test, vi } from 'vitest';
77

8+
import { requestJSON } from './request';
9+
810
describe('json', () => {
911
let testScheduler;
1012

@@ -484,5 +486,37 @@ describe('json', () => {
484486

485487
console.log((await data).globalSymbol === deserialized.globalSymbol);
486488
});
489+
490+
test('demo', async () => {
491+
const { serialize, deserialize } = await import('./json');
492+
493+
const baseURL = 'https://jsonplaceholder.typicode.com/';
494+
const eMail = '[email protected]';
495+
const user = of(new URL(`users?email=${eMail}`, baseURL)).pipe(
496+
requestJSON(),
497+
tap(() => console.log('REQUEST')),
498+
map(([item]) => item),
499+
share()
500+
);
501+
502+
const resolvePosts = () => source =>
503+
source.pipe(
504+
concatMap(user => of(new URL(`users/${user.id}/posts`, baseURL)).pipe(requestJSON()))
505+
);
506+
507+
const resolveTodos = () => source =>
508+
source.pipe(
509+
concatMap(user => of(new URL(`users/${user.id}/todos`, baseURL)).pipe(requestJSON()))
510+
);
511+
512+
const p = of({
513+
user,
514+
posts: user.pipe(resolvePosts()),
515+
todos: user.pipe(resolveTodos())
516+
}).pipe(serialize());
517+
518+
const res = await firstValueFrom(p);
519+
console.log(JSON.parse(res));
520+
});
487521
/* v8 ignore stop */
488522
});

0 commit comments

Comments
 (0)