Check for ObjectContaining on either side of equality.
- Also clean up `undefined` check. Fixes #682
This commit is contained in:
@@ -1552,13 +1552,9 @@ getJasmineRequireObj().ObjectContaining = function(j$) {
|
||||
ObjectContaining.prototype.asymmetricMatch = function(other) {
|
||||
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
||||
|
||||
var hasKey = function(obj, keyName) {
|
||||
return obj !== null && !j$.util.isUndefined(obj[keyName]);
|
||||
};
|
||||
|
||||
for (var property in this.sample) {
|
||||
if (!hasKey(other, property) && hasKey(this.sample, property) ||
|
||||
!j$.matchersUtil.equals(other[property], this.sample[property])) {
|
||||
if (!Object.prototype.hasOwnProperty.call(other, property) ||
|
||||
!j$.matchersUtil.equals(this.sample[property], other[property])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -2259,6 +2255,10 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
||||
return b.asymmetricMatch(a);
|
||||
}
|
||||
|
||||
if (a instanceof j$.ObjectContaining) {
|
||||
return a.asymmetricMatch(b);
|
||||
}
|
||||
|
||||
if (b instanceof j$.ObjectContaining) {
|
||||
return b.asymmetricMatch(a);
|
||||
}
|
||||
|
||||
@@ -43,4 +43,16 @@ describe("ObjectContaining", function() {
|
||||
|
||||
expect(containing.asymmetricMatch({one: {two: {}}})).toBe(true);
|
||||
});
|
||||
|
||||
it("matches when key is present with undefined value", function() {
|
||||
var containing = new j$.ObjectContaining({ one: undefined });
|
||||
|
||||
expect(containing.asymmetricMatch({ one: undefined })).toBe(true);
|
||||
});
|
||||
|
||||
it("does not match when key with undefined value is not present", function() {
|
||||
var containing = new j$.ObjectContaining({ one: undefined });
|
||||
|
||||
expect(containing.asymmetricMatch({})).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -191,9 +191,11 @@ describe("matchersUtil", function() {
|
||||
var obj = {
|
||||
foo: 3,
|
||||
bar: 7
|
||||
};
|
||||
},
|
||||
containing = new j$.ObjectContaining({foo: 3});
|
||||
|
||||
expect(j$.matchersUtil.equals(obj, new j$.ObjectContaining({foo: 3}))).toBe(true);
|
||||
expect(j$.matchersUtil.equals(obj, containing)).toBe(true);
|
||||
expect(j$.matchersUtil.equals(containing, obj)).toBe(true);
|
||||
});
|
||||
|
||||
it("passes when a custom equality matcher returns true", function() {
|
||||
|
||||
@@ -7,13 +7,9 @@ getJasmineRequireObj().ObjectContaining = function(j$) {
|
||||
ObjectContaining.prototype.asymmetricMatch = function(other) {
|
||||
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
||||
|
||||
var hasKey = function(obj, keyName) {
|
||||
return obj !== null && !j$.util.isUndefined(obj[keyName]);
|
||||
};
|
||||
|
||||
for (var property in this.sample) {
|
||||
if (!hasKey(other, property) && hasKey(this.sample, property) ||
|
||||
!j$.matchersUtil.equals(other[property], this.sample[property])) {
|
||||
if (!Object.prototype.hasOwnProperty.call(other, property) ||
|
||||
!j$.matchersUtil.equals(this.sample[property], other[property])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,10 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
||||
return b.asymmetricMatch(a);
|
||||
}
|
||||
|
||||
if (a instanceof j$.ObjectContaining) {
|
||||
return a.asymmetricMatch(b);
|
||||
}
|
||||
|
||||
if (b instanceof j$.ObjectContaining) {
|
||||
return b.asymmetricMatch(a);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user