Skip to content

Instantly share code, notes, and snippets.

@marinho
Last active September 20, 2016 11:25
Show Gist options
  • Save marinho/ac6bc0431399241432ead5b01d6e7fe5 to your computer and use it in GitHub Desktop.
Save marinho/ac6bc0431399241432ead5b01d6e7fe5 to your computer and use it in GitHub Desktop.
Basic example of a Redux action being tested in Angular 2 (final version)
/*
* Testing a Redux based action in Angular 2 app (using ng2-redux)
* More info: https://angular.io/docs/ts/latest/guide/testing.html#!#inject
*/
import { TestBed, async, inject } from '@angular/core/testing';
import { NgRedux } from 'ng2-redux';
import { Observable } from 'rxjs';
import { AppState } from '../../store';
import { rootReducer } from '../../store/reducers';
import { ApiService } from 'apis/c4re';
import { HomeActions } from './home.actions';
describe('HomeActions', () => {
let fakeApiService = {
getSomeData: (args) => {
return Observable.of({some: 'data'});
}
};
let reduxFactory = () => {
let ngRedux = new NgRedux<AppState>();
ngRedux.configureStore(rootReducer, undefined);
return ngRedux;
};
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
{ provide: NgRedux, useFactory: reduxFactory},
{ provide: ApiService, useValue: fakeApiService},
HomeActions
]
});
}));
it('should update queryParams', inject([NgRedux, HomeActions], (ngRedux: NgRedux<AppState>, homeActions: HomeActions) => {
homeActions.getSomeData({ id: 20 });
ngRedux.select([ 'home', 'someData' ])
.subscribe(someData => {
expect(queryParams).toEqual({some: 'data'});
});
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment