UNPKG

@angular-redux/store

Version:
363 lines (350 loc) 14.9 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular-redux/store'), require('rxjs'), require('rxjs/operators')) : typeof define === 'function' && define.amd ? define('@angular-redux/store/testing', ['exports', '@angular/core', '@angular-redux/store', 'rxjs', 'rxjs/operators'], factory) : (factory((global['angular-redux'] = global['angular-redux'] || {}, global['angular-redux'].store = global['angular-redux'].store || {}, global['angular-redux'].store.testing = {}),global.ng.core,global['angular-redux'].store,global.rxjs,global.rxjs.operators)); }(this, (function (exports,core,store,rxjs,operators) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } /** * @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: core.Injectable } ]; return MockDevToolsExtension; }(store.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(operators.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 rxjs.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. store.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(); store.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; }(store.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: core.NgModule, args: [{ imports: [], providers: [ { provide: store.NgRedux, useFactory: _mockNgReduxFactory }, { provide: store.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 */ exports.NgReduxTestingModule = NgReduxTestingModule; exports.MockDevToolsExtension = MockDevToolsExtension; exports.MockNgRedux = MockNgRedux; exports.MockObservableStore = MockObservableStore; exports.ɵa = _mockNgReduxFactory; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=angular-redux-store-testing.umd.js.map