forked from angular-redux/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.spec.ts
118 lines (100 loc) · 3.31 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
118
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 { LionPageComponent } 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('Lion Page Container', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [LionPageComponent, MockAnimalListComponent],
imports: [NgReduxTestingModule],
providers: [AnimalAPIActions],
}).compileComponents();
MockNgRedux.reset();
});
// TO DO: debug later
xit('should select some lions from the Redux store', done => {
const fixture = TestBed.createComponent(LionPageComponent);
const lionPage = fixture.componentInstance;
const mockStoreSequence = [
{ lion1: { name: 'I am a Lion!', id: 'lion1' } },
{
lion1: { name: 'I am a Lion!', id: 'lion1' },
lion2: { name: 'I am a second Lion!', id: 'lion2' },
},
];
const expectedSequence = [
[{ name: 'I am a Lion!', id: 'lion1' }],
[
// Alphanumeric sort by name.
{ name: 'I am a Lion!', id: 'lion1' },
{ name: 'I am a second Lion!', id: 'lion2' },
],
];
const itemStub = MockNgRedux.getSelectorStub(['lion', 'items']);
mockStoreSequence.forEach(value => itemStub.next(value));
itemStub.complete();
lionPage.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(LionPageComponent);
const lionPage = fixture.componentInstance;
const lionsLoadingStub = MockNgRedux.getSelectorStub(['lion', 'loading']);
lionsLoadingStub.next(false);
lionsLoadingStub.next(true);
lionsLoadingStub.complete();
lionPage.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(LionPageComponent);
const lionPage = fixture.componentInstance;
const lionsErrorStub = MockNgRedux.getSelectorStub(['lion', 'error']);
lionsErrorStub.next(false);
lionsErrorStub.next(true);
lionsErrorStub.complete();
lionPage.error
.pipe(toArray())
.subscribe(
actualSequence => expect(actualSequence).toEqual([false, true]),
undefined,
done,
);
});
it('should load lions on creation', () => {
const spy = spyOn(MockNgRedux.getInstance(), 'dispatch');
TestBed.createComponent(LionPageComponent);
expect(spy).toHaveBeenCalledWith({
type: AnimalAPIActions.LOAD_ANIMALS,
meta: { animalType: ANIMAL_TYPES.LION },
});
});
});