Skip to content
This repository was archived by the owner on May 26, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 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 @@ -57,14 +58,22 @@
});
}

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;
}
}

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