Test comparison of objects from different frames

This commit is contained in:
Ben Christel
2016-07-19 14:31:58 -07:00
parent 7f7dda7a2c
commit a0bce8031e
2 changed files with 28 additions and 2 deletions

View File

@@ -93,6 +93,24 @@ describe("matchersUtil", function() {
expect(jasmineUnderTest.matchersUtil.equals(new Error("foo"), new Error("bar"))).toBe(false);
});
it("fails for objects with different constructors", function() {
function One() {}
function Two() {}
expect(jasmineUnderTest.matchersUtil.equals(new One(), new Two())).toBe(false);
});
if (typeof document === 'object') {
it("passes for equivalent objects from different frames", function() {
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
console.log(iframe);
iframe.contentWindow.eval('window.testObject = {}');
expect(jasmineUnderTest.matchersUtil.equals({}, iframe.contentWindow.testObject)).toBe(true);
document.body.removeChild(iframe);
});
}
it("passes for Objects that are equivalent (simple case)", function() {
expect(jasmineUnderTest.matchersUtil.equals({a: "foo"}, {a: "foo"})).toBe(true);
});

View File

@@ -178,8 +178,8 @@ getJasmineRequireObj().matchersUtil = function(j$) {
// Objects with different constructors are not equivalent, but `Object`s
// or `Array`s from different frames are.
var aCtor = a.constructor, bCtor = b.constructor;
if (aCtor !== bCtor && !(isFunction(aCtor) && aCtor instanceof aCtor &&
isFunction(bCtor) && bCtor instanceof bCtor)) {
if (aCtor !== bCtor && !(isObjectConstructor(aCtor) &&
isObjectConstructor(bCtor))) {
return false;
}
}
@@ -239,5 +239,13 @@ getJasmineRequireObj().matchersUtil = function(j$) {
function isFunction(obj) {
return typeof obj === 'function';
}
function isObjectConstructor(ctor) {
// aCtor instanceof aCtor is true for the Object and Function
// constructors (since a constructor is-a Function and a function is-a
// Object). We don't just compare ctor === Object because the constructor
// might come from a different frame with different globals.
return isFunction(ctor) && ctor instanceof ctor;
}
}
};