Ghost/ghost/moleculer-service-from-class/test/moleculer-service-from-class.test.js
Fabien O'Carroll 2d520ec32f Used versions for service deps and instance (#63)
no-issue

This is a major change and will require that we explicitly define the
versions of the services which we depend on. This will make sure that
updated services will always be sure of what actions their dependents
will have.
2020-05-18 15:16:41 +02:00

92 lines
2.9 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const moleculer = require('moleculer');
const createMoleculerServiceFromClass = require('../');
const hasOwnProperty = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);
describe('MoleculerServiceFromClass', function () {
it('Exposes name & methods as actions (excluding private & constructor) as well as a default ping', function () {
class Dep {
async _init() {}
async _privateMethod() {}
async someMethod() {
return 5;
}
}
const service = createMoleculerServiceFromClass({Service: Dep, name: 'dep', version: '1'});
const name = service.name;
const version = service.version;
const actions = service.actions;
should.equal(name, 'dep');
should.equal(version, '1');
should.equal(hasOwnProperty(actions, '_privateMethod'), false);
should.equal(hasOwnProperty(actions, 'constructor'), false);
should.equal(hasOwnProperty(actions, 'someMethod'), true);
should.equal(hasOwnProperty(actions, 'ping'), true);
});
it('Wires up dynamic and static dependencies correctly', async function () {
const fakeStaticDep = 13;
class Dep {
async _init() {}
constructor({staticDep}) {
this._staticDep = staticDep;
should.equal(staticDep, fakeStaticDep);
}
async someMethod() {
return this._staticDep;
}
}
class Main {
async _init() {}
/**
* @param {object} deps
* @param {Dep} deps.dep
*/
constructor({dep}) {
this._dep = dep;
}
async someOtherMethod() {
const num = await this._dep.someMethod();
return num * 2;
}
}
const depService = createMoleculerServiceFromClass({Service: Dep, name: 'dep', version: '1', staticDeps: {
staticDep: fakeStaticDep
}});
const mainService = createMoleculerServiceFromClass({Service: Main, name: 'main', version: '1', serviceDeps: {
dep: {
name: 'dep',
version: '1'
}
}});
const broker = new moleculer.ServiceBroker({logger: false});
broker.createService(depService);
broker.createService(mainService);
await broker.start();
const someMethod = sinon.spy(Dep.prototype, 'someMethod');
const someOtherMethod = sinon.spy(Main.prototype, 'someOtherMethod');
const result = await broker.call('1.main.someOtherMethod');
should.equal(someMethod.called, true);
should.equal(someOtherMethod.called, true);
should.equal(result, 26);
await broker.stop();
});
});