Skip to content

Instantly share code, notes, and snippets.

@jtcrowson
Last active March 12, 2019 20:31
Show Gist options
  • Save jtcrowson/ed4881e111a4d36a81cbc9ba03acf689 to your computer and use it in GitHub Desktop.
Save jtcrowson/ed4881e111a4d36a81cbc9ba03acf689 to your computer and use it in GitHub Desktop.
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