Skip to content

Commit 0b0353c

Browse files
authored
feat(events): add addChainableMethod event (#1707)
1 parent 4cbd383 commit 0b0353c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/chai/utils/addChainableMethod.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import {Assertion} from '../assertion.js';
8+
import {PluginEvent, events} from './events.js';
89
import {addLengthGuard} from './addLengthGuard.js';
910
import {flag} from './flag.js';
1011
import {proxify} from './proxify.js';
@@ -36,6 +37,13 @@ let excludeNames = Object.getOwnPropertyNames(testFn).filter(function (name) {
3637
let call = Function.prototype.call,
3738
apply = Function.prototype.apply;
3839

40+
export class PluginAddChainableMethodEvent extends PluginEvent {
41+
constructor(type, name, fn, chainingBehavior) {
42+
super(type, name, fn);
43+
this.chainingBehavior = chainingBehavior;
44+
}
45+
}
46+
3947
/**
4048
* ### .addChainableMethod(ctx, name, method, chainingBehavior)
4149
*
@@ -143,4 +151,13 @@ export function addChainableMethod(ctx, name, method, chainingBehavior) {
143151
},
144152
configurable: true
145153
});
154+
155+
events.dispatchEvent(
156+
new PluginAddChainableMethodEvent(
157+
'addChainableMethod',
158+
name,
159+
method,
160+
chainingBehavior
161+
)
162+
);
146163
}

test/utilities.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,5 +1552,32 @@ describe('utilities', function () {
15521552

15531553
expect(calledTimes).to.equal(1);
15541554
});
1555+
1556+
it('emits addChainableMethod', function () {
1557+
var calledTimes = 0;
1558+
1559+
chai.use(function(_chai, _utils) {
1560+
const method = function () {
1561+
new chai.Assertion(this._obj).to.be.equal('x');
1562+
}
1563+
const _chainingBehavior = function () {
1564+
if (this._obj === Object(this._obj)) {
1565+
this._obj.__x = 'X!'
1566+
}
1567+
}
1568+
1569+
chai.util.events.addEventListener("addChainableMethod", eventHandler = function({ name, fn, chainingBehavior }) {
1570+
if (name === 'x' && fn === method && chainingBehavior === _chainingBehavior)
1571+
calledTimes++;
1572+
});
1573+
_chai.Assertion.addChainableMethod('x',
1574+
method
1575+
, _chainingBehavior
1576+
)
1577+
1578+
});
1579+
1580+
expect(calledTimes).to.equal(1);
1581+
});
15551582
});
15561583
});

0 commit comments

Comments
 (0)