From 443dcf0aa39649cf1c9a68ee750f1969c97dd3a9 Mon Sep 17 00:00:00 2001 From: Thom Wright Date: Tue, 19 Jan 2016 20:32:48 +0000 Subject: [PATCH] Use original all method if asserting on an object Fixes #24 --- lib/chai-things.js | 23 ++++++++++++++++------- package.json | 2 +- test/all.coffee | 28 ++++++++++++++-------------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/chai-things.js b/lib/chai-things.js index b1388c3..33c2c88 100644 --- a/lib/chai-things.js +++ b/lib/chai-things.js @@ -89,12 +89,12 @@ methodNames.forEach(function (methodName) { // Override the method to react on a possible `something` in the chain - Assertion.overwriteMethod(methodName, function (_super) { + Assertion.overwriteMethod(methodName, function (originalMethod) { return function somethingMethod() { // Return if no `something` has been used in the chain var somethingFlags = utils.flag(this, "something"); if (!somethingFlags) - return _super.apply(this, arguments); + return originalMethod.apply(this, arguments); // Use the nested `something` flag as the new `something` flag for this. utils.flag(this, "something", utils.flag(somethingFlags, "something")); @@ -157,17 +157,26 @@ }); // Override the method to react on a possible `all` in the chain - Assertion.overwriteMethod(methodName, function (_super) { + Assertion.overwriteMethod(methodName, function (originalMethod) { return function allMethod() { - // Return if no `all` has been used in the chain + // use original method if no `all` has been used in the chain var allFlags = utils.flag(this, "all"); - if (!allFlags) - return _super.apply(this, arguments); + if (!allFlags) { + return originalMethod.apply(this, arguments); + } + + // use original method if asserting on an object + // we're probably doing should.have.all.keys + var obj = utils.flag(this, "object"); + if (utils.type(obj) === "object") { + return originalMethod.apply(this, arguments); + } + // Use the nested `all` flag as the new `all` flag for this. utils.flag(this, "all", utils.flag(allFlags, "all")); // The assertion's object for `all` should be array-like - var arrayObject = utils.flag(this, "object"); + var arrayObject = obj; expect(arrayObject).to.have.property("length"); var length = arrayObject.length; expect(length).to.be.a("number", "all object length"); diff --git a/package.json b/package.json index 602a8ab..d0843c5 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "test": "./node_modules/mocha/bin/mocha" }, "devDependencies": { - "chai": "1.4.x", + "chai": "1.9.x", "coffee-script": "1.4.x", "mocha": "1.7.x", "jshint": "0.9.x" diff --git a/test/all.coffee b/test/all.coffee index 7689492..2509a28 100644 --- a/test/all.coffee +++ b/test/all.coffee @@ -2,28 +2,28 @@ describe "using all()", -> +describe "original chai all.keys implementation", -> + simpleObj = + foo: "foo" + bar: "bar" + + it "should assert keys on an object", -> + simpleObj.should.have.all.keys ["foo", "bar"] + + it "fails if the object doesn't have all keys", -> + (() -> simpleObj.should.have.all.keys ["foo", "bar", "baz"]). + should.throw + describe "an object without length", -> nonLengthObject = {} it "fails to have all elements equal to 1", -> (() -> nonLengthObject.should.all.equal 1). - should.throw "expected {} to have a property 'length'" + should.throw it "fails not to have all elements equal to 1", -> (() -> nonLengthObject.should.all.equal 1). - should.throw "expected {} to have a property 'length'" - - -describe "an object without numeric length", -> - nonNumLengthObject = { length: 'a' } - - it "fails to have all elements equal to 1", -> - (() -> nonNumLengthObject.should.all.equal 1). - should.throw "all object length: expected 'a' to be a number" - - it "fails not to have all elements equal to 1", -> - (() -> nonNumLengthObject.should.all.equal 1). - should.throw "all object length: expected 'a' to be a number" + should.throw describe "an empty array's elements", ->