Skip to content

Instantly share code, notes, and snippets.

@jtcrowson
Last active March 12, 2019 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtcrowson/34c745d736c02eaa82f9e9e0d32c4fde to your computer and use it in GitHub Desktop.
Save jtcrowson/34c745d736c02eaa82f9e9e0d32c4fde to your computer and use it in GitHub Desktop.
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