vendredi 1 décembre 2017

Angular 4.4 testing service with ng2-redux (angular-redux)

I can't catch dispatch method in my service. As far as I understand, there is problem, because dispatch that I need is into Observable.subscribe().

Here is service:

@Injectable()
export class AuthUserBackendService extends BaseBackendDataService{

constructor(
 private http: Http,
 private tokenLoader: TokenLoaderBackendService,
 private ngRedux: NgRedux<IAppState>,
 private logger: LoggerService,
 private errorService: BackendDataErrorsService
) {
  super(http, tokenLoader, ngRedux, logger, errorService);
} 
........
testMethod() {

// this dispatch ok
// this.ngRedux.dispatch({type: 
ActiontionsBackendData.authUserBackendActions.startHttpRequestAuthUser});

Observable.of({Id: 'testId', Name: 'TestName'}).delay(100).subscribe(
  (result) => {

    const payload = {authUser: result};

    // this dispatch I can't catch in test
    this.ngRedux.dispatch({type: ActionsBackendData. authUserBackendActions. 
    receivedAuthUserHttpResponseSuccess, payload: payload});
   });
 }
}

Here is my .spec.ts

let testService: AuthUserBackendService;
let mockState: any = {
authUserState: {
  authUser: null
 }
};
let mockRedux: NgRedux<IAppState> = new MockRedux(mockState);


describe('AuthUserBackendService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
  imports: [
    ToastyModule,
    TranslateModule.forRoot(),
    NgReduxModule,
    LoggerModule,
    MockBackendModule,
    RouterTestingModule.withRoutes([]),
    BackendDataModule,
    CoreModule
  ],
  providers: [
    {provide: Router, useClass: RouterTestingModule},
    {provide: NgRedux, useValue: mockRedux},

  ]
});



  testService = TestBed.get(AuthUserBackendService);

 });

  it('should be created', () => {
    expect(testService).toBeTruthy();
  });

 describe('testMethod()', ()=>{

it('catch dispatch calls number', ()=> {

  const spy = spyOn(mockRedux, 'dispatch');

  testService.testMethod();

  expect(spy.calls.count()).toEqual(1);

  });

 });

});

So for 'catch dispatch calls number' I get Expected 0 to equal 1. If I uncomment first dispatch, test will be ok, but it means, that I can't catch dispatch in subscribe area. What is wrong? Thanks a lot for you advise.

Aucun commentaire:

Enregistrer un commentaire