UNPKG

13.1 kBSource Map (JSON)View Raw
1{"version":3,"file":"ngrx-store-testing.js","sources":["../../../../modules/store/testing/src/mock_state.ts","../../../../modules/store/testing/src/tokens.ts","../../../../modules/store/testing/src/mock_store.ts","../../../../modules/store/testing/src/mock_reducer_manager.ts","../../../../modules/store/testing/src/testing.ts","../../../../modules/store/testing/src/ngrx-store-testing.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport { BehaviorSubject } from 'rxjs';\n\n@Injectable()\nexport class MockState<T extends {}> extends BehaviorSubject<T> {\n constructor() {\n super(<T>{});\n }\n}\n","import { InjectionToken } from '@angular/core';\n\nexport const MOCK_SELECTORS = new InjectionToken('@ngrx/store Mock Selectors');\n","import { Inject, Injectable } from '@angular/core';\nimport { TestBed } from '@angular/core/testing';\nimport { Observable, BehaviorSubject } from 'rxjs';\nimport {\n Action,\n ActionsSubject,\n INITIAL_STATE,\n ReducerManager,\n Store,\n createSelector,\n MemoizedSelectorWithProps,\n MemoizedSelector,\n} from '@ngrx/store';\nimport { MockState } from './mock_state';\nimport { MockSelector } from './mock_selector';\nimport { MOCK_SELECTORS } from './tokens';\n\nif (typeof afterEach === 'function') {\n afterEach(() => {\n try {\n const mockStore: MockStore | undefined = TestBed.inject(MockStore);\n if (mockStore) {\n mockStore.resetSelectors();\n }\n // eslint-disable-next-line no-empty\n } catch {}\n });\n}\n\ntype OnlyMemoized<T, Result> = T extends string | MemoizedSelector<any, any>\n ? MemoizedSelector<any, Result>\n : T extends MemoizedSelectorWithProps<any, any, any>\n ? MemoizedSelectorWithProps<any, any, Result>\n : never;\n\ntype Memoized<Result> =\n | MemoizedSelector<any, Result>\n | MemoizedSelectorWithProps<any, any, Result>;\n\n@Injectable()\nexport class MockStore<T = object> extends Store<T> {\n private readonly selectors = new Map<Memoized<any> | string, any>();\n\n readonly scannedActions$: Observable<Action>;\n private lastState?: T;\n\n constructor(\n private state$: MockState<T>,\n actionsObserver: ActionsSubject,\n reducerManager: ReducerManager,\n @Inject(INITIAL_STATE) private initialState: T,\n @Inject(MOCK_SELECTORS) mockSelectors: MockSelector[] = []\n ) {\n super(state$, actionsObserver, reducerManager);\n this.resetSelectors();\n this.setState(this.initialState);\n this.scannedActions$ = actionsObserver.asObservable();\n for (const mockSelector of mockSelectors) {\n this.overrideSelector(mockSelector.selector, mockSelector.value);\n }\n }\n\n setState(nextState: T): void {\n this.state$.next(nextState);\n this.lastState = nextState;\n }\n\n overrideSelector<\n Selector extends Memoized<Result>,\n Value extends Result,\n Result = Selector extends MemoizedSelector<any, infer T>\n ? T\n : Selector extends MemoizedSelectorWithProps<any, any, infer U>\n ? U\n : Value\n >(\n selector: Selector | string,\n value: Value\n ): OnlyMemoized<typeof selector, Result> {\n this.selectors.set(selector, value);\n\n const resultSelector: Memoized<Result> =\n typeof selector === 'string'\n ? createSelector(\n () => {},\n (): Result => value\n )\n : selector;\n\n resultSelector.setResult(value);\n\n return resultSelector as OnlyMemoized<typeof selector, Result>;\n }\n\n resetSelectors() {\n for (const selector of this.selectors.keys()) {\n if (typeof selector !== 'string') {\n selector.release();\n selector.clearResult();\n }\n }\n\n this.selectors.clear();\n }\n\n select(selector: any, prop?: any) {\n if (typeof selector === 'string' && this.selectors.has(selector)) {\n return new BehaviorSubject<any>(\n this.selectors.get(selector)\n ).asObservable();\n }\n\n return super.select(selector, prop);\n }\n\n addReducer() {\n /* noop */\n }\n\n removeReducer() {\n /* noop */\n }\n\n /**\n * Refreshes the existing state.\n */\n refreshState() {\n if (this.lastState) this.setState({ ...this.lastState });\n }\n}\n","import { Injectable } from '@angular/core';\nimport { BehaviorSubject } from 'rxjs';\nimport { ActionReducer } from '@ngrx/store';\n\n@Injectable()\nexport class MockReducerManager extends BehaviorSubject<\n ActionReducer<any, any>\n> {\n constructor() {\n super(() => undefined);\n }\n\n addFeature(feature: any) {\n /* noop */\n }\n\n addFeatures(feature: any) {\n /* noop */\n }\n\n removeFeature(feature: any) {\n /* noop */\n }\n\n removeFeatures(features: any) {\n /* noop */\n }\n\n addReducer(key: any, reducer: any) {\n /* noop */\n }\n\n addReducers(reducers: any) {\n /* noop */\n }\n\n removeReducer(featureKey: any) {\n /* noop */\n }\n\n removeReducers(featureKeys: any) {\n /* noop */\n }\n}\n","import {\n ExistingProvider,\n FactoryProvider,\n Injector,\n ValueProvider,\n} from '@angular/core';\nimport { MockState } from './mock_state';\nimport {\n ActionsSubject,\n INITIAL_STATE,\n ReducerManager,\n StateObservable,\n Store,\n setNgrxMockEnvironment,\n} from '@ngrx/store';\nimport { MockStore } from './mock_store';\nimport { MockReducerManager } from './mock_reducer_manager';\nimport { MockSelector } from './mock_selector';\nimport { MOCK_SELECTORS } from './tokens';\n\nexport interface MockStoreConfig<T> {\n initialState?: T;\n selectors?: MockSelector[];\n}\n\n/**\n * @description\n * Creates mock store providers.\n *\n * @param config `MockStoreConfig<T>` to provide the values for `INITIAL_STATE` and `MOCK_SELECTORS` tokens.\n * By default, `initialState` and `selectors` are not defined.\n * @returns Mock store providers that can be used with both `TestBed.configureTestingModule` and `Injector.create`.\n *\n * @usageNotes\n *\n * **With `TestBed.configureTestingModule`**\n *\n * ```typescript\n * describe('Books Component', () => {\n * let store: MockStore;\n *\n * beforeEach(() => {\n * TestBed.configureTestingModule({\n * providers: [\n * provideMockStore({\n * initialState: { books: { entities: [] } },\n * selectors: [\n * { selector: selectAllBooks, value: ['Book 1', 'Book 2'] },\n * { selector: selectVisibleBooks, value: ['Book 1'] },\n * ],\n * }),\n * ],\n * });\n *\n * store = TestBed.inject(MockStore);\n * });\n * });\n * ```\n *\n * **With `Injector.create`**\n *\n * ```typescript\n * describe('Counter Component', () => {\n * let injector: Injector;\n * let store: MockStore;\n *\n * beforeEach(() => {\n * injector = Injector.create({\n * providers: [\n * provideMockStore({ initialState: { counter: 0 } }),\n * ],\n * });\n * store = injector.get(MockStore);\n * });\n * });\n * ```\n */\nexport function provideMockStore<T = any>(\n config: MockStoreConfig<T> = {}\n): (ValueProvider | ExistingProvider | FactoryProvider)[] {\n setNgrxMockEnvironment(true);\n return [\n {\n provide: ActionsSubject,\n useFactory: () => new ActionsSubject(),\n deps: [],\n },\n { provide: MockState, useFactory: () => new MockState<T>(), deps: [] },\n {\n provide: MockReducerManager,\n useFactory: () => new MockReducerManager(),\n deps: [],\n },\n { provide: INITIAL_STATE, useValue: config.initialState || {} },\n { provide: MOCK_SELECTORS, useValue: config.selectors },\n { provide: StateObservable, useExisting: MockState },\n { provide: ReducerManager, useExisting: MockReducerManager },\n {\n provide: MockStore,\n useFactory: mockStoreFactory,\n deps: [\n MockState,\n ActionsSubject,\n ReducerManager,\n INITIAL_STATE,\n MOCK_SELECTORS,\n ],\n },\n { provide: Store, useExisting: MockStore },\n ];\n}\n\nfunction mockStoreFactory<T>(\n mockState: MockState<T>,\n actionsSubject: ActionsSubject,\n reducerManager: ReducerManager,\n initialState: T,\n mockSelectors: MockSelector[]\n): MockStore<T> {\n return new MockStore(\n mockState,\n actionsSubject,\n reducerManager,\n initialState,\n mockSelectors\n );\n}\n\n/**\n * @description\n * Creates mock store with all necessary dependencies outside of the `TestBed`.\n *\n * @param config `MockStoreConfig<T>` to provide the values for `INITIAL_STATE` and `MOCK_SELECTORS` tokens.\n * By default, `initialState` and `selectors` are not defined.\n * @returns `MockStore<T>`\n *\n * @usageNotes\n *\n * ```typescript\n * describe('Books Effects', () => {\n * let store: MockStore;\n *\n * beforeEach(() => {\n * store = getMockStore({\n * initialState: { books: { entities: ['Book 1', 'Book 2', 'Book 3'] } },\n * selectors: [\n * { selector: selectAllBooks, value: ['Book 1', 'Book 2'] },\n * { selector: selectVisibleBooks, value: ['Book 1'] },\n * ],\n * });\n * });\n * });\n * ```\n */\nexport function getMockStore<T>(config: MockStoreConfig<T> = {}): MockStore<T> {\n const injector = Injector.create({ providers: provideMockStore(config) });\n return injector.get(MockStore);\n}\n\nexport { MockReducerManager } from './mock_reducer_manager';\nexport { MockState } from './mock_state';\nexport { MockStore } from './mock_store';\nexport { MockSelector } from './mock_selector';\n","/**\n *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