Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricduffournet committed Dec 6, 2021
1 parent 26d4749 commit 2e76aa4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 54 deletions.
50 changes: 0 additions & 50 deletions modules/store/testing/spec/mock_store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,53 +516,3 @@ describe('Refreshing state', () => {
expect(getTodoItems('li')[0].nativeElement.textContent.trim()).toBe('bbb');
});
});

describe('Cleanup selectors after each test', () => {
let store: MockStore;
const selectData = createSelector(
(state: any) => state,
(state) => state.value
);

// see https://github.com/ngrx/platform/issues/3243
afterEach(() => {
store.resetSelectors();
});

it('should return the mocked selectors value', (done: any) => {
TestBed.configureTestingModule({
providers: [
provideMockStore({
initialState: {
value: 100,
},
selectors: [{ selector: selectData, value: 200 }],
}),
],
});

store = TestBed.inject(MockStore);
store.pipe(select(selectData)).subscribe((v) => {
expect(v).toBe(200);
done();
});
});

it('should return the real value', (done: any) => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({} as any, {
initialState: {
value: 300,
},
}),
],
});

const store = TestBed.inject(Store);
store.pipe(select(selectData)).subscribe((v) => {
expect(v).toBe(300);
done();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MemoizedSelector } from '@ngrx/store';
import { Store, StoreModule } from '@ngrx/store';
import { provideMockStore, MockStore } from '@ngrx/store/testing';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
Expand Down Expand Up @@ -96,3 +97,93 @@ describe('AppComponent', () => {
//#enddocregion mockSelector
});
});

//#docregion resetMockSelector
describe('AppComponent reset selectors', () => {
let store: MockStore<AppState>;

afterEach(() => {
store.resetSelectors();
});

it('should return the mocked value', (done: any) => {
TestBed.configureTestingModule({
providers: [
provideMockStore({
initialState: [
{
id: 'firstId',
volumeInfo: {
title: 'First Title',
authors: ['First Author'],
},
},
],
selectors: [
{
selector: selectBooks,
value: [
{
id: 'secondId',
volumeInfo: {
title: 'Second Title',
authors: ['Second Author'],
},
},
],
},
],
}),
],
});

store.select(selectBooks).subscribe((mockBooks) => {
expect(mockBooks).toEqual([
{
id: 'secondId',
volumeInfo: {
title: 'Second Title',
authors: ['Second Author'],
},
},
]);
done();
});
});

it('should return the real value', (done: any) => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({} as any, {
initialState: {
books: [
{
id: 'thirdId',
volumeInfo: {
title: 'Third Title',
authors: ['Third Author'],
},
},
],
},
}),
],
});

const store = TestBed.inject(Store);

store.select(selectBooks).subscribe((mockBooks) => {
expect(mockBooks).toEqual([
{
id: 'thirdId',
volumeInfo: {
title: 'OThird Title',
authors: ['Third Author'],
},
},
]);
done();
});
});
});
//#edndocregion resetMockSelector
6 changes: 2 additions & 4 deletions projects/ngrx.io/content/guide/store/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ Usage:

In this example based on the [walkthrough](guide/store/walkthrough), we mock the `selectBooks` selector by using `overrideSelector`, passing in the `selectBooks` selector with a default mocked return value of an array of books. Similarly, we mock the `selectBookCollection` selector and pass the selector together with another array. In the test, we use `setResult()` to update the mock selectors to return new array values, then we use `MockStore.refreshState()` to trigger an emission from the `selectBooks` and `selectBookCollection` selectors.

<div class="alert is-helpful">

**Note:** `MockStore` will **not** reset mocked selectors after each test. You can reset selectors by calling the `MockStore.resetSelectors()` method in `afterEach()` hook.
You can reset selectors by calling the `MockStore.resetSelectors()` method in `afterEach()` hook.

</div>
<code-example header="src/app/app.component.spec.ts (Reset Mock Selector) " path="store-walkthrough/src/app/tests/app.component.1.spec.ts" region="resetMockSelector"></code-example>

Try the <live-example name="testing-store"></live-example>.

Expand Down

0 comments on commit 2e76aa4

Please sign in to comment.