forked from angular-redux/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.spec.ts
117 lines (99 loc) · 3.36 KB
/
page.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
117
import {
MockNgRedux,
NgReduxTestingModule,
} from '@angular-redux/store/testing';
import { TestBed } from '@angular/core/testing';
import { Component, Input } from '@angular/core';
import { Observable } from 'rxjs';
import { toArray } from 'rxjs/operators';
import { AnimalAPIActions } from '../animals/api/actions';
import { Animal, ANIMAL_TYPES } from '../animals/model';
import { ElephantPageComponent } from './page';
@Component({
selector: 'zoo-animal-list',
template: 'Mock Animal List',
})
class MockAnimalListComponent {
@Input() animalsName!: string;
@Input() animals!: Observable<Animal[]>;
@Input() loading!: Observable<boolean>;
@Input() error!: Observable<boolean>;
}
describe('Elephant Page Container', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ElephantPageComponent, MockAnimalListComponent],
imports: [NgReduxTestingModule],
providers: [AnimalAPIActions],
}).compileComponents();
MockNgRedux.reset();
});
it('should select some elephants from the Redux store', done => {
const fixture = TestBed.createComponent(ElephantPageComponent);
const elephantPage = fixture.componentInstance;
const mockStoreSequence = [
{ elephant1: { name: 'I am an Elephant!', id: 'elephant1' } },
{
elephant1: { name: 'I am an Elephant!', id: 'elephant1' },
elephant2: { name: 'I am a second Elephant!', id: 'elephant2' },
},
];
const expectedSequence = [
[{ name: 'I am an Elephant!', id: 'elephant1' }],
[
// Alphanumeric sort by name.
{ name: 'I am a second Elephant!', id: 'elephant2' },
{ name: 'I am an Elephant!', id: 'elephant1' },
],
];
const elephantItemStub = MockNgRedux.getSelectorStub(['elephant', 'items']);
mockStoreSequence.forEach(value => elephantItemStub.next(value));
elephantItemStub.complete();
elephantPage.animals
.pipe(toArray())
.subscribe(
actualSequence =>
expect(actualSequence).toEqual(expectedSequence as any),
undefined,
done,
);
});
it('should know when the animals are loading', done => {
const fixture = TestBed.createComponent(ElephantPageComponent);
const elephantPage = fixture.componentInstance;
const stub = MockNgRedux.getSelectorStub(['elephant', 'loading']);
stub.next(false);
stub.next(true);
stub.complete();
elephantPage.loading
.pipe(toArray())
.subscribe(
actualSequence => expect(actualSequence).toEqual([false, true]),
undefined,
done,
);
});
it("should know when there's an error", done => {
const fixture = TestBed.createComponent(ElephantPageComponent);
const elephantPage = fixture.componentInstance;
const stub = MockNgRedux.getSelectorStub(['elephant', 'error']);
stub.next(false);
stub.next(true);
stub.complete();
elephantPage.error
.pipe(toArray())
.subscribe(
actualSequence => expect(actualSequence).toEqual([false, true]),
undefined,
done,
);
});
it('should load elephants on creation', () => {
const spy = spyOn(MockNgRedux.getInstance(), 'dispatch');
TestBed.createComponent(ElephantPageComponent);
expect(spy).toHaveBeenCalledWith({
type: AnimalAPIActions.LOAD_ANIMALS,
meta: { animalType: ANIMAL_TYPES.ELEPHANT },
});
});
});