Skip to content

Instantly share code, notes, and snippets.

@jtcrowson
Last active Mar 12, 2019
Embed
What would you like to do?
auth-guard.service.spec.ts from version 7.3.0 of ngrx/platform using MockStore
// Future version of example-app using MockStore
import { provideMockStore, MockStore } from '@ngrx/store/testing';
describe('Auth Guard', () => {
let guard: AuthGuard;
let store: MockStore<fromAuth.State>;
const initialState = {
auth: {
loginPage: {} as fromLoginPage.State,
status: {
user: null,
},
},
} as fromAuth.State;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [AuthGuard, provideMockStore({ initialState })],
});
store = TestBed.get(Store);
guard = TestBed.get(AuthGuard);
});
it('should return false if the user state is not logged in', () => {
const expected = cold('(a|)', { a: false });
expect(guard.canActivate()).toBeObservable(expected);
});
it('should return true if the user state is logged in', () => {
store.setState({
...initialState,
auth: {
loginPage: {} as fromLoginPage.State,
status: {
user: {
name: 'John',
},
},
},
});
const expected = cold('(a|)', { a: true });
expect(guard.canActivate()).toBeObservable(expected);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment