From 7a806e299124a79ec242cd81cd89115cbe2f89a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Stanimirovi=C4=87?= Date: Tue, 29 Dec 2020 15:22:22 +0100 Subject: [PATCH] docs(store): add 'Testing without TestBed' example (#2836) --- .../ngrx.io/content/guide/store/testing.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/projects/ngrx.io/content/guide/store/testing.md b/projects/ngrx.io/content/guide/store/testing.md index 2c93b1fac3..81008ce81b 100644 --- a/projects/ngrx.io/content/guide/store/testing.md +++ b/projects/ngrx.io/content/guide/store/testing.md @@ -105,3 +105,42 @@ You can use the projector function used by the selector by accessing the `.proje The following example tests the `booksReducer` from the [walkthrough](guide/store/walkthrough). In the first test we check that the state returns the same reference when the reducer is not supposed to handle the action (unknown action). The second test checks that `retrievedBookList` action updates the state and returns the new instance of it. + +### Testing without `TestBed` + +The `provideMockStore()` function can be also used with `Injector.create`: + + +import { MockStore, provideMockStore } from '@ngrx/store/testing'; +import { Injector } from '@angular/core'; + +describe('Books Component', () => { + let store: MockStore; + const initialState = { books: ['Book 1', 'Book 2', 'Book 3'] }; + + beforeEach(() => { + const injector = Injector.create({ + providers: [ + provideMockStore({ initialState }), + ], + }); + + store = injector.get(MockStore); + }); +}); + + +Another option to create the `MockStore` without `TestBed` is by calling the `getMockStore()` function: + + +import { MockStore, getMockStore } from '@ngrx/store/testing'; + +describe('Books Component', () => { + let store: MockStore; + const initialState = { books: ['Book 1', 'Book 2', 'Book 3'] }; + + beforeEach(() => { + store = getMockStore({ initialState }); + }); +}); +