11import { TestBed , ComponentFixture } from '@angular/core/testing' ;
22import { skip , take } from 'rxjs/operators' ;
3- import { MockStore , provideMockStore } from '@ngrx/store/testing' ;
3+ import {
4+ getMockStore ,
5+ MockReducerManager ,
6+ MockState ,
7+ MockStore ,
8+ provideMockStore ,
9+ } from '@ngrx/store/testing' ;
410import {
511 Store ,
612 createSelector ,
@@ -9,9 +15,14 @@ import {
915 MemoizedSelector ,
1016 createFeatureSelector ,
1117 isNgrxMockEnvironment ,
18+ INITIAL_STATE ,
19+ ActionsSubject ,
20+ INIT ,
21+ StateObservable ,
22+ ReducerManager ,
1223} from '@ngrx/store' ;
1324import { INCREMENT } from '../../spec/fixtures/counter' ;
14- import { Component } from '@angular/core' ;
25+ import { Component , Injector } from '@angular/core' ;
1526import { Observable } from 'rxjs' ;
1627import { By } from '@angular/platform-browser' ;
1728
@@ -22,7 +33,7 @@ interface TestAppSchema {
2233 counter4 ?: number ;
2334}
2435
25- describe ( 'Mock Store' , ( ) => {
36+ describe ( 'Mock Store with TestBed ' , ( ) => {
2637 let mockStore : MockStore < TestAppSchema > ;
2738 const initialState = { counter1 : 0 , counter2 : 1 , counter4 : 3 } ;
2839 const stringSelector = 'counter4' ;
@@ -281,6 +292,150 @@ describe('Mock Store', () => {
281292 } ) ;
282293} ) ;
283294
295+ describe ( 'Mock Store with Injector' , ( ) => {
296+ const initialState = { counter : 0 } as const ;
297+ const mockSelector = { selector : 'counter' , value : 10 } as const ;
298+
299+ describe ( 'Injector.create' , ( ) => {
300+ let injector : Injector ;
301+
302+ beforeEach ( ( ) => {
303+ injector = Injector . create ( {
304+ providers : [
305+ provideMockStore ( { initialState, selectors : [ mockSelector ] } ) ,
306+ ] ,
307+ } ) ;
308+ } ) ;
309+
310+ it ( 'should set NgrxMockEnvironment to true' , ( ) => {
311+ expect ( isNgrxMockEnvironment ( ) ) . toBe ( true ) ;
312+ } ) ;
313+
314+ it ( 'should provide Store' , ( done ) => {
315+ const store : Store < typeof initialState > = injector . get ( Store ) ;
316+
317+ store . pipe ( take ( 1 ) ) . subscribe ( ( state ) => {
318+ expect ( state ) . toBe ( initialState ) ;
319+ done ( ) ;
320+ } ) ;
321+ } ) ;
322+
323+ it ( 'should provide MockStore' , ( done ) => {
324+ const mockStore : MockStore < typeof initialState > = injector . get ( MockStore ) ;
325+
326+ mockStore . pipe ( take ( 1 ) ) . subscribe ( ( state ) => {
327+ expect ( state ) . toBe ( initialState ) ;
328+ done ( ) ;
329+ } ) ;
330+ } ) ;
331+
332+ it ( 'should provide the same instance for Store and MockStore' , ( ) => {
333+ const store : Store < typeof initialState > = injector . get ( Store ) ;
334+ const mockStore : MockStore < typeof initialState > = injector . get ( MockStore ) ;
335+
336+ expect ( store ) . toBe ( mockStore ) ;
337+ } ) ;
338+
339+ it ( 'should use a mock selector' , ( done ) => {
340+ const mockStore : MockStore < typeof initialState > = injector . get ( MockStore ) ;
341+
342+ mockStore
343+ . select ( mockSelector . selector )
344+ . pipe ( take ( 1 ) )
345+ . subscribe ( ( selectedValue ) => {
346+ expect ( selectedValue ) . toBe ( mockSelector . value ) ;
347+ done ( ) ;
348+ } ) ;
349+ } ) ;
350+
351+ it ( 'should provide INITIAL_STATE' , ( ) => {
352+ const providedInitialState = injector . get ( INITIAL_STATE ) ;
353+
354+ expect ( providedInitialState ) . toBe ( initialState ) ;
355+ } ) ;
356+
357+ it ( 'should provide ActionsSubject' , ( done ) => {
358+ const actionsSubject = injector . get ( ActionsSubject ) ;
359+
360+ actionsSubject . pipe ( take ( 1 ) ) . subscribe ( ( action ) => {
361+ expect ( action . type ) . toBe ( INIT ) ;
362+ done ( ) ;
363+ } ) ;
364+ } ) ;
365+
366+ it ( 'should provide MockState' , ( done ) => {
367+ const mockState : MockState < typeof initialState > = injector . get ( MockState ) ;
368+
369+ mockState . pipe ( take ( 1 ) ) . subscribe ( ( state ) => {
370+ expect ( state ) . toEqual ( { } ) ;
371+ done ( ) ;
372+ } ) ;
373+ } ) ;
374+
375+ it ( 'should provide StateObservable' , ( done ) => {
376+ const stateObservable = injector . get ( StateObservable ) ;
377+
378+ stateObservable . pipe ( take ( 1 ) ) . subscribe ( ( state ) => {
379+ expect ( state ) . toEqual ( { } ) ;
380+ done ( ) ;
381+ } ) ;
382+ } ) ;
383+
384+ it ( 'should provide the same instance for MockState and StateObservable' , ( ) => {
385+ const mockState : MockState < typeof initialState > = injector . get ( MockState ) ;
386+ const stateObservable : StateObservable = injector . get ( StateObservable ) ;
387+
388+ expect ( mockState ) . toBe ( stateObservable ) ;
389+ } ) ;
390+
391+ it ( 'should provide ReducerManager' , ( ) => {
392+ const reducerManager = injector . get ( ReducerManager ) ;
393+
394+ expect ( reducerManager . addFeature ) . toEqual ( expect . any ( Function ) ) ;
395+ expect ( reducerManager . addFeatures ) . toEqual ( expect . any ( Function ) ) ;
396+ } ) ;
397+
398+ it ( 'should provide MockReducerManager' , ( ) => {
399+ const mockReducerManager = injector . get ( MockReducerManager ) ;
400+
401+ expect ( mockReducerManager . addFeature ) . toEqual ( expect . any ( Function ) ) ;
402+ expect ( mockReducerManager . addFeatures ) . toEqual ( expect . any ( Function ) ) ;
403+ } ) ;
404+
405+ it ( 'should provide the same instance for ReducerManager and MockReducerManager' , ( ) => {
406+ const reducerManager = injector . get ( ReducerManager ) ;
407+ const mockReducerManager = injector . get ( MockReducerManager ) ;
408+
409+ expect ( reducerManager ) . toBe ( mockReducerManager ) ;
410+ } ) ;
411+ } ) ;
412+
413+ describe ( 'getMockStore' , ( ) => {
414+ let mockStore : MockStore < typeof initialState > ;
415+
416+ beforeEach ( ( ) => {
417+ mockStore = getMockStore ( { initialState, selectors : [ mockSelector ] } ) ;
418+ } ) ;
419+
420+ it ( 'should create MockStore' , ( done ) => {
421+ mockStore . pipe ( take ( 1 ) ) . subscribe ( ( state ) => {
422+ expect ( state ) . toBe ( initialState ) ;
423+ done ( ) ;
424+ } ) ;
425+ } ) ;
426+
427+ it ( 'should use a mock selector' , ( done ) => {
428+ mockStore
429+ . select ( mockSelector . selector )
430+ . pipe ( take ( 1 ) )
431+ . subscribe ( ( selectedValue ) => {
432+ expect ( selectedValue ) . toBe ( mockSelector . value ) ;
433+ done ( ) ;
434+ } ) ;
435+ } ) ;
436+ } ) ;
437+ } ) ;
438+
284439describe ( 'Refreshing state' , ( ) => {
285440 type TodoState = {
286441 items : { name : string ; done : boolean } [ ] ;
0 commit comments