Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/chai/utils/addChainableMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import {Assertion} from '../assertion.js';
import {PluginEvent, events} from './events.js';
import {addLengthGuard} from './addLengthGuard.js';
import {flag} from './flag.js';
import {proxify} from './proxify.js';
Expand Down Expand Up @@ -36,6 +37,13 @@ let excludeNames = Object.getOwnPropertyNames(testFn).filter(function (name) {
let call = Function.prototype.call,
apply = Function.prototype.apply;

export class PluginAddChainableMethodEvent extends PluginEvent {
constructor(type, name, fn, chainingBehavior) {
super(type, name, fn);
this.chainingBehavior = chainingBehavior;
}
}

/**
* ### .addChainableMethod(ctx, name, method, chainingBehavior)
*
Expand Down Expand Up @@ -143,4 +151,13 @@ export function addChainableMethod(ctx, name, method, chainingBehavior) {
},
configurable: true
});

events.dispatchEvent(
new PluginAddChainableMethodEvent(
'addChainableMethod',
name,
method,
chainingBehavior
)
);
}
27 changes: 27 additions & 0 deletions test/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,5 +1552,32 @@ describe('utilities', function () {

expect(calledTimes).to.equal(1);
});

it('emits addChainableMethod', function () {
var calledTimes = 0;

chai.use(function(_chai, _utils) {
const method = function () {
new chai.Assertion(this._obj).to.be.equal('x');
}
const _chainingBehavior = function () {
if (this._obj === Object(this._obj)) {
this._obj.__x = 'X!'
}
}

chai.util.events.addEventListener("addChainableMethod", eventHandler = function({ name, fn, chainingBehavior }) {
if (name === 'x' && fn === method && chainingBehavior === _chainingBehavior)
calledTimes++;
});
_chai.Assertion.addChainableMethod('x',
method
, _chainingBehavior
)

});

expect(calledTimes).to.equal(1);
});
});
});
Loading