/book.effects.spec.ts Secret
Last active
March 12, 2019 20:30
Star
You must be signed in to star a gist
Adapted book.effects.spec.ts from version 7.3.0 of ngrx/platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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