不要怂,就是干,撸起袖子干!

Commit c1634392 by Nate Silva Committed by Sushant

fix(hooks): removeHook should only remove the specified hook (#8095)

previously it would also remove all unnamed hooks of the same type
1 parent ed6b3ca1
Showing with 36 additions and 2 deletions
......@@ -182,9 +182,10 @@ const Hooks = {
this.options.hooks[type] = this.options.hooks[type].filter(hook => {
if (isReference && typeof hook === 'function') {
return hook !== name; // check if same method
} else {
return typeof hook === 'object' && hook.name !== name;
} else if (!isReference && typeof hook === 'object') {
return hook.name !== name;
}
return true;
});
}
......
......@@ -209,6 +209,39 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
expect(hook2).not.to.have.been.called;
});
});
it('should not remove other hooks', function() {
const hook1 = sinon.spy(),
hook2 = sinon.spy(),
hook3 = sinon.spy(),
hook4 = sinon.spy();
this.Model.addHook('beforeCreate', hook1);
this.Model.addHook('beforeCreate', 'myHook', hook2);
this.Model.beforeCreate('myHook2', hook3);
this.Model.beforeCreate(hook4);
return this.Model.runHooks('beforeCreate').bind(this).then(function() {
expect(hook1).to.have.been.calledOnce;
expect(hook2).to.have.been.calledOnce;
expect(hook3).to.have.been.calledOnce;
expect(hook4).to.have.been.calledOnce;
hook1.reset();
hook2.reset();
hook3.reset();
hook4.reset();
this.Model.removeHook('beforeCreate', 'myHook');
return this.Model.runHooks('beforeCreate');
}).then(() => {
expect(hook1).to.have.been.calledOnce;
expect(hook2).not.to.have.been.called;
expect(hook3).to.have.been.calledOnce;
expect(hook4).to.have.been.calledOnce;
});
});
});
describe('#addHook', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!