Skip to content

Commit

Permalink
Merge pull request sinonjs#1543 from alex94puchades/master
Browse files Browse the repository at this point in the history
Add support to instanceOf for [Symbol.hasInstance]
  • Loading branch information
fatso83 authored Aug 27, 2017
2 parents c419543 + d035dea commit 15d5737
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/sinon/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ function assertType(value, type, name) {
}
}

function assertMethodExists(value, method, name, methodPath) {
if (value[method] == null) {
throw new TypeError("Expected " + name + " to have method " + methodPath);
}
}

var matcher = {
toString: function () {
return this.message;
Expand Down Expand Up @@ -179,10 +185,14 @@ match.typeOf = function (type) {
};

match.instanceOf = function (type) {
assertType(type, "function", "type");
if (typeof Symbol === "undefined" || typeof Symbol.hasInstance === "undefined") {
assertType(type, "function", "type");
} else {
assertMethodExists(type, Symbol.hasInstance, "type", "[Symbol.hasInstance]");
}
return match(function (actual) {
return actual instanceof type;
}, "instanceOf(" + functionName(type) + ")");
}, "instanceOf(" + (functionName(type) || Object.prototype.toString.call(type)) + ")");
};

function createPropertyMatcher(propertyTest, messagePrefix) {
Expand Down
8 changes: 8 additions & 0 deletions test/match-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ describe("sinonMatch", function () {
}, "TypeError");
});

if (typeof Symbol !== "undefined" && typeof Symbol.hasInstance !== "undefined") {
it("does not throw if given argument defines Symbol.hasInstance", function () {
var objectWithCustomTypeChecks = {};
objectWithCustomTypeChecks[Symbol.hasInstance] = function () {};
sinonMatch.instanceOf(objectWithCustomTypeChecks);
});
}

it("returns matcher", function () {
var instanceOf = sinonMatch.instanceOf(function () {});

Expand Down

0 comments on commit 15d5737

Please sign in to comment.