Skip to content

Commit

Permalink
Test namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Feb 21, 2020
1 parent e399dc6 commit 787f8cd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
16 changes: 8 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,20 @@ internals.boundInstance = Symbol('boundInstance');

internals.services = (getRealm) => {

return function (all) {
return function (namespace) {

const realm = getRealm(this);

if (!all) {
if (!namespace) {
return internals.state(realm).services;
}

if (typeof all === 'string') {
const namespace = internals.rootState(realm).namespaces[all];
Hoek.assert(namespace.size !== 0, `The plugin namespace ${namespace} does not exist.`);
Hoek.assert(namespace.size <= 1, `The plugin namespace ${namespace} is not unique.`);
const [namespaceRealm] = [...namespace.values()];
return internals.rootState(namespaceRealm).services;
if (typeof namespace === 'string') {
const namespaceSet = internals.rootState(realm).namespaces[namespace];
Hoek.assert(namespaceSet, `The plugin namespace ${namespace} does not exist.`);
Hoek.assert(namespaceSet.size === 1, `The plugin namespace ${namespace} is not unique: is that plugin registered multiple times?`);
const [namespaceRealm] = [...namespaceSet.values()];
return internals.state(namespaceRealm).services;
}

return internals.rootState(realm).services;
Expand Down
58 changes: 56 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,11 @@ describe('Schmervice', () => {

describe('plugin', () => {

const getPlugin = async (server, name) => {
const getPlugin = async (server, name, others) => {

const register = () => null;

return await Ahem.instance(server, { name, register }, {}, { controlled: false });
return await Ahem.instance(server, { name, register, ...others }, {}, { controlled: false });
};

it('can be registered multiple times.', async () => {
Expand Down Expand Up @@ -1351,6 +1351,60 @@ describe('Schmervice', () => {
expect(serviceX).to.be.an.instanceof(ServiceX);
expect(serviceY).to.be.an.instanceof(ServiceY);
});

it('returns service instances associated with a plugin namespace when passed a string.', async () => {

const server = Hapi.server();
await server.register(Schmervice);

const ServiceX = class ServiceX {};
const ServiceY = class ServiceY {};
const ServiceZ = class ServiceZ {
static get [Schmervice.sandbox]() {

return true;
}
};
const ServiceW = class ServiceW {};

const pluginA = await getPlugin(server, 'a');
const pluginB = await getPlugin(pluginA, 'b');

server.registerService(ServiceX);
pluginA.registerService(ServiceY);
pluginA.registerService(ServiceZ);
pluginB.registerService(ServiceW);

expect(server.services()).to.shallow.equal(pluginB.services(true));
expect(server.services('a')).to.shallow.equal(pluginB.services('a'));
expect(pluginA.services('b')).to.shallow.equal(pluginB.services());

expect(server.services()).to.only.contain(['serviceX', 'serviceY', 'serviceW']);
expect(pluginA.services()).to.only.contain(['serviceY', 'serviceZ', 'serviceW']);
expect(pluginB.services()).to.only.contain(['serviceW']);
});

it('throws when accessing a namespace that doesn\'t exist.', async () => {

const server = Hapi.server();
await server.register(Schmervice);

expect(() => server.services('nope')).to.throw('The plugin namespace nope does not exist.');
});

it('throws when accessing a non-unique namespace.', async () => {

const server = Hapi.server();
await server.register(Schmervice);

const pluginX1 = await getPlugin(server, 'x', { multiple: true });
pluginX1.registerService({ name: 'serviceX' });

const pluginX2 = await getPlugin(server, 'x', { multiple: true });
pluginX2.registerService({ name: 'serviceY' });

expect(() => server.services('x')).to.throw('The plugin namespace x is not unique: is that plugin registered multiple times?');
});
});

describe('service ownership', () => {
Expand Down

0 comments on commit 787f8cd

Please sign in to comment.