Skip to content

Commit

Permalink
[rebase] take #1305 as a workbase
Browse files Browse the repository at this point in the history
  • Loading branch information
scottinet committed May 28, 2019
1 parent 5df2125 commit 6593e9e
Showing 1 changed file with 120 additions and 2 deletions.
122 changes: 120 additions & 2 deletions test/api/core/pluginsManager/pipe.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const
mockrequire = require('mock-require'),
should = require('should'),
sinon = require('sinon'),
ElasticsearchClientMock = require('../../../mocks/services/elasticsearchClient.mock'),
KuzzleMock = require('../../../mocks/kuzzle.mock'),
{
errors: {
PluginImplementationError
}
},
Request: KuzzleRequest
} = require('kuzzle-common-objects');

describe('pluginsManager.pipe', () => {
Expand All @@ -22,7 +24,10 @@ describe('pluginsManager.pipe', () => {
const PluginsManager = mockrequire.reRequire('../../../../lib/api/core/plugins/pluginsManager');

kuzzle = new KuzzleMock();
kuzzle.emit.restore();
kuzzle.pipe.restore();
pluginsManager = new PluginsManager(kuzzle);
kuzzle.pluginsManager = pluginsManager;
});

it('should trigger hooks with wildcard event', done => {
Expand All @@ -47,6 +52,119 @@ describe('pluginsManager.pipe', () => {
});
});

it('should trigger hooks with before wildcard event', done => {
pluginsManager.plugins = [{
object: {
init: () => {},
hooks: {
'foo:before*': 'myFunc'
},
myFunc: done
},
config: {},
activated: true,
manifest: {
name: 'foo'
}
}];

pluginsManager.run()
.then(() => {
kuzzle.emit('foo:beforeBar');
});
});

it('should trigger hooks with after wildcard event', done => {
pluginsManager.plugins = [{
object: {
init: () => {},
hooks: {
'foo:after*': 'myFunc'
},
myFunc: done
},
config: {},
activated: true,
manifest: {
name: 'foo'
}
}];

pluginsManager.run()
.then(() => {
kuzzle.emit('foo:afterBar');
});
});

it('should trigger pipes with wildcard event', () => {
pluginsManager.plugins = [{
object: {
init: () => {},
pipes: {
'foo:*': 'myFunc'
},
myFunc: sinon.stub().callsArgWith(1, null, new KuzzleRequest({}))
},
config: {},
activated: true,
manifest: {
name: 'foo'
}
}];

return pluginsManager.run()
.then(() => kuzzle.pipe('foo:bar'))
.then(() => {
should(pluginsManager.plugins[0].object.myFunc).be.calledOnce();
});
});

it('should trigger pipes with before wildcard event', () => {
pluginsManager.plugins = [{
object: {
init: () => {},
pipes: {
'foo:before*': 'myFunc'
},
myFunc: sinon.stub().callsArgWith(1, null, new KuzzleRequest({}))
},
config: {},
activated: true,
manifest: {
name: 'foo'
}
}];

return pluginsManager.run()
.then(() => kuzzle.pipe('foo:beforeBar'))
.then(() => {
should(pluginsManager.plugins[0].object.myFunc).be.calledOnce();
});
});

it('should trigger pipes with after wildcard event', () => {
pluginsManager.plugins = [{
object: {
init: () => {},
pipes: {
'foo:after*': 'myFunc'
},
myFunc: sinon.stub().callsArgWith(1, null, new KuzzleRequest({}))
},
config: {},
activated: true,
manifest: {
name: 'foo'
}
}];

return pluginsManager.run()
.then(() => kuzzle.pipe('foo:afterBar'))
.then(() => {
should(pluginsManager.plugins[0].object.myFunc).be.calledOnce();
});
});

it('should reject a pipe returned value if it is not a Request instance', () => {
const pluginMock = {
object: {
Expand All @@ -69,7 +187,7 @@ describe('pluginsManager.pipe', () => {

pluginsManager.registerPipe(pluginMock, 50, 200, 'foo:bar', 'myFunc');

return should(pluginsManager.pipe('foo:bar')).rejectedWith(
return should(kuzzle.pipe('foo:bar')).rejectedWith(
PluginImplementationError,
{message: /^Plugin foo pipe for event 'foo:bar' threw a non-Kuzzle error: Error: foobar.*/});
});
Expand Down

0 comments on commit 6593e9e

Please sign in to comment.