From 0d0b095f802f3eb49b4f291378f3afee4dd0ecc2 Mon Sep 17 00:00:00 2001 From: Steven D Vachon Date: Thu, 20 Apr 2017 00:09:58 -0400 Subject: [PATCH 1/2] Add cross-realm URL support fixes #60 --- lib/chai-subset.js | 9 +++++++++ package.json | 3 ++- test/unit/chai-subset.spec.js | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/chai-subset.js b/lib/chai-subset.js index d2dce91..456d72b 100644 --- a/lib/chai-subset.js +++ b/lib/chai-subset.js @@ -12,6 +12,7 @@ })(function(chai, utils) { var Assertion = chai.Assertion; var assertionPrototype = Assertion.prototype; + var toString = Object.prototype.toString; Assertion.addMethod('containSubset', function (expected) { var actual = utils.flag(this, 'object'); @@ -65,6 +66,14 @@ } } + if (toString.call(expected) === '[object URL]') { + if (toString.call(actual) === '[object URL]') { + return expected.href === actual.href; + } else { + return false; + } + } + return Object.keys(expected).every(function (key) { var eo = expected[key]; var ao = actual[key]; diff --git a/package.json b/package.json index 4fca43c..b6536a4 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,8 @@ "coveralls": "2.11.16", "istanbul": "0.4.5", "mocha": "3.2.0", - "mocha-eslint": "3.0.1" + "mocha-eslint": "3.0.1", + "universal-url": "^1.0.0-alpha" }, "files": [ "lib" diff --git a/test/unit/chai-subset.spec.js b/test/unit/chai-subset.spec.js index 3fc4e28..bc35882 100644 --- a/test/unit/chai-subset.spec.js +++ b/test/unit/chai-subset.spec.js @@ -173,6 +173,26 @@ describe('comparison of dates', function() { }); }); +describe('comparison of urls', function() { + const URL = require('universal-url').URL; + + it('should pass for the same url', function() { + expect(new URL('http://host/')).to.containSubset(new URL('http://host/')); + }); + + it('should pass for the same url if nested', function() { + expect({a: new URL('http://host/')}).to.containSubset({a: new URL('http://host/')}); + }); + + it('should fail for a different url', function() { + expect(new URL('http://host1/')).to.not.containSubset(new URL('http://host2/')); + }); + + it('should fail for a different url if nested', function() { + expect({a: new URL('http://host1/')}).to.not.containSubset({a: new URL('http://host2/')}); + }); +}); + describe('cyclic objects', () => { it('should pass', () => { const child = {}; From 11dc1d023d1db63e9efbb320ddaeec9a91c45fb4 Mon Sep 17 00:00:00 2001 From: Steven D Vachon Date: Wed, 26 Apr 2017 23:56:03 -0400 Subject: [PATCH 2/2] Add cross-realm Date support --- lib/chai-subset.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chai-subset.js b/lib/chai-subset.js index 456d72b..c20f7a7 100644 --- a/lib/chai-subset.js +++ b/lib/chai-subset.js @@ -58,8 +58,8 @@ }); } - if (expected instanceof Date) { - if (actual instanceof Date) { + if (toString.call(expected) === '[object Date]') { + if (toString.call(actual) === '[object Date]') { return expected.getTime() === actual.getTime(); } else { return false;