Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36dae89

Browse files
committed
Return new assertion instead of this for overwriteChainableMethod
1 parent c1d5a54 commit 36dae89

File tree

2 files changed

+98
-34
lines changed

2 files changed

+98
-34
lines changed

lib/chai/utils/overwriteChainableMethod.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* MIT Licensed
55
*/
66

7+
var chai = require('../../chai');
8+
var transferFlags = require('./transferFlags');
9+
710
/**
811
* ### overwriteChainableMethod (ctx, name, method, chainingBehavior)
912
*
@@ -43,12 +46,24 @@ module.exports = function (ctx, name, method, chainingBehavior) {
4346
var _chainingBehavior = chainableBehavior.chainingBehavior;
4447
chainableBehavior.chainingBehavior = function () {
4548
var result = chainingBehavior(_chainingBehavior).call(this);
46-
return result === undefined ? this : result;
49+
if (result !== undefined) {
50+
return result;
51+
}
52+
53+
var newAssertion = new chai.Assertion();
54+
transferFlags(this, newAssertion);
55+
return newAssertion;
4756
};
4857

4958
var _method = chainableBehavior.method;
5059
chainableBehavior.method = function () {
5160
var result = method(_method).apply(this, arguments);
52-
return result === undefined ? this : result;
61+
if (result !== undefined) {
62+
return result;
63+
}
64+
65+
var newAssertion = new chai.Assertion();
66+
transferFlags(this, newAssertion);
67+
return newAssertion;
5368
};
5469
};

test/utilities.js

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -908,57 +908,106 @@ describe('utilities', function () {
908908
delete chai.Assertion.prototype.foo;
909909
});
910910

911-
it('overwriteChainableMethod', function () {
912-
chai.use(function (_chai, _) {
913-
_chai.Assertion.addChainableMethod('x',
914-
function () {
915-
new chai.Assertion(this._obj).to.be.equal('x');
916-
}
917-
, function () {
918-
this._obj = this._obj || {};
919-
this._obj.__x = 'X!'
920-
}
921-
);
911+
describe('overwriteChainableMethod', function() {
912+
var assertionConstructor;
913+
var utils;
922914

923-
_chai.Assertion.overwriteChainableMethod('x',
924-
function(_super) {
925-
return function() {
926-
if (_.flag(this, 'marked')) {
927-
new chai.Assertion(this._obj).to.be.equal('spot');
928-
} else {
915+
before(function() {
916+
chai.use(function (_chai, _utils) {
917+
assertionConstructor = _chai.Assertion;
918+
utils = _utils;
919+
920+
_chai.Assertion.addChainableMethod('x',
921+
function () {
922+
new chai.Assertion(this._obj).to.be.equal('x');
923+
}
924+
, function () {
925+
this._obj = this._obj || {};
926+
this._obj.__x = 'X!'
927+
}
928+
);
929+
930+
_chai.Assertion.overwriteChainableMethod('x',
931+
function(_super) {
932+
return function() {
933+
utils.flag(this, 'mySpecificFlag', 'value1');
934+
utils.flag(this, 'ultraSpecificFlag', 'value2');
935+
936+
if (utils.flag(this, 'marked')) {
937+
new chai.Assertion(this._obj).to.be.equal('spot');
938+
} else {
939+
_super.apply(this, arguments);
940+
}
941+
};
942+
}
943+
, function(_super) {
944+
return function() {
945+
utils.flag(this, 'message', 'x marks the spot');
929946
_super.apply(this, arguments);
930-
}
931-
};
932-
}
933-
, function(_super) {
934-
return function() {
935-
_.flag(this, 'message', 'x marks the spot');
936-
_super.apply(this, arguments);
937-
};
938-
}
939-
);
947+
};
948+
}
949+
);
940950

951+
_chai.Assertion.addMethod('checkFlags', function() {
952+
this.assert(
953+
utils.flag(this, 'mySpecificFlag') === 'value1' &&
954+
utils.flag(this, 'ultraSpecificFlag') === 'value2' &&
955+
utils.flag(this, 'message') === 'x marks the spot'
956+
, 'expected assertion to have specific flags'
957+
, "this doesn't matter"
958+
);
959+
});
960+
});
961+
});
962+
963+
after(function() {
964+
delete chai.Assertion.prototype.x;
965+
delete chai.Assertion.prototype.checkFlags;
966+
});
967+
968+
it('overwriteChainableMethod', function () {
941969
// Make sure the original behavior of 'x' remains the same
942970
expect('foo').x.to.equal("foo");
943971
expect("x").x();
944972
expect(function () {
945973
expect("foo").x();
946-
}).to.throw(_chai.AssertionError);
974+
}).to.throw(chai.AssertionError);
947975
var obj = {};
948976
expect(obj).x.to.be.ok;
949977
expect(obj).to.have.property('__x', 'X!');
950978

951979
// Test the new behavior of 'x'
952980
var assertion = expect('foo').x.to.be.ok;
953-
expect(_.flag(assertion, 'message')).to.equal('x marks the spot');
981+
expect(utils.flag(assertion, 'message')).to.equal('x marks the spot');
954982
expect(function () {
955983
var assertion = expect('x');
956-
_.flag(assertion, 'marked', true);
984+
utils.flag(assertion, 'marked', true);
957985
assertion.x()
958-
}).to.throw(_chai.AssertionError);
986+
}).to.throw(chai.AssertionError);
959987
});
960988

961-
delete chai.Assertion.prototype.x;
989+
it('should return a new assertion with flags copied over', function () {
990+
var assertion1 = expect('x');
991+
var assertion2 = assertion1.x();
992+
993+
chai.config.proxyExcludedKeys.push('nodeType');
994+
995+
// Checking if a new assertion was returned
996+
expect(assertion1).to.not.be.equal(assertion2);
997+
998+
// Check if flags were copied
999+
assertion2.checkFlags();
1000+
1001+
// Checking if it's really an instance of an Assertion
1002+
expect(assertion2).to.be.instanceOf(assertionConstructor);
1003+
1004+
// Test chaining `.length` after a method to guarantee it is not a function's `length`
1005+
expect('x').to.be.x().length.above(0);
1006+
1007+
// Ensure that foo returns an Assertion (not a function)
1008+
expect(expect('x').x()).to.be.an.instanceOf(assertionConstructor);
1009+
expect(expect('x').x).to.be.an.instanceOf(assertionConstructor);
1010+
});
9621011
});
9631012

9641013
it('compareByInspect', function () {

0 commit comments

Comments
 (0)