-
-
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
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
// 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