-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSagaTester.ts
90 lines (75 loc) · 2.27 KB
/
SagaTester.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
/* istanbul ignore file */
import { cloneableGenerator, SagaIteratorClone } from "@redux-saga/testing-utils";
interface Config {
nextValue?: any,
nextError?: Error,
}
class SagaTester {
public static create = (generator: any, ...args: any[]): SagaTester => {
return new SagaTester(cloneableGenerator(generator)(...args))
}
private generator: SagaIteratorClone
private nextValue: any
private nextError: any
private constructor(generator: SagaIteratorClone, config?: Config) {
this.generator = generator
if (config) {
this.nextValue = config.nextValue
this.nextError = config.nextError
}
}
public pass = (value: any) => {
this.nextValue = value
return this
}
public throw = (error: Error) => {
this.nextError = error
return this
}
public expectNext = (desc: string, value: any) => {
const next = this.passValueAndNext()
it(desc, () => {
expect(next.value).toEqual(value)
})
return this
}
public expectDone = () => {
const next = this.passValueAndNext()
it('is done', () => {
expect(next.value).toBeUndefined()
expect(next.done).toEqual(true)
})
return this
}
public returns = (value: any) => {
const next = this.passValueAndNext()
it('is done', () => {
expect(next.done).toEqual(true)
})
it('returns', () => {
expect(next.value).toEqual(value)
})
}
public clone = () => {
return new SagaTester(this.generator.clone(), {
nextError: this.nextError,
nextValue: this.nextValue
})
}
private passValueAndNext = () => {
if (this.nextError) {
const nextError = this.nextError
this.nextError = undefined
const throwFunc = this.generator.throw
if (throwFunc) {
return throwFunc(nextError)
}
} else if (this.nextValue !== undefined) {
const nextValue = this.nextValue
this.nextValue = undefined
return this.generator.next(nextValue)
}
return this.generator.next()
}
}
export default SagaTester