Sébastien Lucas / Dev wiki
Ctrlk
  • Blog Wiki
  • Story book
  • Developers comics
  • Angular
    • Angular CLI + Angular.json
    • ngTemplateOutlet
    • Angular Schematic
    • Interceptor
    • Micro frontend + Mono repo
    • Build a library module
    • Error handling
    • Virtual and infinite scroll
    • Angular i18n
    • Debug Angular
    • Angular LifeCycle Hook
    • Nested forms in Angular
    • Angular Recipes
    • Master component in Angular
    • Perfomance optimization
    • Service Workers + PWA
    • Mobile
    • Electron app / Desktop app
    • Unit test & Angular
    • Communication between components
    • Angular snippet library
    • Release and version management
    • Angular tutorial selection
    • UI components libraries
    • Angular libraries
    • Angular Tutorials
    • NGRX
      • Angular NGRX / Tips and code examples
      • Angular new Redux (alternative to ngrx)
      • NGRX unit test
      • Angular ngrx / Basics
    • Angular 2/Angular 1 differences
  • Graphql
  • Three.js
  • Ag grid
  • Open source BIM and 3D
  • Javascript
  • RXJS
  • NODE.js
  • Gatsby / React
  • Ghost blog
  • Services for developers
  • MISC
  • AI
  • Typescript
  • Docker
  • Mongodb
  • Devops
  • Developers ressources
  • Tooling
  • HTML / CSS
  • Tests
  • REVIT & AEC tools
  • Git
  • Design Pattern
  • Job board / Remote jobs
  • SVG
  • JSON
  • Github
  • NPM
  • Yarn
Powered by GitBook
On this page
  1. Angular
  2. NGRX

NGRX unit test

What to test

  • Actions

  • Effects

  • Reducer

  • Selector

How to

  • https://christianlydemann.com/the-complete-guide-to-ngrx-testing/

Marbles test

  • https://medium.com/@bencabanes/marble-testing-observable-introduction-1f5ad39231c

  • https://github.com/jisaacks/RxJS/blob/master/doc/writing-marble-tests.md

Tricks

Testing an effect not returning action

  • https://stackoverflow.com/questions/53358677/ngrx-effects-testing-with-dispatch-false

  • https://stackoverflow.com/questions/48137205/ngrx-effects-testing-an-effect-returns-empty/53188160#53188160

  • Successful for me more simple technic https://stackoverflow.com/questions/48318095/how-to-unit-test-this-effect-with-dispatch-false

the code

The test

PreviousAngular new Redux (alternative to ngrx)NextAngular ngrx / Basics

Last updated 6 years ago

Was this helpful?

  • What to test
  • How to
  • Marbles test
  • Tricks

Was this helpful?

@Injectable()
export class NotificationEffects {
  @Effect({dispatch: false})
  notificationShow$ = this.actions$
    .ofType(notificationAction.NOTIFICATION_SHOW)
    .do((action: notificationAction.NotificationShowAction) => {
      this.notificationService.info(action.payload.config);
    });

  constructor(private actions$: Actions, private notificationService: NotificationService) {}
}
describe('notificationShow$', () => {
  let effects: NotificationEffects;
  let service: any;
  let actions$: Observable<Action>;
  const payload = {test: 123};

  beforeEach( () => {
    TestBed.configureTestingModule( {
      providers: [
        NotificationEffects,
        provideMockActions( () => actions$ ),
        {
          provide: NotificationService,
          useValue: jasmine.createSpyObj('NotificationService', ['info'])
        }
      ]
    } );

    effects = TestBed.get(NotificationEffects);
    service = TestBed.get(NotificationService);
  });

  it('should call a notification service method info with a payload', () => {
    actions$ = cold('a', { a: new notificationAction.NotificationShowAction(payload) });
    effects.notificationShow$.subscribe(() => {
      expect(service.info).toHaveBeenCalledWith(payload);
    });
  });
});