Skip to content

Commit

Permalink
Add cross-realm URL support
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenvachon committed Apr 27, 2017
1 parent 9675580 commit 0d0b095
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/chai-subset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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];
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions test/unit/chai-subset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down

0 comments on commit 0d0b095

Please sign in to comment.