14. Test reducer
In this section we will start to see the benefit of using redux in making out testing simpler as we separate out the concerns of updating state with those of rendering views in a component.
1. Fix broken EventComponent test
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { Store } from '@ngrx/store';
import { NO_ERRORS_SCHEMA } from '@angular/compiler/src/core';
import { HttpClient } from '@angular/common/http';
import { EventComponent } from './event.component';
import { EventService } from '../../services/event.service';
describe('EventComponent', () => {
let component: EventComponent;
let fixture: ComponentFixture<EventComponent>;
let service: EventService;
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
{ provide: HttpClient, useValue: null },
{
provide: EventService,
useValue: {
getAttendees: () => {}
}
},
{
provide: Store,
useValue: {
pipe: () => {}
}
}
],
declarations: [EventComponent],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EventComponent);
component = fixture.componentInstance;
service = TestBed.get(EventService);
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should have a list of attendees set', () => {
const fakeAttendees = [{ name: 'FAKE_NAME', attending: false, guests: 0 }];
jest
.spyOn(service, 'getAttendees')
.mockImplementation(() => of(fakeAttendees));
component.ngOnInit();
component.attendees$.subscribe(attendees => {
expect(attendees).toEqual(fakeAttendees);
});
});
});
2. Test a reducer
StackBlitz Link
Last updated