UNPKG

@rxx/core

Version:

React MVI micro framework.

189 lines (188 loc) 6.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var rxjs_1 = require("rxjs"); var chai_1 = require("chai"); var provisioning_1 = require("../provisioning"); var handler_1 = require("../handler/handler"); var state_handler_1 = require("../handler/state-handler"); var store_1 = require("../store/store"); var intent_1 = require("../intent/intent"); var operators_1 = require("rxjs/operators"); var factory_1 = require("../factory"); var TestStore = (function () { function TestStore() { } TestStore.prototype.initialize = function () { return { view: { test: 1, }, }; }; TestStore = tslib_1.__decorate([ store_1.store ], TestStore); return TestStore; }()); var TestIntent = (function () { function TestIntent() { } TestIntent.prototype.getIntent = function () { return this.intent; }; TestIntent.prototype.getHandler = function () { return this.testHandler; }; TestIntent = tslib_1.__decorate([ intent_1.intent ], TestIntent); return TestIntent; }()); var TestParentStore = (function () { function TestParentStore() { } TestParentStore.prototype.initialize = function () { return { view: { parent: 1, }, }; }; TestParentStore = tslib_1.__decorate([ store_1.store ], TestParentStore); return TestParentStore; }()); var TestParentIntent = (function () { function TestParentIntent() { } TestParentIntent.prototype.getIntent = function () { return this.intent; }; TestParentIntent.prototype.getHandler = function () { return this.testHandler; }; TestParentIntent = tslib_1.__decorate([ intent_1.intent ], TestParentIntent); return TestParentIntent; }()); var TestGrandParentStore = (function () { function TestGrandParentStore() { } TestGrandParentStore.prototype.initialize = function () { return { view: { grandParent: 1, }, }; }; TestGrandParentStore = tslib_1.__decorate([ store_1.store ], TestGrandParentStore); return TestGrandParentStore; }()); var TestGrandParentIntent = (function () { function TestGrandParentIntent() { } TestGrandParentIntent.prototype.getIntent = function () { return this.intent; }; TestGrandParentIntent.prototype.getHandler = function () { return this.testHandler; }; TestGrandParentIntent = tslib_1.__decorate([ intent_1.intent ], TestGrandParentIntent); return TestGrandParentIntent; }()); var TestHandler = (function (_super) { tslib_1.__extends(TestHandler, _super); function TestHandler() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.subscribed = false; return _this; } TestHandler.prototype.subscribe = function (a) { this.subscribed = true; return new rxjs_1.Subscription(); }; TestHandler.prototype.isSubscribed = function () { return this.subscribed; }; TestHandler.prototype.push = function (k, a) { return new Promise(function (r) { }); }; TestHandler.prototype.clone = function () { return new TestHandler(); }; return TestHandler; }(state_handler_1.StateHandler)); describe('Provisioning', function () { var context = { provisioning: (function () { var p = new provisioning_1.Provisioning('', { provisioning: (function () { var p = new provisioning_1.Provisioning('', {}, { intent: TestGrandParentIntent }, [TestGrandParentStore], undefined, {}); p.prepare(); return p; })(), __intent: undefined, __subject: undefined, }, { intent: TestParentIntent }, [TestParentStore], undefined, {}); p.prepare(); return p; })(), __intent: undefined, __subject: undefined, }; var testHandler = new TestHandler(); beforeAll(function () { state_handler_1.registerHandlers({ testHandler: testHandler, }); }); describe('#prepare', function () { it('should connect store and intent and handler', function () { var p = new provisioning_1.Provisioning('', context, { intent: TestIntent }, [TestStore], undefined, {}); p.prepare(); chai_1.expect(p.getIntentInstance().intent).to.be.instanceof(TestIntent); chai_1.expect(p.getIntentInstance().intent.getIntent()).to.be.instanceof(handler_1.HandlerResponse); chai_1.expect(p.getIntentInstance().intent.getHandler()).to.be.instanceof(handler_1.HandlerResponse); chai_1.expect(p.getStores()[0]).to.be.instanceof(TestStore); chai_1.expect(p.getStores()[0].intent).to.be.instanceof(TestIntent); chai_1.expect(p.getState()).to.be.deep.eq({ view: { test: 1, parent: 1, grandParent: 1 }, }); chai_1.expect(p.getHandlers().testHandler.isSubscribed()).to.be.eq(true); }); it('should accept intent map', function () { var p = new provisioning_1.Provisioning('', context, { test: TestIntent }, [TestStore], undefined, {}); p.prepare(); chai_1.expect(p.getIntentInstance().test).to.be.instanceof(TestIntent); chai_1.expect(p.getIntentInstance().test.getIntent()).to.be.instanceof(handler_1.HandlerResponse); chai_1.expect(p.getIntentInstance().test.getHandler()).to.be.instanceof(handler_1.HandlerResponse); chai_1.expect(p.getStores()[0]).to.be.instanceof(TestStore); chai_1.expect(p.getStores()[0].test).to.be.instanceof(TestIntent); chai_1.expect(p.getState()).to.be.deep.eq({ view: { test: 1, parent: 1, grandParent: 1 }, }); chai_1.expect(p.getHandlers().testHandler.isSubscribed()).to.be.eq(true); }); it('should accept state factories', function () { var p = new provisioning_1.Provisioning('', context, {}, [], factory_1.makeApp({ test: function (observable, state) { return { view: observable.pipe(operators_1.startWith(state)), }; }, })({ test: 1 }), {}); p.prepare(); chai_1.expect(p.getState()).to.be.deep.eq({ view: { test: 1, parent: 1, grandParent: 1 }, }); chai_1.expect(p.getHandlers().testHandler.isSubscribed()).to.be.eq(true); }); }); });