Fixed comparison between URL objects

* Fixes #1866
This commit is contained in:
Steve Gravrock
2020-11-21 13:35:10 -08:00
parent 88de272c89
commit 89331bb1bb
8 changed files with 106 additions and 0 deletions

View File

@@ -329,6 +329,15 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
);
};
j$.isURL = function(obj) {
return (
obj !== null &&
typeof obj !== 'undefined' &&
typeof jasmineGlobal.URL !== 'undefined' &&
obj.constructor === jasmineGlobal.URL
);
};
j$.isDataView = function(obj) {
return (
obj !== null &&
@@ -5094,6 +5103,10 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
diffBuilder.recordMismatch();
return false;
}
} else if (j$.isURL(a) && j$.isURL(b)) {
// URLs have no enumrable properties, so the default object comparison
// would consider any two URLs to be equal.
return a.toString() === b.toString();
} else {
// Objects with different constructors are not equivalent, but `Object`s
// or `Array`s from different frames are.

View File

@@ -62,4 +62,17 @@ describe('base helpers', function() {
expect(jasmineUnderTest.isSet({})).toBe(false);
});
});
describe('isURL', function() {
it('returns true when the object is a URL', function() {
jasmine.getEnv().requireUrls();
// eslint-disable-next-line compat/compat
expect(jasmineUnderTest.isURL(new URL('http://localhost/'))).toBe(true);
});
it('returns false when the object is not a URL', function() {
jasmine.getEnv().requireUrls();
expect(jasmineUnderTest.isURL({})).toBe(false);
});
});
});

View File

@@ -837,6 +837,54 @@ describe('matchersUtil', function() {
expect(matchersUtil.equals(mapA, mapB)).toBe(false);
});
it('passes when comparing two identical URLs', function() {
jasmine.getEnv().requireUrls();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(
matchersUtil.equals(
// eslint-disable-next-line compat/compat
new URL('http://localhost/1'),
// eslint-disable-next-line compat/compat
new URL('http://localhost/1')
)
).toBe(true);
});
it('fails when comparing two different URLs', function() {
jasmine.getEnv().requireUrls();
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
// eslint-disable-next-line compat/compat
url1 = new URL('http://localhost/1');
// eslint-disable-next-line compat/compat
expect(matchersUtil.equals(url1, new URL('http://localhost/2'))).toBe(
false
);
// eslint-disable-next-line compat/compat
expect(matchersUtil.equals(url1, new URL('http://localhost/1?foo'))).toBe(
false
);
// eslint-disable-next-line compat/compat
expect(matchersUtil.equals(url1, new URL('http://localhost/1#foo'))).toBe(
false
);
// eslint-disable-next-line compat/compat
expect(matchersUtil.equals(url1, new URL('https://localhost/1'))).toBe(
false
);
expect(
// eslint-disable-next-line compat/compat
matchersUtil.equals(url1, new URL('http://localhost:8080/1'))
).toBe(false);
// eslint-disable-next-line compat/compat
expect(matchersUtil.equals(url1, new URL('http://example.com/1'))).toBe(
false
);
});
describe('when running in an environment with array polyfills', function() {
var findIndexDescriptor = Object.getOwnPropertyDescriptor(
Array.prototype,

View File

@@ -0,0 +1,17 @@
/* eslint-disable compat/compat */
(function(env) {
function hasUrlConstructor() {
try {
new URL('http://localhost/');
return true;
} catch (e) {
return false;
}
}
env.requireUrls = function() {
if (!hasUrlConstructor()) {
env.pending('Environment does not support URLs');
}
};
})(jasmine.getEnv());

View File

@@ -24,6 +24,7 @@ module.exports = {
'helpers/checkForSet.js',
'helpers/checkForSymbol.js',
'helpers/checkForTypedArrays.js',
'helpers/checkForUrl.js',
'helpers/domHelpers.js',
'helpers/integrationMatchers.js',
'helpers/promises.js',

View File

@@ -11,6 +11,7 @@
"helpers/checkForSet.js",
"helpers/checkForSymbol.js",
"helpers/checkForTypedArrays.js",
"helpers/checkForUrl.js",
"helpers/domHelpers.js",
"helpers/integrationMatchers.js",
"helpers/promises.js",

View File

@@ -161,6 +161,15 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
);
};
j$.isURL = function(obj) {
return (
obj !== null &&
typeof obj !== 'undefined' &&
typeof jasmineGlobal.URL !== 'undefined' &&
obj.constructor === jasmineGlobal.URL
);
};
j$.isDataView = function(obj) {
return (
obj !== null &&

View File

@@ -475,6 +475,10 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
diffBuilder.recordMismatch();
return false;
}
} else if (j$.isURL(a) && j$.isURL(b)) {
// URLs have no enumrable properties, so the default object comparison
// would consider any two URLs to be equal.
return a.toString() === b.toString();
} else {
// Objects with different constructors are not equivalent, but `Object`s
// or `Array`s from different frames are.