-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.ts
168 lines (139 loc) · 4.18 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
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
import {TestScheduler} from "rxjs/testing"
import {delay, mapTo, toArray} from "rxjs/operators"
import {exhaustMapToWithTrailing, exhaustMapWithTrailing} from "./index"
import {concat, EMPTY, firstValueFrom, of, timer} from "rxjs"
import {RunHelpers} from "rxjs/internal/testing/TestScheduler"
describe("exhaustMapWithTrailing()", () => {
it("is empty when source is empty", () => {
runTest(({cold, expectObservable}) => {
const [input, expected] = marble`
|
|
`
const result = cold(input).pipe(
exhaustMapWithTrailing((d) => of(d).pipe(delay(4)))
)
expectObservable(result).toBe(expected)
})
})
it("emits both leading and trailing ends", () => {
runTest(({cold, expectObservable}) => {
const [input, expected] = marble`
ab-----|
----a---(b|)
`
const result = cold(input).pipe(
exhaustMapWithTrailing((d) => of(d).pipe(delay(4)))
)
expectObservable(result).toBe(expected)
})
})
it("emits trailing values after source has completed", () => {
runTest(({cold, expectObservable}) => {
const [input, expected] = marble`
ab|
----a---(b|)
`
const result = cold(input).pipe(
exhaustMapWithTrailing((d) => of(d).pipe(delay(4)))
)
expectObservable(result).toBe(expected)
})
})
it("completes immediately if source ended while waiting for inner observable to complete", () => {
runTest(({cold, expectObservable}) => {
const [input, expected] = marble`
a-|
---- (a|)
`
const result = cold(input).pipe(
exhaustMapWithTrailing((d) => of(d).pipe(delay(4)))
)
expectObservable(result).toBe(expected)
})
})
it("emits the trailing value if emitted in same frame as the current", () => {
runTest(({cold, expectObservable}) => {
const [input, expected] = marble`
a-b-c-|
----a---(c|)
`
const result = cold(input).pipe(
exhaustMapWithTrailing((d) => of(d).pipe(delay(4)))
)
expectObservable(result).toBe(expected)
})
})
it("emits the trailing value if emitted after new value received", () => {
runTest(({cold, expectObservable}) => {
const [input, expected] = marble`
a-b--c----|
----a---b---(c|)
`
const result = cold(input).pipe(
exhaustMapWithTrailing((d) => of(d).pipe(delay(4)))
)
expectObservable(result).toBe(expected)
})
})
it("waits for the source observable to complete", () => {
runTest(({cold, expectObservable}) => {
const [input, expected] = marble`
a-b-c-----|
----a---c-|
`
const result = cold(input).pipe(
exhaustMapWithTrailing((d) => of(d).pipe(delay(4)))
)
expectObservable(result).toBe(expected)
})
})
it("forwards errors from source observable", () => {
runTest(({cold, expectObservable}) => {
const [input, expected] = marble`
a-#
--#
`
const result = cold(input).pipe(
exhaustMapWithTrailing((d) => of(d).pipe(delay(4)))
)
expectObservable(result).toBe(expected)
})
})
})
describe("exhaustMapToWithTrailing()", () => {
it("takes an observable as argument", () => {
runTest(({cold, expectObservable}) => {
const [input, expected] = marble`
a-b-c-----|
----x---x-|
`
const result = cold(input).pipe(
exhaustMapToWithTrailing(of("x").pipe(delay(4)))
)
expectObservable(result).toBe(expected)
})
})
})
it("sync projected values", async () => {
const values = concat(
timer(5).pipe(mapTo("async")),
timer(10).pipe(mapTo("sync")),
timer(10).pipe(mapTo("empty")),
timer(15).pipe(mapTo("async"))
).pipe(
exhaustMapWithTrailing((x) =>
x === "empty" ? EMPTY : x === "sync" ? of(x) : timer(1).pipe(mapTo(x))
),
toArray()
)
expect(await firstValueFrom(values)).toEqual(["async", "sync", "async"])
})
function runTest(callback: (helpers: RunHelpers) => void) {
return new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected)
}).run(callback)
}
function marble(strings: TemplateStringsArray) {
return strings.join("").split("\n").filter(Boolean)
}