UNPKG

@triviality/core

Version:
269 lines 12.7 kB
"use strict"; 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); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var __1 = __importStar(require("../..")); var invokeFeatureFactory_1 = require("../../invokeFeatureFactory"); it('Can merge features', function () { return __awaiter(void 0, void 0, void 0, function () { var container, context, myFeature, services; return __generator(this, function (_a) { switch (_a.label) { case 0: container = new __1.ServiceFunctionReferenceContainer(); context = __1.createFeatureFactoryContext({ container: container, invoke: invokeFeatureFactory_1.invokeFeatureFactory }); myFeature = function () { return ({ bar: function () { return 'bar'; }, foo: function () { return 1; }, }); }; services = context.merge(myFeature).services('bar', 'foo'); return [4 /*yield*/, container.build()]; case 1: _a.sent(); expect(services.foo()).toEqual(1); expect(services.bar()).toEqual('bar'); return [2 /*return*/]; } }); }); }); it('Merged services can use default services', function () { return __awaiter(void 0, void 0, void 0, function () { var spy, myFeature; return __generator(this, function (_a) { switch (_a.label) { case 0: spy = jest.fn(); myFeature = function (_a) { var setupCallbacks = _a.registers.setupCallbacks; return (__assign({}, setupCallbacks(function () { return spy; }))); }; return [4 /*yield*/, __1.default().add(function (_a) { var merge = _a.merge; merge(myFeature); return {}; }).build()]; case 1: _a.sent(); expect(spy).toBeCalled(); return [2 /*return*/]; } }); }); }); it('Can merge multiple features', function () { return __awaiter(void 0, void 0, void 0, function () { var userFeature, buildingFeature, container, context, services; return __generator(this, function (_a) { switch (_a.label) { case 0: userFeature = function () { return ({ users: function () { return 3; }, }); }; buildingFeature = function () { return ({ buildings: function () { return 10; }, }); }; container = new __1.ServiceFunctionReferenceContainer(); context = __1.createFeatureFactoryContext({ container: container, invoke: invokeFeatureFactory_1.invokeFeatureFactory }); services = context .merge(userFeature) .with(buildingFeature) .services('users', 'buildings'); return [4 /*yield*/, container.build()]; case 1: _a.sent(); expect(services.users()).toEqual(3); expect(services.buildings()).toEqual(10); return [2 /*return*/]; } }); }); }); it('Can merge multiple times', function () { return __awaiter(void 0, void 0, void 0, function () { var a, userFeature, i, buildingFeature, myFeature, _a, b1, b2, u1, u2; return __generator(this, function (_b) { switch (_b.label) { case 0: a = 20; userFeature = function () { return ({ users: function () { a *= 2; return { age: a }; }, }); }; i = 0; buildingFeature = function () { return ({ buildings: function () { i += 1; return ({ size: i }); }, }); }; myFeature = function (_a) { var merge = _a.merge; var withUserFeature = merge(userFeature); var _b = withUserFeature .with(buildingFeature) .services('buildings', 'users'), buildings1 = _b.buildings, users1 = _b.users; var _c = withUserFeature .with(buildingFeature) .services('buildings', 'users'), buildings2 = _c.buildings, users2 = _c.users; return { buildings1: buildings1, buildings2: buildings2, users1: users1, users2: users2, }; }; return [4 /*yield*/, __1.default().add(myFeature).build()]; case 1: _a = _b.sent(), b1 = _a.buildings1, b2 = _a.buildings2, u1 = _a.users1, u2 = _a.users2; expect(b1).toEqual({ size: 1 }); expect(b2).toEqual({ size: 2 }); expect(u1).toEqual({ age: 40 }); expect(u2).toEqual({ age: 40 }); return [2 /*return*/]; } }); }); }); it('Can merge multiple services', function () { return __awaiter(void 0, void 0, void 0, function () { var f1, f2, f3, myFeature, _a, one, two, three; return __generator(this, function (_b) { switch (_b.label) { case 0: f1 = function () { return ({ one: function () { return 1; } }); }; f2 = function () { return ({ two: function () { return 2; } }); }; f3 = function () { return ({ three: function () { return 3; } }); }; myFeature = function (_a) { var merge = _a.merge; return merge(f1).with(f2).with(f3).all(); }; return [4 /*yield*/, __1.default() .add(myFeature) .build()]; case 1: _a = _b.sent(), one = _a.one, two = _a.two, three = _a.three; expect([one, two, three]).toEqual([1, 2, 3]); return [2 /*return*/]; } }); }); }); it('Can override merged services', function () { return __awaiter(void 0, void 0, void 0, function () { var orginalFeature, f2, f3, myFeature, myFeatureOverride, _a, one, two, three; return __generator(this, function (_b) { switch (_b.label) { case 0: orginalFeature = function () { return ({ one: function () { return 1; } }); }; f2 = function (_a) { var o = _a.one; return ({ two: function () { return o() + 2; } }); }; f3 = function (_a) { var o = _a.one; return ({ three: function () { return o() + 3; } }); }; myFeature = function (_a) { var merge = _a.merge; return merge(orginalFeature).with(f2).with(f3).all(); }; myFeatureOverride = function (_a) { var o = _a.override.one; return __assign({}, o(function () { return 10; })); }; return [4 /*yield*/, __1.default() .add(myFeature) .add(myFeatureOverride) .build()]; case 1: _a = _b.sent(), one = _a.one, two = _a.two, three = _a.three; expect([one, two, three]).toEqual([10, 12, 13]); return [2 /*return*/]; } }); }); }); it('Can directly fetch all new services', function () { return __awaiter(void 0, void 0, void 0, function () { var userFeature, johnUserFeature, JaneUserFeature, myFeature, names; return __generator(this, function (_a) { switch (_a.label) { case 0: userFeature = function (_a) { var registerSet = _a.registerSet; return ({ users: registerSet(), }); }; johnUserFeature = function (_a) { var users = _a.registers.users; return (__assign({}, users(function () { return ({ name: 'John' }); }))); }; JaneUserFeature = function (_a) { var users = _a.registers.users; return (__assign({}, users(function () { return ({ name: 'Jane' }); }))); }; myFeature = function (_a) { var merge = _a.merge; var users = merge(userFeature).with(johnUserFeature).with(JaneUserFeature).services('users').users; return { users: users, names: function () { return users().map(function (_a) { var name = _a.name; return name; }); }, }; }; return [4 /*yield*/, __1.default() .add(myFeature) .build()]; case 1: names = (_a.sent()).names; expect(names).toEqual(['John', 'Jane']); return [2 /*return*/]; } }); }); }); //# sourceMappingURL=MergeFeatureContext.test.js.map