Skip to content

Instantly share code, notes, and snippets.

@jtcrowson
Last active Mar 12, 2019
Embed
What would you like to do?
Adapted book.effects.spec.ts from version 7.3.0 of ngrx/platform
import * as fromBooks from '@example-app/books/reducers';
import * as fromSearch from '@example-app/books/reducers/search.reducer';
import * as fromChildBooks from '@example-app/books/reducers/books.reducer';
// Omitting autoimports
describe('BookEffects', () => {
let effects: BookEffects;
let actions$: Observable<any>;
let store: MockStore<fromBooks.State>;
const initialState = {
books: {
search: {} as fromSearch.State,
books: {} as fromChildBooks.State,
collection: {
loaded: true,
loading: false,
ids: ['1']
}
}
} as fromBooks.State;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
BookEffects,
{
provide: GoogleBooksService,
useValue: { searchBooks: jest.fn() },
},
provideMockActions(() => actions$),
provideMockStore({ initialState }),
],
});
effects = TestBed.get(BookEffects);
actions$ = TestBed.get(Actions);
store = TestBed.get(Store);
spyOn(window, 'alert');
});
describe('addBookSuccess$', () => {
it('should print congratulatory message when adding '
+ 'the first book', (done: any) => {
const action = new AddBookSuccess(generateMockBook());
actions$ = of(action);
effects.addBookSuccess$.subscribe(() => {
expect(window.alert)
.toHaveBeenCalledWith(
'Congrats on adding your first book!'
);
done();
});
});
it('should print number of books after adding '
+ 'the first book', (done: any) => {
store.setState({
...initialState,
books: {
search: {} as fromSearch.State,
books: {} as fromChildBooks.State,
collection: {
loaded: true,
loading: false,
ids: ['1', '2']
}
}
});
const action = new AddBookSuccess(generateMockBook());
actions$ = of(action);
effects.addBookSuccess$.subscribe(() => {
expect(window.alert)
.toHaveBeenCalledWith(
'You have added book number 2'
);
done();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment