feat(store): use provideMockStore outside of the TestBed#2759
feat(store): use provideMockStore outside of the TestBed#2759brandonroberts merged 2 commits intongrx:masterfrom
Conversation
|
Preview docs changes for 93d567a at https://previews.ngrx.io/pr2759-93d567a0/ |
42d8238 to
232ba9c
Compare
ce217ec to
d63fc80
Compare
|
🤔 |
alex-okrushko
left a comment
There was a problem hiding this comment.
I really like where this is going!
| * }); | ||
| * ``` | ||
| */ | ||
| export function provideMockStore<T = any>( |
There was a problem hiding this comment.
How about the following?
This is close to what you had, but I want to make sure that useExisting is used, and it's a bit more compacted.
| export function provideMockStore<T = any>( | |
| export function provideMockStore<T = any>( | |
| config: MockStoreConfig<T> = {} | |
| ): Provider[] { | |
| setNgrxMockEnvironment(true); | |
| return [ | |
| { provide: ActionsSubject, useFactory: () => new ActionsSubject(), deps: []}, | |
| { provide: MockState, useFactory: () => new MockState<T>(), deps: []}, | |
| { provide: MockReducerManager, useFactory: () => new MockReducerManager(), deps: []}, | |
| { provide: INITIAL_STATE, useValue: config.initialState || {} }, | |
| { provide: MOCK_SELECTORS, useValue: config.selectors }, | |
| { provide: StateObservable, useExisting: MockState }, | |
| { provide: ReducerManager, useExisting: MockReducerManager }, | |
| { | |
| provide: MockStore, | |
| useFactory: mockStoreFactory, | |
| deps: [ | |
| MockState, | |
| ActionsSubject, | |
| ReducerManager, | |
| INITIAL_STATE, | |
| MOCK_SELECTORS, | |
| ], | |
| }, | |
| { provide: Store, useExisting: MockStore }, | |
| ]; | |
| } |
There was a problem hiding this comment.
@alex-okrushko
Done. I assumed that StateObservable needs useExisting: MockState, but I added useFactory: () => new MockState() in order to keep the same behavior as previous { provide: StateObservable, useClass: MockState }.
About return type, I kept (ValueProvider | ExistingProvider | FactoryProvider)[], because if Provider[] is used, following code will not compile:
injector = Injector.create({ providers: provideMockStore(...) });because Provider is not compatible with StaticProvider and (ValueProvider | ExistingProvider | FactoryProvider) is compatible with both Provider and StaticProvider.
| mockSelectors | ||
| ); | ||
| } | ||
|
|
There was a problem hiding this comment.
Instead of going through the Injector every time, maybe let's create the getMockStore or createMockStore function:
| export function getMockStore<T>( | |
| config: MockStoreConfig<T> = {} | |
| ): MockStore<T> { | |
| return Injector.create({ providers: [ provideMockStore(config) ]}).get(MockStore); | |
| } |
d63fc80 to
ad4ae11
Compare
ad4ae11 to
93d567a
Compare
| MockStore, | ||
| { | ||
| provide: ActionsSubject, | ||
| useFactory: () => new ActionsSubject(), |
There was a problem hiding this comment.
Why not use useClass here?
| useFactory: () => new ActionsSubject(), | |
| useClass: ActionsSubject, |
There was a problem hiding this comment.
@brandonroberts Because class providers are different for TestBed.configureTestingModule and Injector.create. TestBed.configureTestingModule expects ClassProvider. On the other hand, Injector.create expects StaticClassProvider. However, they both accept FactoryProvider.
There was a problem hiding this comment.
Ok, what about just { provide: ActionsSubject, deps: [] }? I know it's compatible with the Injector, and should be compatible with the TestBed also
There was a problem hiding this comment.
For some reason the ActionsSubject is undefined in this case.
| useFactory: () => new ActionsSubject(), | ||
| deps: [], | ||
| }, | ||
| { provide: MockState, useFactory: () => new MockState<T>(), deps: [] }, |
| { provide: MockState, useFactory: () => new MockState<T>(), deps: [] }, | ||
| { | ||
| provide: MockReducerManager, | ||
| useFactory: () => new MockReducerManager(), |
|
This would be a separate effort, but what about creating a factory function for creating a https://github.com/brandonroberts/react-ngrx-poc/blob/master/src/store.tsx cc: @alex-okrushko |
alex-okrushko
left a comment
There was a problem hiding this comment.
Thanks a lot @markostanimirovic 👍
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
provideMockStorecan only be used withTestBed.configureTestingModule.Closes #2745
What is the new behavior?
provideMockStorecan be used withTestBed.configureTestingModuleandInjector.create.Does this PR introduce a breaking change?
Other information