-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): add testing package (#1027)
Introduces `@ngrx/store/testing` that provides a mock store for using in test setup Closes #915 ```ts import { Store } from '@ngrx/store'; import { provideMockStore, MockStore } from '@ngrx/store/testing'; import { take } from 'rxjs/operators'; describe('Mock Store', () => { let mockStore: MockStore<{ counter1: number, counter2: number }>; beforeEach(() => { const initialState = { counter1: 0, counter2: 1 }; TestBed.configureTestingModule({ providers: [provideMockStore({ initialState })], }); mockStore = TestBed.get(Store); }); it('should set the new state', () => { mockStore.setState({ counter1: 1, counter2: 2 }); mockStore.pipe(take(1)).subscribe(state => { expect(state.counter1).toBe(1); }); }); }); ```
- Loading branch information
1 parent
d9de463
commit ab56aac
Showing
12 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
exports_files(["package.json"]) | ||
|
||
load("//tools:defaults.bzl", "ng_module") | ||
|
||
ng_module( | ||
name = "testing", | ||
srcs = glob([ | ||
"*.ts", | ||
"src/**/*.ts", | ||
]), | ||
module_name = "@ngrx/store/testing", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//modules/store", | ||
"@rxjs", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src/testing'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@ngrx/store/testing", | ||
"typings": "./testing.d.ts", | ||
"main": "../bundles/core-testing.umd.js", | ||
"module": "../fesm5/testing.js", | ||
"es2015": "../fesm2015/testing.js", | ||
"esm5": "../esm5/testing/testing.js", | ||
"esm2015": "../esm2015/testing/testing.js", | ||
"fesm5": "../fesm5/testing.js", | ||
"fesm2015": "../fesm2015/testing.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default { | ||
entry: './dist/store/@ngrx/store/testing.es5.js', | ||
dest: './dist/store/bundles/store-testing.umd.js', | ||
format: 'umd', | ||
exports: 'named', | ||
moduleName: 'ngrx.store.testing', | ||
globals: { | ||
'@angular/core': 'ng.core', | ||
'@ngrx/store': 'ngrx.store', | ||
'rxjs': 'Rx', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { ActionReducer } from '@ngrx/store'; | ||
|
||
@Injectable() | ||
export class MockReducerManager extends BehaviorSubject< | ||
ActionReducer<any, any> | ||
> { | ||
constructor() { | ||
super(() => undefined); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class MockState<T extends {}> extends BehaviorSubject<T> { | ||
constructor() { | ||
super(<T>{}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Inject, Injectable } from '@angular/core'; | ||
import { Observable, BehaviorSubject } from 'rxjs'; | ||
import { | ||
Action, | ||
ActionsSubject, | ||
INITIAL_STATE, | ||
ReducerManager, | ||
Store, | ||
} from '@ngrx/store'; | ||
import { MockState } from './mock_state'; | ||
|
||
@Injectable() | ||
export class MockStore<T> extends Store<T> { | ||
public scannedActions$: Observable<Action>; | ||
|
||
constructor( | ||
private state$: MockState<T>, | ||
actionsObserver: ActionsSubject, | ||
reducerManager: ReducerManager, | ||
@Inject(INITIAL_STATE) private initialState: T | ||
) { | ||
super(state$, actionsObserver, reducerManager); | ||
this.state$.next(this.initialState); | ||
this.scannedActions$ = actionsObserver.asObservable(); | ||
} | ||
|
||
setState(nextState: T): void { | ||
this.state$.next(nextState); | ||
} | ||
|
||
addReducer() { | ||
/* noop */ | ||
} | ||
|
||
removeReducer() { | ||
/* noop */ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Provider } from '@angular/core'; | ||
import { MockState } from './mock_state'; | ||
import { | ||
ActionsSubject, | ||
INITIAL_STATE, | ||
ReducerManager, | ||
StateObservable, | ||
Store, | ||
} from '@ngrx/store'; | ||
import { MockStore } from './mock_store'; | ||
import { MockReducerManager } from './mock_reducer_manager'; | ||
|
||
export interface MockStoreConfig<T> { | ||
initialState?: T; | ||
} | ||
|
||
export function provideMockStore<T = any>( | ||
config: MockStoreConfig<T> = {} | ||
): Provider[] { | ||
return [ | ||
ActionsSubject, | ||
MockState, | ||
{ provide: INITIAL_STATE, useValue: config.initialState }, | ||
{ provide: StateObservable, useClass: MockState }, | ||
{ provide: ReducerManager, useClass: MockReducerManager }, | ||
{ provide: Store, useClass: MockStore }, | ||
]; | ||
} | ||
|
||
export { MockReducerManager } from './mock_reducer_manager'; | ||
export { MockState } from './mock_state'; | ||
export { MockStore } from './mock_store'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "../tsconfig-build", | ||
"compilerOptions": { | ||
"paths": { | ||
"@ngrx/store": ["../../dist/packages/store"] | ||
} | ||
}, | ||
"files": ["index.ts"], | ||
"angularCompilerOptions": { | ||
// Work around for issue: https://github.com/angular/angular/issues/22210 | ||
"strictMetadataEmit": false | ||
} | ||
} |