Angular and NgRx
  • Introduction
  • Setup
  • 1. Create an application
  • 2. Create a home component
  • 3. Test HomeComponent
  • 4. Create event feature module
  • 5.Create AddAttendeeComponent
  • 6. Test AddAttendeeComponent
  • 7. Listen to child component events
  • 8. Add test for the event emitter
  • 9. Create EventListComponent
  • 10. Test EventListComponent
  • 11. Create EventService
  • 12. Test EventService
  • 13. Add simple NgRx spinner
  • 14. Test reducer
  • 15. Strongly type our store
  • 16. Update reducer tests
  • 17. Store dev tools
  • 18. Create selectors
  • 19. Create feature state
  • 20. Create effect
  • 21. Test an effect
  • 22. Use Entity Adapter
  • 23. Add attendee logic
  • 24. Router store
  • 25. Fix EventComponent tests
Powered by GitBook
On this page
  • 1. Fix broken EventComponent test
  • 2. Install jest-marbles
  • 3. Add effect spec file
  • StackBlitz Link
  • Extras and Homework
  • Read more about testing observables

21. Test an effect

In this section we will use marble tests to test our effects observable streams.

1. Fix broken EventComponent test

  • Add the dispatch method to our fake Store provided.

src/app/event/containers/event/event.component.spec.ts
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { EventComponent } from './event.component';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { EventService } from '../../services/event.service';
import { Store } from '@ngrx/store';

describe('EventComponent', () => {
  let component: EventComponent;
  let fixture: ComponentFixture<EventComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      providers: [
        {
          provide: EventService,
          useValue: {
            getAttendees: () => {},
          }
        },
        {
          provide: Store,
          useValue: {
            pipe: () => {},
            dispatch: () => {}
          }
        }
      ],
      declarations: [EventComponent],
      schemas: [NO_ERRORS_SCHEMA]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(EventComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

2. Install jest-marbles

Important to use @latest which is for version 6 of RxJS.

npm i jest-marbles@latest -D

3. Add effect spec file

  • Add attendees.effects.spec.ts file.

Unknown why toBeObservable Matcher not working with jest?

src/app/event/state/attendees/attendees.effects.spec.ts
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { hot, cold } from 'jest-marbles';
import { of, Observable } from 'rxjs';

import { AttendeesEffects } from './attendees.effects';
import {
  LoadAttendees,
  LoadAttendeesSuccess,
  AttendeesActions
} from './attendees.actions';
import { Attendee } from '../../../models';
import { EventService } from '../../services/event.service';

describe(`Effect: Attendess`, () => {
  let actions: Observable<AttendeesActions>;
  let effects: AttendeesEffects;
  let service: EventService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [],
      providers: [
        AttendeesEffects,
        provideMockActions(() => actions),
        {
          provide: EventService,
          useValue: {
            getAttendees: jest.fn()
          }
        }
      ]
    });

    service = TestBed.get(EventService);
    effects = TestBed.get(AttendeesEffects);
  });

  it('should return a cold Observable of LoadAttendeesSuccess action', () => {
    const fakeAttendees = [
      { name: 'Duncan', attending: false, guests: 0 } as Attendee
    ];
    const action = new LoadAttendees();
    const completion = new LoadAttendeesSuccess(fakeAttendees);

    jest
      .spyOn(service, 'getAttendees')
      .mockImplementation(() => of(fakeAttendees));

    actions = hot('--a-', { a: action });
    const expected = cold('--(b)', { b: completion });
    expect(effects.getAttendees$).toBeObservable(expected);
  });
});

StackBlitz Link

Extras and Homework

These extra sections are for doing after the course or if you finish a section early. Please move onto the next section if doing this as a workshop when the instructor advises.

WARNING: Some of these extra sections will make it more difficult to copy and paste the code examples later on in the course.

You might need to apply the code snippet examples a little more carefully amongst any "extras section" code you may add. If you are up for some extra challenges these sections are for you.

Read more about testing observables

Previous20. Create effectNext22. Use Entity Adapter

Last updated 6 years ago

More docs .

https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md
Duncanhunter - Angular And Ngrx Demo App - StackBlitzStackBlitz
Web Link: Link to the demo app running in StackBlitz
Testing Observables in AngularMedium
Logo
Logo