-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
108 lines (88 loc) · 2.65 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
import { Observable } from 'rxjs'
import { delay, scan, debounceTime, map } from 'rxjs/operators'
import { marble } from '../jest';
export const mapToNumber = (string$: Observable<string | number>) => string$.pipe(map(s => Number(s)))
export const valueToValue = (value$: Observable<any>) => value$.pipe(map(v => v))
export const debounceValue = (value$: Observable<any>) => value$.pipe(debounceTime(10))
export const retryOnError = (delayTime: number) => {
return (error$: Observable<Error | number>) =>
error$.pipe(
scan((errorCount, err) => {
if (errorCount >= 2) {
throw err
}
return errorCount + 1
}, 0),
delay(delayTime)
)
}
marble('debounce values', m => {
const input = 'a 10ms b 8ms c 30ms |'
const output = '10ms a 19ms c 20ms |'
// const output = '|'
const values$ = m.cold(input)
// Act
const obs = debounceValue(values$)
// const obs = values$.pipe(x => x)
// Assert
m.flush();
m.expectObservable(obs).toBe(output)
})
marble('map to the same value', m => {
// Arrange
const values = { a: { a: 1 }, b: { b: 2 }, 1: { a: 1 }, 2: { b: 2 } }
const input = 'a--b|'
const output = '1--2|'
const values$ = m.cold(input, values)
// Act
const obs = valueToValue(values$)
// Assert
m.expectObservable(obs).toBe(output, values)
})
marble('map to number', m => {
// Arrange
const values = { a: '1', b: '2', 1: 1, 2: 2 }
const input = 'a--b|'
const output = '1--2|'
const strings$ = m.cold(input, values)
// Act
const obs = mapToNumber(strings$)
// Assert
m.expectObservable(obs).toBe(output, values)
})
marble('count errors', m => {
// Arrange
const values = { a: new Error(), b: new Error(), 1: 1, 2: 2 }
const input = 'a--b|'
const output = '-1--2|' // delay with 1 ms
const error$ = m.cold(input, values)
// Act
const obs = retryOnError(1)(error$ as any)
// Assert
m.expectObservable(obs).toBe(output, values)
})
marble('rethrow after 2 tries', m => {
// Arrange
const error = new Error()
const values = { a: error, 1: 1, 2: 2 }
const input = 'a--a--a'
const output = '1--2--#'
const error$ = m.cold(input, values)
// Act
const obs = retryOnError(0)(error$ as any)
// Assert
m.expectObservable(obs).toBe(output, values, error)
})
marble('unsubscribe after error', m => {
// Arrange
const error = new Error()
const values = { a: error, 1: 1, 2: 2 }
const input = 'a--a--a'
const output = '1--2--#'
const error$ = m.cold(input, values)
// Act
const obs = retryOnError(0)(error$ as any)
// Assert
m.expectObservable(obs).toBe(output, values, error)
m.expectSubscriptions(error$.subscriptions).toBe(['^-----!'])
})