UNPKG

@react-mvi/testing

Version:

Test module for React MVI.

110 lines (109 loc) 4.67 kB
"use strict"; /** * The MIT License (MIT) * Copyright (c) Taketoshi Aono * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * @fileoverview * @author Taketoshi Aono */ var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var sym = function (s) { return (typeof Symbol === 'function' ? Symbol(s) : "@@" + s); }; var STATE_SYM = sym('ReactMVIMockedIntentState'); var SUBJECT_SYM = sym('ReactMVIMockedIntentSubject'); var BUILTINS = (function () { var keys = Object.getOwnPropertyNames(Object.prototype); var result = Object.create(null); for (var i = 0, len = keys.length; i < len; i++) { result[keys[i]] = true; } result.constructor = true; return result; })(); var Mocker = /** @class */ (function () { function Mocker(intent, state) { var _this = this; this[SUBJECT_SYM] = {}; this[STATE_SYM] = state; var proto = intent; while (proto && proto !== Object.prototype) { Object.getOwnPropertyNames(proto).forEach(function (key) { if (_this[key] || BUILTINS[key]) { return; } var descriptor = Object.getOwnPropertyDescriptor(proto, key); if (descriptor) { if (typeof descriptor.get === 'function') { var clone = __assign({}, descriptor); clone.get = Mocker.proxify(_this, key); Object.defineProperty(_this, key, clone); } else if (typeof descriptor.value === 'function') { _this[key] = Mocker.proxify(_this, key); } } }); proto = proto.__proto__; } } Mocker.proxify = function (mocker, methodName) { mocker[SUBJECT_SYM][methodName] = new rxjs_1.Subject(); return function () { return mocker[SUBJECT_SYM][methodName].pipe(operators_1.share()); }; }; return Mocker; }()); exports.Mocker = Mocker; var MockManipulator = /** @class */ (function () { function MockManipulator(mocker) { this.mocker = mocker; } MockManipulator.prototype.send = function (name, data) { if (data === void 0) { data = {}; } MockManipulator.send(this.mocker, name, data); }; MockManipulator.prototype.sendWithoutState = function (name, data) { if (data === void 0) { data = {}; } MockManipulator.sendWithoutState(this.mocker, name, data); }; MockManipulator.send = function (mock, name, data) { if (data === void 0) { data = {}; } if (!mock[SUBJECT_SYM][name]) { throw new Error("$[name} is not valid property name."); } mock[SUBJECT_SYM][name].next({ data: data, state: mock[STATE_SYM] }); }; MockManipulator.sendWithoutState = function (mock, name, data) { if (data === void 0) { data = {}; } if (!mock[SUBJECT_SYM][name]) { throw new Error("$[name} is not valid property name."); } mock[SUBJECT_SYM][name].next(data); }; return MockManipulator; }()); exports.MockManipulator = MockManipulator;