power-di
Version:
A lightweight Dependency Injection library. Using es6 and other features, remove unnecessary concepts, easy and convenient to use.
555 lines • 22.2 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
var ava_1 = require("ava");
var React = require("react");
var react_test_renderer_1 = require("react-test-renderer");
var IocContext_1 = require("../lib/IocContext");
var react_1 = require("../lib/react");
var lib_1 = require("../lib");
var react_2 = require("../react");
(0, ava_1.default)('only component.', function (t) {
var context = IocContext_1.IocContext.DefaultInstance;
var NRServiceDI = /** @class */ (function () {
function NRServiceDI() {
}
return NRServiceDI;
}());
context.register(NRServiceDI);
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.componentDidMount = function () {
t.true(this.service instanceof NRServiceDI);
};
TestComponent.prototype.render = function () {
t.true(this.service instanceof NRServiceDI);
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRServiceDI)
], TestComponent.prototype, "service", void 0);
return TestComponent;
}(react_1.Component));
(0, react_test_renderer_1.create)(React.createElement(TestComponent, null));
});
(0, ava_1.default)('IocProvider.', function (t) {
var context = IocContext_1.IocContext.DefaultInstance;
var NRServiceDI = /** @class */ (function () {
function NRServiceDI() {
}
return NRServiceDI;
}());
context.register(NRServiceDI);
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.componentDidMount = function () {
t.true(this.service instanceof NRServiceDI);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRServiceDI)
], TestComponent.prototype, "service", void 0);
return TestComponent;
}(react_1.Component));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, null,
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('IocProvider with context.', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
}
return NRService;
}());
context.register(NRService);
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.componentDidMount = function () {
t.true(this.service instanceof NRService);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRService)
], TestComponent.prototype, "service", void 0);
return TestComponent;
}(react_1.PureComponent));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('has componentWillMount.', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
}
return NRService;
}());
context.register(NRService);
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.UNSAFE_componentWillMount = function () {
t.true(this.service instanceof NRService);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRService)
], TestComponent.prototype, "service", void 0);
return TestComponent;
}(react_1.Component));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('has componentWillMount, PureComponent.', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
}
return NRService;
}());
context.register(NRService);
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.UNSAFE_componentWillMount = function () {
t.true(this.service instanceof NRService);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRService)
], TestComponent.prototype, "service", void 0);
return TestComponent;
}(react_1.PureComponent));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('postConstruct.', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
}
return NRService;
}());
context.register(NRService);
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.init = function () {
t.true(this.service instanceof NRService);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRService)
], TestComponent.prototype, "service", void 0);
__decorate([
(0, lib_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestComponent.prototype, "init", null);
return TestComponent;
}(react_1.Component));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('consumer.', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
}
return NRService;
}());
context.register(NRService);
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.init = function () {
t.true(this.service instanceof NRService);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRService)
], TestComponent.prototype, "service", void 0);
__decorate([
(0, lib_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestComponent.prototype, "init", null);
TestComponent = __decorate([
(0, react_2.iocConsumer)()
], TestComponent);
return TestComponent;
}(React.Component));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('consumer, PureComponent.', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
}
return NRService;
}());
context.register(NRService);
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.init = function () {
t.true(this.service instanceof NRService);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRService)
], TestComponent.prototype, "service", void 0);
__decorate([
(0, lib_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestComponent.prototype, "init", null);
TestComponent = __decorate([
(0, react_2.iocConsumer)({ pureComponent: true })
], TestComponent);
return TestComponent;
}(React.Component));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('consumer, manual extends.', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
}
return NRService;
}());
context.register(NRService);
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.init = function () {
t.fail();
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRService)
], TestComponent.prototype, "service", void 0);
__decorate([
(0, lib_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestComponent.prototype, "init", null);
TestComponent = __decorate([
(0, react_2.iocConsumer)({ manualExtendsBaseClass: true })
], TestComponent);
return TestComponent;
}(React.Component));
var TestBComponent = /** @class */ (function (_super) {
__extends(TestBComponent, _super);
function TestBComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestBComponent.prototype.init = function () {
t.true(this.service instanceof NRService);
};
TestBComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRService)
], TestBComponent.prototype, "service", void 0);
__decorate([
(0, lib_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestBComponent.prototype, "init", null);
TestBComponent = __decorate([
(0, react_2.iocConsumer)()
], TestBComponent);
return TestBComponent;
}(React.Component));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null),
React.createElement(TestBComponent, null)));
});
(0, ava_1.default)('consumer, manual extends BaseConsumerComponent.', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
}
return NRService;
}());
context.register(NRService);
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.init = function () {
t.true(this.service instanceof NRService);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRService)
], TestComponent.prototype, "service", void 0);
__decorate([
(0, lib_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestComponent.prototype, "init", null);
TestComponent = __decorate([
(0, react_2.iocConsumer)({ manualExtendsBaseClass: true })
], TestComponent);
return TestComponent;
}(react_1.BaseConsumerComponent));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('has createInstanceHook', function (t) {
var context = new IocContext_1.IocContext({
createInstanceHook: function (inst, ioc) {
inst.x = 'test';
return inst;
},
});
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.componentDidMount = function () {
t.deepEqual(this.x, 'test');
};
TestComponent.prototype.render = function () {
return null;
};
return TestComponent;
}(react_1.Component));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('use hooks', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
}
return NRService;
}());
context.register(NRService);
var TestComponent = function () {
t.true((0, react_1.useInstanceHook)(NRService) instanceof NRService);
return null;
};
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('use hooks, symbol', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
}
return NRService;
}());
var INRService = Symbol('INRService');
context.register(NRService, INRService);
var TestComponent = function () {
t.true((0, react_1.useInstanceHook)(INRService) instanceof NRService);
return null;
};
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('postConstruct, subclass', function (t) {
var context = new IocContext_1.IocContext();
var NRService = /** @class */ (function () {
function NRService() {
this.test = 2;
}
return NRService;
}());
context.register(NRService);
var Base = /** @class */ (function (_super) {
__extends(Base, _super);
function Base() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.test = 1;
return _this;
}
return Base;
}(react_1.Component));
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.init = function () {
t.deepEqual(this.test, 1);
t.deepEqual(this.service.test, 2);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.inject)(),
__metadata("design:type", NRService)
], TestComponent.prototype, "service", void 0);
__decorate([
(0, lib_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestComponent.prototype, "init", null);
return TestComponent;
}(Base));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('postConstruct, componentWillMount', function (t) {
var context = new IocContext_1.IocContext();
var Base = /** @class */ (function (_super) {
__extends(Base, _super);
function Base() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.test = 1;
return _this;
}
return Base;
}(react_1.Component));
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.componentWillMount = function () {
this.test++;
};
TestComponent.prototype.init = function () {
t.deepEqual(this.test, 1);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestComponent.prototype, "init", null);
return TestComponent;
}(Base));
(0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
});
(0, ava_1.default)('preDestroy, componentWillUnmount', function (t) {
var context = new IocContext_1.IocContext();
var Base = /** @class */ (function (_super) {
__extends(Base, _super);
function Base() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.test = 1;
return _this;
}
return Base;
}(react_1.Component));
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestComponent.prototype.componentWillUnmount = function () {
this.test++;
};
TestComponent.prototype.destroy = function () {
t.deepEqual(this.test, 1);
};
TestComponent.prototype.render = function () {
return null;
};
__decorate([
(0, lib_1.preDestroy)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestComponent.prototype, "destroy", null);
return TestComponent;
}(Base));
var c = (0, react_test_renderer_1.create)(React.createElement(react_1.IocProvider, { context: context },
React.createElement(TestComponent, null)));
c.unmount();
});
//# sourceMappingURL=react.js.map