Skip to content

Commit

Permalink
Added unit tests for #1482
Browse files Browse the repository at this point in the history
  • Loading branch information
garrypas authored and mbest committed Apr 15, 2017
1 parent 62d5683 commit 068a041
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions spec/utilsBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ describe('arrayForEach', function () {

expect(callback).not.toHaveBeenCalled();
});

it('Should alter "this" context when defined as an argument', function() {
var expectedContext = {};
var actualContext = null;
ko.utils.arrayForEach(["a"], function() {
actualContext = this;
}, expectedContext);
expect(actualContext).toBe(expectedContext);
});
});

describe('arrayIndexOf', function () {
Expand Down Expand Up @@ -203,14 +212,27 @@ describe('arrayMap', function () {
it('Should copy the array before returning it', function () {
var identityFunction = function(x) {
return x;
}
};

var input = ["a", "b", "c"];
var result = ko.utils.arrayMap(input, identityFunction);

expect(result).toEqual(input);
expect(result).not.toBe(input);
});

it('Should alter "this" context when defined as an argument', function() {
var expectedContext = {};
var actualContext = null;
var identityFunction = function(x) {
actualContext = this;
return x;
};

ko.utils.arrayMap(["a"], identityFunction, expectedContext);

expect(actualContext).toBe(expectedContext);
});
});

describe('arrayFilter', function () {
Expand All @@ -236,14 +258,27 @@ describe('arrayFilter', function () {
it('Should copy the array before returning it', function () {
var alwaysTrue = function(x) {
return true;
}
};

var input = ["a", "b", "c"];
var result = ko.utils.arrayFilter(input, alwaysTrue);

expect(result).toEqual(input);
expect(result).not.toBe(input);
});

it('Should alter "this" context when defined as an argument', function () {
var expectedContext = {};
var actualContext = null;
var identityFunction = function(x) {
actualContext = this;
return x;
};

var result = ko.utils.arrayFilter(["a"], identityFunction, expectedContext);

expect(expectedContext).toEqual(actualContext);
});
});

describe('arrayPushAll', function () {
Expand Down Expand Up @@ -342,3 +377,18 @@ describe('Function.bind', function() {
expect(bound3()).toEqual([object1, 'b']);
});
});

describe('objectMap', function () {
it('Should alter "this" context when defined as an argument', function() {
var expectedContext = {};
var actualContext = null;
var identityFunction = function(obj) {
actualContext = this;
return {x : obj.x};
};

var result = ko.utils.objectMap({x:1}, identityFunction, expectedContext);

expect(expectedContext).toEqual(actualContext);
});
});

0 comments on commit 068a041

Please sign in to comment.