Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Solutions #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/clone-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ describe('clone object', function () {
it('should clone an object', function () {
var expected = {name: 'Ahmed', age: 27, skills: ['cycling', 'walking', 'eating']},
obj = {};
// simple one liner to deep clone an object, (regarded to be the fastest method for deep cloning objects?)
obj = JSON.parse(JSON.stringify(expected));

expect(obj).toEqual(expected);
expect(obj).not.toBe(expected);
Expand Down
12 changes: 9 additions & 3 deletions test/flatten-array.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
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];
var arr = [1, 2, [1, 2, [3, 4, 5, [1]]], 2, [2]];
var expected = [1, 1, 1, 2, 2, 2, 2, 3, 4, 5];

//enabling deep level flatten use recursion using reduce and concat methods
function flatten(arr) {
return arr.reduce(function (flat, val) => Array.isArray(val) ? flat.concat(flatten(val)) : flat.concat(val), []);
}
//sorting out an array in ascending order
arr = flatten(arr).sort();
expect(arr).toEqual(expected);
});
});
});
5 changes: 3 additions & 2 deletions test/scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ describe('scoping', function () {
return this.foo;
};

//using the bind method to call a function with the 'this' value set explicitly
Module.prototype.req = function() {
return request(this.method);
return request(this.method.bind(this));
};

expect(mod.req()).toBe('bar');
});
});
});