|
| 1 | +import { TestBed, async } from '@angular/core/testing'; |
| 2 | +import { NgReduxTestingModule, MockNgRedux } from '@angular-redux/store/testing'; |
| 3 | + |
| 4 | +import { Component, Input } from '@angular/core'; |
| 5 | +import { NgRedux } from '@angular-redux/store'; |
| 6 | + |
| 7 | +import { Observable } from 'rxjs/Observable'; |
| 8 | +import 'rxjs/add/observable/of'; |
| 9 | +import 'rxjs/add/operator/toArray'; |
| 10 | +import 'rxjs/add/operator/do'; |
| 11 | + |
| 12 | +import { LionPageComponent } from './lion-page.container'; |
| 13 | +import { AnimalActions } from '../animals/animal.actions'; |
| 14 | +import { ANIMAL_TYPES } from '../animals/animal.types'; |
| 15 | + |
| 16 | +@Component({ |
| 17 | + selector: 'zoo-animal-list', |
| 18 | + template: 'Mock Animal List', |
| 19 | +}) |
| 20 | +class MockAnimalListComponent { |
| 21 | + @Input() animalsName: string; |
| 22 | + @Input() animals: Observable<any>; |
| 23 | + @Input() loading: Observable<boolean>; |
| 24 | + @Input() error: Observable<any>; |
| 25 | +}; |
| 26 | + |
| 27 | +describe('Lion Page Container', () => { |
| 28 | + beforeEach(() => { |
| 29 | + TestBed.configureTestingModule({ |
| 30 | + declarations: [LionPageComponent, MockAnimalListComponent], |
| 31 | + imports: [NgReduxTestingModule], |
| 32 | + providers: [AnimalActions], |
| 33 | + }).compileComponents(); |
| 34 | + |
| 35 | + MockNgRedux.reset(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should select some lions from the Redux store', done => { |
| 39 | + const fixture = TestBed.createComponent(LionPageComponent); |
| 40 | + const lionPage = fixture.debugElement.componentInstance; |
| 41 | + const expectedSequence = [ |
| 42 | + [ { name: 'I am a Lion!' } ], |
| 43 | + [ { name: 'I am a Lion!' }, { name: 'I am a second Lion!' } ] |
| 44 | + ]; |
| 45 | + |
| 46 | + const lionItemsStub = MockNgRedux.getSelectorStub(['lions', 'items']); |
| 47 | + expectedSequence.forEach(value => lionItemsStub.next(value)); |
| 48 | + lionItemsStub.complete(); |
| 49 | + |
| 50 | + lionPage.animals$ |
| 51 | + .toArray() |
| 52 | + .subscribe( |
| 53 | + actualSequence => expect(actualSequence).toEqual(expectedSequence), |
| 54 | + null, |
| 55 | + done); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should know when the animals are loading', done => { |
| 59 | + const fixture = TestBed.createComponent(LionPageComponent); |
| 60 | + const lionPage = fixture.debugElement.componentInstance; |
| 61 | + |
| 62 | + const lionsLoadingStub = MockNgRedux.getSelectorStub(['lions', 'loading']); |
| 63 | + lionsLoadingStub.next(false); |
| 64 | + lionsLoadingStub.next(true); |
| 65 | + lionsLoadingStub.complete(); |
| 66 | + |
| 67 | + lionPage.loading$ |
| 68 | + .toArray() |
| 69 | + .subscribe( |
| 70 | + actualSequence => expect(actualSequence).toEqual([ false, true ]), |
| 71 | + null, |
| 72 | + done); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should know when there\'s an error', done => { |
| 76 | + const fixture = TestBed.createComponent(LionPageComponent); |
| 77 | + const lionPage = fixture.debugElement.componentInstance; |
| 78 | + const expectedSequence = [ true ]; |
| 79 | + |
| 80 | + const lionsErrorStub = MockNgRedux.getSelectorStub(['lions', 'error']); |
| 81 | + lionsErrorStub.next(false); |
| 82 | + lionsErrorStub.next(true); |
| 83 | + lionsErrorStub.complete(); |
| 84 | + |
| 85 | + lionPage.error$ |
| 86 | + .toArray() |
| 87 | + .subscribe( |
| 88 | + actualSequence => expect(actualSequence).toEqual([ false, true ]), |
| 89 | + null, |
| 90 | + done); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should load lions on creation', () => { |
| 94 | + const spy = spyOn(MockNgRedux.mockInstance, 'dispatch'); |
| 95 | + const fixture = TestBed.createComponent(LionPageComponent); |
| 96 | + |
| 97 | + expect(spy).toHaveBeenCalledWith({ |
| 98 | + type: AnimalActions.LOAD_STARTED, |
| 99 | + meta: { animalType: ANIMAL_TYPES.LION }, |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments