@angular-redux/store
Version:
Angular bindings for Redux
205 lines (197 loc) • 6.87 kB
JavaScript
import { Injectable, NgModule } from '@angular/core';
import { DevToolsExtension, NgRedux } from '@angular-redux/store';
import { ReplaySubject } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class MockDevToolsExtension extends DevToolsExtension {
}
MockDevToolsExtension.decorators = [
{ type: Injectable }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @hidden
* @template State
*/
class MockObservableStore {
constructor() {
this.selections = {};
this.subStores = {};
this.getSelectorStub = (selector, comparator) => this.initSelectorStub(selector, comparator).subject;
this.reset = () => {
Object.keys(this.subStores).forEach(k => this.subStores[k].reset());
this.selections = {};
this.subStores = {};
};
this.dispatch = action => action;
this.replaceReducer = () => null;
this.getState = () => ({});
this.subscribe = () => () => null;
this.select = (selector, comparator) => {
/** @type {?} */
const stub = this.initSelectorStub(selector, comparator);
return stub.comparator
? stub.subject.pipe(distinctUntilChanged(stub.comparator))
: stub.subject;
};
this.configureSubStore = (basePath, _) => this.initSubStore(basePath);
this.getSubStore = (...pathSelectors) => {
const [first, ...rest] = pathSelectors;
return (/** @type {?} */ ((first
? this.initSubStore(first).getSubStore(...rest)
: this)));
};
}
/**
* @private
* @template SubState
* @param {?} basePath
* @return {?}
*/
initSubStore(basePath) {
/** @type {?} */
const result = this.subStores[JSON.stringify(basePath)] ||
new MockObservableStore();
this.subStores[JSON.stringify(basePath)] = result;
return result;
}
/**
* @private
* @template SelectedState
* @param {?=} selector
* @param {?=} comparator
* @return {?}
*/
initSelectorStub(selector, comparator) {
/** @type {?} */
const key = selector ? selector.toString() : '';
/** @type {?} */
const record = this.selections[key] || {
subject: new ReplaySubject(),
comparator,
};
this.selections[key] = record;
return record;
}
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* Convenience mock to make it easier to control selector
* behaviour in unit tests.
* @template T
*/
class MockNgRedux extends NgRedux {
/**
* @hidden
*/
constructor() {
super();
//
this.mockRootStore = new MockObservableStore();
this.configureSubStore = (/** @type {?} */ (this.mockRootStore.configureSubStore));
this.dispatch = (/** @type {?} */ (this.mockRootStore.dispatch));
this.getState = (/** @type {?} */ (this.mockRootStore.getState));
this.subscribe = this.mockRootStore.subscribe;
this.replaceReducer = this.mockRootStore.replaceReducer;
this.select = this.mockRootStore.select;
this.provideStore = (_) => undefined;
this.configureStore = (_, __, ___, ____) => undefined;
// This hooks the mock up to @select.
NgRedux.instance = (/** @type {?} */ (this));
}
/**
* Returns a subject that's connected to any observable returned by the
* given selector. You can use this subject to pump values into your
* components or services under test; when they call .select or \@select
* in the context of a unit test, MockNgRedux will give them the values
* you pushed onto your stub.
* @template R, S
* @param {?=} selector
* @param {?=} comparator
* @return {?}
*/
static getSelectorStub(selector, comparator) {
return MockNgRedux.getInstance().mockRootStore.getSelectorStub(selector, comparator);
}
/**
* Returns a mock substore that allows you to set up selectorStubs for
* any 'fractal' stores your app creates with NgRedux.configureSubStore.
*
* If your app creates deeply nested substores from other substores,
* pass the chain of pathSelectors in as ordered arguments to mock
* the nested substores out.
* @template S
* @param {...?} pathSelectors
* @return {?}
*/
static getSubStore(...pathSelectors) {
return pathSelectors.length
? MockNgRedux.getInstance().mockRootStore.getSubStore(...pathSelectors)
: MockNgRedux.getInstance().mockRootStore;
}
/**
* Reset all previously configured stubs.
* @return {?}
*/
static reset() {
MockNgRedux.getInstance().mockRootStore.reset();
NgRedux.instance = (/** @type {?} */ (MockNgRedux.mockInstance));
}
/**
* Gets the singleton MockNgRedux instance. Useful for cases where your
* tests need to spy on store methods, for example.
* @return {?}
*/
static getInstance() {
MockNgRedux.mockInstance = MockNgRedux.mockInstance || new MockNgRedux();
return MockNgRedux.mockInstance;
}
}
/**
* @deprecated Use MockNgRedux.getInstance() instead.
*/
MockNgRedux.mockInstance = undefined;
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
// Needs to be initialized early so @select's use the mocked version too.
/** @type {?} */
const mockNgRedux = MockNgRedux.getInstance();
/**
* @hidden
* @return {?}
*/
function _mockNgReduxFactory() {
return mockNgRedux;
}
class NgReduxTestingModule {
}
NgReduxTestingModule.decorators = [
{ type: NgModule, args: [{
imports: [],
providers: [
{ provide: NgRedux, useFactory: _mockNgReduxFactory },
{ provide: DevToolsExtension, useClass: MockDevToolsExtension },
],
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { NgReduxTestingModule, MockDevToolsExtension, MockNgRedux, MockObservableStore, _mockNgReduxFactory as ɵa };
//# sourceMappingURL=angular-redux-store-testing.js.map