forked from ngrx/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheffect_sources.spec.ts
More file actions
121 lines (93 loc) · 3.24 KB
/
effect_sources.spec.ts
File metadata and controls
121 lines (93 loc) · 3.24 KB
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
import 'rxjs/add/operator/concat';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import { cold, getTestScheduler } from 'jasmine-marbles';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { timer } from 'rxjs/observable/timer';
import { _throw } from 'rxjs/observable/throw';
import { never } from 'rxjs/observable/never';
import { empty } from 'rxjs/observable/empty';
import { TestBed } from '@angular/core/testing';
import { ErrorHandler } from '@angular/core';
import { Effect, EffectSources } from '../';
describe('EffectSources', () => {
let mockErrorReporter: ErrorHandler;
let effectSources: EffectSources;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [EffectSources],
});
mockErrorReporter = TestBed.get(ErrorHandler);
effectSources = TestBed.get(EffectSources);
spyOn(mockErrorReporter, 'handleError');
});
it('should have an "addEffects" method to push new source instances', () => {
const effectSource = {};
spyOn(effectSources, 'next');
effectSources.addEffects(effectSource);
expect(effectSources.next).toHaveBeenCalledWith(effectSource);
});
describe('toActions() Operator', () => {
const a = { type: 'From Source A' };
const b = { type: 'From Source B' };
const c = { type: 'From Source C that completes' };
const d = { not: 'a valid action' };
const error = new Error('An Error');
class SourceA {
@Effect() a$ = alwaysOf(a);
}
class SourceB {
@Effect() b$ = alwaysOf(b);
}
class SourceC {
@Effect() c$ = of(c);
}
class SourceD {
@Effect() d$ = alwaysOf(d);
}
class SourceE {
@Effect() e$ = _throw(error);
}
class SourceG {
@Effect() empty = of('value');
@Effect() never = timer(50, getTestScheduler()).map(() => 'update');
}
it('should resolve effects from instances', () => {
const sources$ = cold('--a--', { a: new SourceA() });
const expected = cold('--a--', { a });
const output = toActions(sources$);
expect(output).toBeObservable(expected);
});
it('should ignore duplicate sources', () => {
const sources$ = cold('--a--b--c--', {
a: new SourceA(),
b: new SourceA(),
c: new SourceA(),
});
const expected = cold('--a--------', { a });
const output = toActions(sources$);
expect(output).toBeObservable(expected);
});
it('should report an error if an effect dispatches an invalid action', () => {
const sources$ = of(new SourceD());
toActions(sources$).subscribe();
expect(mockErrorReporter.handleError).toHaveBeenCalled();
});
it('should not complete the group if just one effect completes', () => {
const sources$ = cold('g', {
g: new SourceG(),
});
const expected = cold('a----b-----', { a: 'value', b: 'update' });
const output = toActions(sources$);
expect(output).toBeObservable(expected);
});
function toActions(source: any): Observable<any> {
source['errorHandler'] = mockErrorReporter;
return effectSources.toActions.call(source);
}
});
function alwaysOf<T>(value: T) {
return of(value).concat(never<T>());
}
});