From 0d6ecbec17a043e5ea048188e08ef2a112bff1cf Mon Sep 17 00:00:00 2001 From: Sean Parmelee Date: Tue, 25 Apr 2017 10:31:36 -0500 Subject: [PATCH 1/2] iterate through keys with a regular for loop --- spec/core/matchers/matchersUtilSpec.js | 43 ++++++++++++++++++++++++++ src/core/matchers/matchersUtil.js | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index 3a8054a7..6d48b9cd 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -413,6 +413,49 @@ describe("matchersUtil", function() { var setB = new Set([6, 3]); expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false); }); + + describe("when running in an environment with array polyfills", function() { + // IE 8 doesn't support `definePropery` on non-DOM nodes + if (jasmine.getEnv().ieVersion < 9) { return; } + + beforeEach(function() { + this.origDescriptor = Object.getOwnPropertyDescriptor(Array.prototype, 'findIndex'); + Object.defineProperty(Array.prototype, 'findIndex', { + enumerable: true, + value: function (predicate) { + if (this === null) { + throw new TypeError('Array.prototype.findIndex called on null or undefined'); + } + + if (typeof predicate !== 'function') { + throw new TypeError('predicate must be a function'); + } + + var list = Object(this); + var length = list.length >>> 0; + var thisArg = arguments[1]; + var value; + + for (var i = 0; i < length; i++) { + value = list[i]; + if (predicate.call(thisArg, value, i, list)) { + return i; + } + } + + return -1; + } + }); + }); + + afterEach(function() { + Object.defineProperty(Array.prototype, 'findIndex', this.origDescriptor); + }); + + it("passes when there's an array polyfill", function() { + expect(['foo']).toEqual(['foo']); + }); + }); }); describe("contains", function() { diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index 28314891..3aba3f0f 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -327,7 +327,7 @@ getJasmineRequireObj().matchersUtil = function(j$) { } var extraKeys = []; - for (var i in allKeys) { + for (var i = 0; i < allKeys.length; i++) { if (!allKeys[i].match(/^[0-9]+$/)) { extraKeys.push(allKeys[i]); } From ef3cfe7f449d2daec96692491025dc71d75cfa66 Mon Sep 17 00:00:00 2001 From: Sean Parmelee Date: Tue, 25 Apr 2017 12:27:32 -0500 Subject: [PATCH 2/2] =?UTF-8?q?skip=20the=20test=20when=20we=20can?= =?UTF-8?q?=E2=80=99t=20get=20the=20propertyDescriptor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/core/matchers/matchersUtilSpec.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index 6d48b9cd..1e1140e2 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -418,8 +418,12 @@ describe("matchersUtil", function() { // IE 8 doesn't support `definePropery` on non-DOM nodes if (jasmine.getEnv().ieVersion < 9) { return; } + var findIndexDescriptor = Object.getOwnPropertyDescriptor(Array.prototype, 'findIndex'); + if (!findIndexDescriptor) { + return; + } + beforeEach(function() { - this.origDescriptor = Object.getOwnPropertyDescriptor(Array.prototype, 'findIndex'); Object.defineProperty(Array.prototype, 'findIndex', { enumerable: true, value: function (predicate) { @@ -449,7 +453,7 @@ describe("matchersUtil", function() { }); afterEach(function() { - Object.defineProperty(Array.prototype, 'findIndex', this.origDescriptor); + Object.defineProperty(Array.prototype, 'findIndex', findIndexDescriptor); }); it("passes when there's an array polyfill", function() {