Skip to content

Commit

Permalink
docs(store): add section for provideMockStore to testing guide (#1591)
Browse files Browse the repository at this point in the history
  • Loading branch information
lacolaco authored and brandonroberts committed Mar 1, 2019
1 parent 1ccbd49 commit 5cb9a46
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion projects/ngrx.io/content/guide/store/testing.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
# Testing

### Providing Store for testing
### Using a Mock Store

The `provideMockStore()` function registers providers that allow you to mock out the `Store` for testing functionality that has a dependency on `Store` without setting up reducers.
You can write tests validating behaviors corresponding to the specific state snapshot easily.

<div class="alert is-helpful">

**Note:** All dispatched actions don't affect to the state, but you can see them in the `Actions` stream.

</div>

Usage:

<code-example header="auth.guard.spec.ts">
import { TestBed } from '@angular/core/testing';
import { Store } from '@ngrx/store';
import { provideMockStore } from '@ngrx/store/testing';
import { cold } from 'jasmine-marbles';

import { AuthGuard } from '../guards/auth.guard';
import * as AuthActions from '../actions/auth-actions';

describe('Auth Guard', () => {
let guard: AuthGuard;
let store: MockStore&lt;{ loggedIn: boolean }&gt;;
const initialState = { loggedIn: false };

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
// any modules needed
],
providers: [
AuthGuard,
provideMockStore({ initialState }),
// other providers
],
});

guard = TestBed.get(AuthGuard);
store = TestBed.get(Store);
});

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({ loggedIn: true });

const expected = cold('(a|)', { a: true });

expect(guard.canActivate()).toBeObservable(expected);
});
});
</code-example>

### Using Store for Integration Testing

Use the `StoreModule.forRoot` in your `TestBed` configuration when testing components or services that inject `Store`.

Expand Down

0 comments on commit 5cb9a46

Please sign in to comment.