@angular-redux/store
Version:
Angular bindings for Redux
298 lines (290 loc) • 10.7 kB
JavaScript
import { Injectable, NgModule } from '@angular/core';
import { DevToolsExtension, NgRedux } from '@angular-redux/store';
import { __extends, __spread, __read } from 'tslib';
import { ReplaySubject } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var MockDevToolsExtension = /** @class */ (function (_super) {
__extends(MockDevToolsExtension, _super);
function MockDevToolsExtension() {
return _super !== null && _super.apply(this, arguments) || this;
}
MockDevToolsExtension.decorators = [
{ type: Injectable }
];
return MockDevToolsExtension;
}(DevToolsExtension));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @hidden
* @template State
*/
var /**
* @hidden
* @template State
*/
MockObservableStore = /** @class */ (function () {
function MockObservableStore() {
var _this = this;
this.selections = {};
this.subStores = {};
this.getSelectorStub = function (selector, comparator) {
return _this.initSelectorStub(selector, comparator).subject;
};
this.reset = function () {
Object.keys(_this.subStores).forEach(function (k) { return _this.subStores[k].reset(); });
_this.selections = {};
_this.subStores = {};
};
this.dispatch = function (action) { return action; };
this.replaceReducer = function () { return null; };
this.getState = function () { return ({}); };
this.subscribe = function () { return function () { return null; }; };
this.select = function (selector, comparator) {
/** @type {?} */
var stub = _this.initSelectorStub(selector, comparator);
return stub.comparator
? stub.subject.pipe(distinctUntilChanged(stub.comparator))
: stub.subject;
};
this.configureSubStore = function (basePath, _) { return _this.initSubStore(basePath); };
this.getSubStore = function () {
var pathSelectors = [];
for (var _i = 0; _i < arguments.length; _i++) {
pathSelectors[_i] = arguments[_i];
}
var _a;
var _b = __read(pathSelectors), first = _b[0], rest = _b.slice(1);
return (/** @type {?} */ ((first
? (_a = _this.initSubStore(first)).getSubStore.apply(_a, __spread(rest)) : _this)));
};
}
/**
* @private
* @template SubState
* @param {?} basePath
* @return {?}
*/
MockObservableStore.prototype.initSubStore = /**
* @private
* @template SubState
* @param {?} basePath
* @return {?}
*/
function (basePath) {
/** @type {?} */
var result = this.subStores[JSON.stringify(basePath)] ||
new MockObservableStore();
this.subStores[JSON.stringify(basePath)] = result;
return result;
};
/**
* @private
* @template SelectedState
* @param {?=} selector
* @param {?=} comparator
* @return {?}
*/
MockObservableStore.prototype.initSelectorStub = /**
* @private
* @template SelectedState
* @param {?=} selector
* @param {?=} comparator
* @return {?}
*/
function (selector, comparator) {
/** @type {?} */
var key = selector ? selector.toString() : '';
/** @type {?} */
var record = this.selections[key] || {
subject: new ReplaySubject(),
comparator: comparator,
};
this.selections[key] = record;
return record;
};
return MockObservableStore;
}());
/**
* @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
*/
var MockNgRedux = /** @class */ (function (_super) {
__extends(MockNgRedux, _super);
/** @hidden */
function MockNgRedux() {
var _this = _super.call(this) || this;
//
_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 = function (_) { return undefined; };
_this.configureStore = function (_, __, ___, ____) { return undefined; };
// This hooks the mock up to @select.
NgRedux.instance = (/** @type {?} */ (_this));
return _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.
*/
/**
* 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 {?}
*/
MockNgRedux.getSelectorStub = /**
* 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 {?}
*/
function (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.
* @param pathSelectors
*/
/**
* 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 {?}
*/
MockNgRedux.getSubStore = /**
* 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 {?}
*/
function () {
var pathSelectors = [];
for (var _i = 0; _i < arguments.length; _i++) {
pathSelectors[_i] = arguments[_i];
}
var _a;
return pathSelectors.length
? (_a = MockNgRedux.getInstance().mockRootStore).getSubStore.apply(_a, __spread(pathSelectors)) : MockNgRedux.getInstance().mockRootStore;
};
/**
* Reset all previously configured stubs.
*/
/**
* Reset all previously configured stubs.
* @return {?}
*/
MockNgRedux.reset = /**
* Reset all previously configured stubs.
* @return {?}
*/
function () {
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.
*/
/**
* Gets the singleton MockNgRedux instance. Useful for cases where your
* tests need to spy on store methods, for example.
* @return {?}
*/
MockNgRedux.getInstance = /**
* Gets the singleton MockNgRedux instance. Useful for cases where your
* tests need to spy on store methods, for example.
* @return {?}
*/
function () {
MockNgRedux.mockInstance = MockNgRedux.mockInstance || new MockNgRedux();
return MockNgRedux.mockInstance;
};
/**
* @deprecated Use MockNgRedux.getInstance() instead.
*/
MockNgRedux.mockInstance = undefined;
return MockNgRedux;
}(NgRedux));
/**
* @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 {?} */
var mockNgRedux = MockNgRedux.getInstance();
/**
* @hidden
* @return {?}
*/
function _mockNgReduxFactory() {
return mockNgRedux;
}
var NgReduxTestingModule = /** @class */ (function () {
function NgReduxTestingModule() {
}
NgReduxTestingModule.decorators = [
{ type: NgModule, args: [{
imports: [],
providers: [
{ provide: NgRedux, useFactory: _mockNgReduxFactory },
{ provide: DevToolsExtension, useClass: MockDevToolsExtension },
],
},] }
];
return NgReduxTestingModule;
}());
/**
* @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