Sébastien Lucas / Dev wiki
search
Ctrlk
  • Blog Wiki
  • Story book
  • Developers comics
  • Angularchevron-right
    • 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 & Angularchevron-right
    • Communication between components
    • Angular snippet library
    • Release and version management
    • Angular tutorial selection
    • UI components libraries
    • Angular libraries
    • Angular Tutorials
    • NGRXchevron-right
      • 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
  • Javascriptchevron-right
  • RXJSchevron-right
  • NODE.jschevron-right
  • Gatsby / Reactchevron-right
  • Ghost blog
  • Services for developerschevron-right
  • MISCchevron-right
  • AIchevron-right
  • Typescriptchevron-right
  • Dockerchevron-right
  • Mongodbchevron-right
  • Devopschevron-right
  • Developers ressourceschevron-right
  • Toolingchevron-right
  • HTML / CSSchevron-right
  • Testschevron-right
  • REVIT & AEC tools
  • Gitchevron-right
  • Design Patternchevron-right
  • Job board / Remote jobs
  • SVG
  • JSON
  • Githubchevron-right
  • NPMchevron-right
  • Yarnchevron-right
gitbookPowered by GitBook
block-quoteOn this pagechevron-down
  1. Angularchevron-right
  2. NGRX

NGRX unit test

hashtag
What to test

  • Actions

  • Effects

  • Reducer

  • Selector

hashtag
How to

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

hashtag
Marbles test

  • https://medium.com/@bencabanes/marble-testing-observable-introduction-1f5ad39231carrow-up-right

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

hashtag
Tricks

hashtag
Testing an effect not returning action

  • https://stackoverflow.com/questions/53358677/ngrx-effects-testing-with-dispatch-falsearrow-up-right

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

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

the code

The test

PreviousAngular new Redux (alternative to ngrx)chevron-leftNextAngular ngrx / Basicschevron-right

Last updated 6 years ago

  • What to test
  • How to
  • Marbles test
  • Tricks
@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);
    });
  });
});