1- /* tslint:disable:no-unused-variable */
2- import { TestBed , inject } from '@angular/core/testing' ;
31import { Http , BaseRequestOptions , ConnectionBackend , RequestOptions } from '@angular/http' ;
42import { MockBackend } from '@angular/http/testing' ;
5- import { CoreModule } from '../../core.module' ;
63import { AuthEffects } from './auth.effects' ;
4+ import { EffectsTestingModule , EffectsRunner } from '@ngrx/effects/testing' ;
5+ import { LogoutAction , ActionTypes , LoadAndProcessTokenAction , AuthSuccessAction } from './auth.actions' ;
6+ import { TestBed , inject } from '@angular/core/testing' ;
7+ import { routerActions } from '@ngrx/router-store' ;
8+
79
810describe ( 'Effects: Auth' , ( ) => {
911
12+ let runner : EffectsRunner = null ;
13+ let authEffects : AuthEffects = null ;
14+
1015 beforeEach ( ( ) => {
1116 TestBed . configureTestingModule ( {
1217 imports : [
13- CoreModule
18+ EffectsTestingModule
1419 ] ,
1520 providers : [
1621 BaseRequestOptions ,
@@ -27,9 +32,60 @@ describe('Effects: Auth', () => {
2732 } ) ;
2833 } ) ;
2934
35+ beforeEach ( inject ( [ EffectsRunner , AuthEffects ] , ( _runner : EffectsRunner , _authEffects : AuthEffects ) => {
36+ runner = _runner ;
37+ authEffects = _authEffects ;
38+ _authEffects . jwtToken = null ;
39+ }
40+ ) ) ;
41+
3042 it ( 'should exist' , inject ( [ AuthEffects ] , ( service : AuthEffects ) => {
3143 expect ( service ) . toBeTruthy ( ) ;
3244 } ) ) ;
3345
46+ it ( 'should return a CLEAR_AUTH_STATE action after logging out' , ( ) => {
47+ runner . queue ( new LogoutAction ( ) ) ;
48+
49+ authEffects . logout$ . subscribe ( result => {
50+ expect ( result . type ) . toEqual ( ActionTypes . CLEAR_AUTH_STATE ) ;
51+ } ) ;
52+
53+ } ) ;
54+
55+ it ( 'should return a PROCESS_TOKEN action after LoadAndProcessTokenAction when token is set' , ( ) => {
56+ authEffects . jwtToken = 'NON_EMPTY' ;
57+ runner . queue ( new LoadAndProcessTokenAction ( ) ) ;
58+ authEffects . loadAndProcessToken$ . subscribe ( result => {
59+ expect ( result . type ) . toEqual ( ActionTypes . PROCESS_TOKEN ) ;
60+ } ) ;
61+ } ) ;
62+
63+ it ( 'should return a no action after LoadAndProcessTokenAction when token is not set' , ( ) => {
64+ authEffects . jwtToken = null ;
65+ runner . queue ( new LoadAndProcessTokenAction ( ) ) ;
66+
67+ authEffects . loadAndProcessToken$ . subscribe ( result => {
68+ expect ( result ) . toBeFalsy ( ) ;
69+ expect ( result ) . toBeTruthy ( ) ;
70+ } ) ;
71+ } ) ;
72+
73+ it ( 'should return actions after auth success' , ( ) => {
74+ authEffects . jwtToken = null ;
75+ runner . queue ( new AuthSuccessAction ( 'TOKEN' ) ) ;
76+ let eventCount = 0 ;
77+ authEffects . authSuccess$ . subscribe ( result => {
78+ eventCount ++ ;
79+ if ( eventCount === 1 ) {
80+ expect ( result . type ) . toEqual ( routerActions . GO ) ;
81+ }
82+
83+ if ( eventCount === 2 ) {
84+ expect ( result . type ) . toEqual ( ActionTypes . PROCESS_TOKEN ) ;
85+ }
86+
87+ } ) ;
88+ } ) ;
89+
3490
3591} ) ;
0 commit comments