Skip to content

Commit

Permalink
Allow plugin to be loaded multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
martinj committed Mar 16, 2022
1 parent fd0b10f commit c6169c1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const validOptions = Joi.array().items(Joi.object({
function register(server, opts) {
const value = Joi.attempt(opts, validOptions);

const snsInstances = [];
const snsInstances = (server.plugins.hookido && server.plugins.hookido.snsInstances) || [];
value.forEach((config) => init(config));
server.expose('snsInstances', snsInstances);
return;
Expand Down Expand Up @@ -80,5 +80,6 @@ function register(server, opts) {

module.exports = {
name: 'hookido',
register
register,
multiple: true
};
37 changes: 37 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,41 @@ describe('Hookido Hapi Plugin', () => {
expect(server.plugins.hookido.snsInstances).to.have.a.lengthOf(2);

});

it('supports to load multiple times', async () => {

const server = new Hapi.Server();

await server.register({
plugin,
options: {
route: {
path: '/foobar'
},
handlers: {
notification: () => {}
}
}
});

await server.register({
plugin,
options: {
route: {
path: '/foobar2'
},
handlers: {
notification: () => {}
}
}
});

const table = server.table();

expect(table[0].path).to.equal('/foobar');
expect(table[1].path).to.equal('/foobar2');
expect(server.plugins.hookido.snsInstances).to.have.a.lengthOf(2);

});

});

0 comments on commit c6169c1

Please sign in to comment.