From 14958ed6fc426bc357f6399e0d3ec47b00b8e856 Mon Sep 17 00:00:00 2001 From: acunil Date: Thu, 17 Dec 2020 11:14:43 +0000 Subject: [PATCH 1/3] 9 Tests passing --- .gitignore | 2 ++ package.json | 5 +++-- test/clone-object.js | 14 ++++++++++---- test/flatten-array.js | 21 +++++++++++++++++---- test/scoping.js | 20 ++++++++++---------- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index da23d0d..44e4d24 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ build/Release # Deployed apps should consider commenting this line out: # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git node_modules + +package-lock.json \ No newline at end of file diff --git a/package.json b/package.json index 7d46b91..b317b6d 100644 --- a/package.json +++ b/package.json @@ -26,11 +26,12 @@ }, "homepage": "https://github.com/ahmednuaman/javascript-tests", "dependencies": { - "karma": "^0.12.32", + "karma": "^5.2.3", "karma-jasmine": "^0.3.5", - "karma-phantomjs-launcher": "^0.1.4" + "karma-phantomjs-launcher": "^1.0.4" }, "devDependencies": { + "jasmine-core": "^3.6.0", "karma-chrome-launcher": "^0.1.12", "karma-firefox-launcher": "^0.1.6" } diff --git a/test/clone-object.js b/test/clone-object.js index 86ec647..b5f57f7 100644 --- a/test/clone-object.js +++ b/test/clone-object.js @@ -1,7 +1,13 @@ -describe('clone object', function () { - it('should clone an object', function () { - var expected = {name: 'Ahmed', age: 27, skills: ['cycling', 'walking', 'eating']}, - obj = {}; +describe("clone object", function () { + it("should clone an object", function () { + var expected = { + name: "Ahmed", + age: 27, + skills: ["cycling", "walking", "eating"], + }, + obj = {}; + + obj = JSON.parse(JSON.stringify(expected)); expect(obj).toEqual(expected); expect(obj).not.toBe(expected); diff --git a/test/flatten-array.js b/test/flatten-array.js index c7f0632..4f11b43 100644 --- a/test/flatten-array.js +++ b/test/flatten-array.js @@ -1,8 +1,21 @@ -describe('flatten array', function () { - it('should flatten an array', function () { +describe("flatten array", function () { + it("should flatten an array", function () { var arr = [1, 2, [1, 2, [3, 4, 5, [1]]], 2, [2]], - expected = [1, 1, 1, 2, 2, 2, 2, 3, 4, 5]; + expected = [1, 1, 1, 2, 2, 2, 2, 3, 4, 5]; + + // arr.flat() does not work on IE + + function flatten(array) { + if (array instanceof Array) { + return array.length === 0 + ? [] + : flatten(array[0]).concat(flatten(array.slice(1))); + } + return [array]; + } + + arr = flatten(arr).sort(); expect(arr).toEqual(expected); }); -}); \ No newline at end of file +}); diff --git a/test/scoping.js b/test/scoping.js index 557c54a..9872719 100644 --- a/test/scoping.js +++ b/test/scoping.js @@ -1,24 +1,24 @@ -describe('scoping', function () { - it('should correctly deal with scoping `this` back to the callee', function () { +describe("scoping", function () { + it("should correctly deal with scoping `this` back to the callee", function () { var mod = new Module(), - request; + request; request = function (callback) { return callback(); }; - function Module () { - this.foo = 'bar'; + function Module() { + this.foo = "bar"; } - Module.prototype.method = function() { + Module.prototype.method = function () { return this.foo; }; - Module.prototype.req = function() { - return request(this.method); + Module.prototype.req = function () { + return request(this.method.bind(this)); }; - expect(mod.req()).toBe('bar'); + expect(mod.req()).toBe("bar"); }); -}); \ No newline at end of file +}); From 5d1711f22336d00c791bd5386f3b6ca725801f4d Mon Sep 17 00:00:00 2001 From: acunil Date: Thu, 17 Dec 2020 11:38:00 +0000 Subject: [PATCH 2/3] formatting --- test/clone-object.js | 14 +++++--------- test/flatten-array.js | 6 +++--- test/scoping.js | 18 +++++++++--------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/test/clone-object.js b/test/clone-object.js index b5f57f7..0c1625b 100644 --- a/test/clone-object.js +++ b/test/clone-object.js @@ -1,14 +1,10 @@ -describe("clone object", function () { - it("should clone an object", function () { - var expected = { - name: "Ahmed", - age: 27, - skills: ["cycling", "walking", "eating"], - }, - obj = {}; +describe('clone object', function () { + it('should clone an object', function () { + var expected = {name: 'Ahmed', age: 27, skills: ['cycling', 'walking', 'eating']}, + obj = {}; obj = JSON.parse(JSON.stringify(expected)); - + expect(obj).toEqual(expected); expect(obj).not.toBe(expected); }); diff --git a/test/flatten-array.js b/test/flatten-array.js index 4f11b43..121eec8 100644 --- a/test/flatten-array.js +++ b/test/flatten-array.js @@ -1,7 +1,7 @@ -describe("flatten array", function () { - it("should flatten an array", function () { +describe('flatten array', function () { + it('should flatten an array', function () { var arr = [1, 2, [1, 2, [3, 4, 5, [1]]], 2, [2]], - expected = [1, 1, 1, 2, 2, 2, 2, 3, 4, 5]; + expected = [1, 1, 1, 2, 2, 2, 2, 3, 4, 5]; // arr.flat() does not work on IE diff --git a/test/scoping.js b/test/scoping.js index 9872719..92c32e3 100644 --- a/test/scoping.js +++ b/test/scoping.js @@ -1,24 +1,24 @@ -describe("scoping", function () { - it("should correctly deal with scoping `this` back to the callee", function () { +describe('scoping', function () { + it('should correctly deal with scoping `this` back to the callee', function () { var mod = new Module(), - request; + request; request = function (callback) { return callback(); }; - function Module() { - this.foo = "bar"; + function Module () { + this.foo = 'bar'; } - Module.prototype.method = function () { + Module.prototype.method = function() { return this.foo; }; - Module.prototype.req = function () { + Module.prototype.req = function() { return request(this.method.bind(this)); }; - expect(mod.req()).toBe("bar"); + expect(mod.req()).toBe('bar'); }); -}); +}); \ No newline at end of file From 0202caca9254d3c166a8c13cbfff1baa897ef660 Mon Sep 17 00:00:00 2001 From: acunil Date: Thu, 17 Dec 2020 11:40:05 +0000 Subject: [PATCH 3/3] extra line --- test/clone-object.js | 4 ++-- test/flatten-array.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/clone-object.js b/test/clone-object.js index 0c1625b..b0ded9b 100644 --- a/test/clone-object.js +++ b/test/clone-object.js @@ -4,8 +4,8 @@ describe('clone object', function () { obj = {}; obj = JSON.parse(JSON.stringify(expected)); - + expect(obj).toEqual(expected); expect(obj).not.toBe(expected); }); -}); +}); \ No newline at end of file diff --git a/test/flatten-array.js b/test/flatten-array.js index 121eec8..349773f 100644 --- a/test/flatten-array.js +++ b/test/flatten-array.js @@ -18,4 +18,4 @@ describe('flatten array', function () { expect(arr).toEqual(expected); }); -}); +}); \ No newline at end of file