forked from darky/ts-fp-di-rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
61 lines (49 loc) · 1.38 KB
/
test.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
import test from 'node:test'
import assert from 'node:assert'
import { dirx } from './index'
import { from, lastValueFrom, map, merge, mergeMap } from 'rxjs'
import { diInit } from 'ts-fp-di'
test('basic', async () => {
await diInit(async () => {
const incNumbers$ = dirx(
arr => from(arr).pipe(map(n => n + 1)),
() => [1, 2, 3]
)
const resp = await lastValueFrom(incNumbers$())
assert.strictEqual(resp, 4)
})
})
test('multicast', async () => {
await diInit(async () => {
let call = 0
const inc = async (n: number) => {
call += 1
return n + 1
}
const incNumbers$ = dirx(
arr => from(arr).pipe(mergeMap(inc)),
() => [1, 2, 3]
)
const numbersToStrings$ = dirx(numbers$ => numbers$.pipe(map(n => n.toString())), incNumbers$)
const decNumbers$ = dirx(numbers$ => numbers$.pipe(map(n => n - 1)), incNumbers$)
await lastValueFrom(merge(numbersToStrings$(), decNumbers$()))
assert.strictEqual(call, 3)
})
})
test('type-level, should return Observable', async () => {
dirx(
// @ts-expect-error
arr => arr,
() => [1, 2, 3]
)
})
test('raw', async () => {
await diInit(async () => {
const incNumbers$ = dirx(
arr => from(arr).pipe(map(n => n + 1)),
() => [1, 2, 3]
)
const resp = await lastValueFrom(incNumbers$.raw([-2, -1, 0]))
assert.strictEqual(resp, 1)
})
})