inversify
Version:
A powerful and lightweight inversion of control container for JavaScript and Node.js apps powered by TypeScript.
66 lines • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const vitest_1 = require("vitest");
const __1 = require("../..");
(0, vitest_1.describe)('Issue 1297', () => {
(0, vitest_1.it)('should call onActivation once if the service is a constant value binding', () => {
const container = new __1.Container();
const onActivationHandlerMock = vitest_1.vitest
.fn()
.mockImplementation((_, value) => value);
container
.bind('message')
.toConstantValue('Hello world')
.onActivation(onActivationHandlerMock);
container.get('message');
container.get('message');
(0, vitest_1.expect)(onActivationHandlerMock).toHaveBeenCalledTimes(1);
});
(0, vitest_1.it)('should call onActivation once if the service is a factory binding', () => {
let Katana = class Katana {
hit() {
return 'cut!';
}
};
Katana = tslib_1.__decorate([
(0, __1.injectable)()
], Katana);
const container = new __1.Container();
const onActivationHandlerMock = vitest_1.vitest
.fn()
.mockImplementation((_, instance) => instance);
container.bind('Katana').to(Katana);
container
.bind('Factory<Katana>')
.toFactory((context) => () => context.get('Katana'))
.onActivation(onActivationHandlerMock);
container.get('Factory<Katana>');
container.get('Factory<Katana>');
(0, vitest_1.expect)(onActivationHandlerMock).toHaveBeenCalledTimes(1);
});
(0, vitest_1.it)('should call onActivation once if the service is a provider binding', () => {
let Katana = class Katana {
hit() {
return 'cut!';
}
};
Katana = tslib_1.__decorate([
(0, __1.injectable)()
], Katana);
const container = new __1.Container();
const onActivationHandlerMock = vitest_1.vitest.fn().mockImplementation(
// eslint-disable-next-line @typescript-eslint/no-deprecated
(_, instance) => instance);
container
// eslint-disable-next-line @typescript-eslint/no-deprecated
.bind('Provider<Katana>')
// eslint-disable-next-line @typescript-eslint/no-deprecated
.toProvider((_context) => async () => Promise.resolve(new Katana()))
.onActivation(onActivationHandlerMock);
container.get('Provider<Katana>');
container.get('Provider<Katana>');
(0, vitest_1.expect)(onActivationHandlerMock).toHaveBeenCalledTimes(1);
});
});
//# sourceMappingURL=issue_1297.test.js.map