-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathng-redux.mock.spec.ts
116 lines (88 loc) · 3.01 KB
/
ng-redux.mock.spec.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
import { TestBed } from '@angular/core/testing';
import { Component } from '@angular/core';
import { Observable } from 'rxjs';
import { map, toArray } from 'rxjs/operators';
import { MockNgRedux } from './ng-redux.mock';
import { NgRedux, select, select$ } from '../src';
@Component({
template: 'whatever',
selector: 'test-component',
})
class TestComponent {
@select('foo') readonly obs$: Observable<number>;
@select$('bar', obs$ => obs$.pipe(map((x: any) => 2 * x)))
readonly barTimesTwo$: Observable<number>;
readonly baz$: Observable<number>;
constructor(ngRedux: NgRedux<any>) {
this.baz$ = ngRedux.select('baz');
}
}
describe('NgReduxMock', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
providers: [{ provide: NgRedux, useFactory: MockNgRedux.getInstance }],
}).compileComponents();
MockNgRedux.reset();
});
it('should reset stubs used by @select', () => {
const instance = TestBed.createComponent(TestComponent).debugElement
.componentInstance;
const stub1 = MockNgRedux.getSelectorStub('foo');
stub1.next(1);
stub1.next(2);
stub1.complete();
instance.obs$
.pipe(toArray())
.subscribe((values: number[]) => expect(values).toEqual([1, 2]));
MockNgRedux.reset();
// Reset should result in a new stub getting created.
const stub2 = MockNgRedux.getSelectorStub('foo');
expect(stub1 === stub2).toBe(false);
stub2.next(3);
stub2.complete();
instance.obs$
.pipe(toArray())
.subscribe((values: number[]) => expect(values).toEqual([3]));
});
it('should reset stubs used by @select$', () => {
const instance = TestBed.createComponent(TestComponent).debugElement
.componentInstance;
const stub1 = MockNgRedux.getSelectorStub('bar');
stub1.next(1);
stub1.next(2);
stub1.complete();
instance.barTimesTwo$
.pipe(toArray())
.subscribe((values: number[]) => expect(values).toEqual([2, 4]));
MockNgRedux.reset();
// Reset should result in a new stub getting created.
const stub2 = MockNgRedux.getSelectorStub('bar');
expect(stub1 === stub2).toBe(false);
stub2.next(3);
stub2.complete();
instance.obs$
.pipe(toArray())
.subscribe((values: number[]) => expect(values).toEqual([6]));
});
it('should reset stubs used by .select', () => {
const instance = TestBed.createComponent(TestComponent).debugElement
.componentInstance;
const stub1 = MockNgRedux.getSelectorStub('baz');
stub1.next(1);
stub1.next(2);
stub1.complete();
instance.baz$
.pipe(toArray())
.subscribe((values: number[]) => expect(values).toEqual([1, 2]));
MockNgRedux.reset();
// Reset should result in a new stub getting created.
const stub2 = MockNgRedux.getSelectorStub('baz');
expect(stub1 === stub2).toBe(false);
stub2.next(3);
stub2.complete();
instance.obs$
.pipe(toArray())
.subscribe((values: number[]) => expect(values).toEqual([3]));
});
});