Skip to content

Commit

Permalink
Add additional test cases to includes 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cflynn07 committed Oct 12, 2015
1 parent 73ae756 commit c022c6e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion includes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function includes (array, searchElement, fromIndex) {
fromIndex = 0;
}
if (Array.prototype.includes) {
return array.includes(searchElement, fromIndex);
return Array.prototype.includes.call(array, searchElement, fromIndex);
} else {
// ES7 Array.prototype.includes polyfill (modified)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Browser_compatibility
Expand Down
57 changes: 54 additions & 3 deletions test/test-includes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,41 @@
*/
'use strict';

var Code = require('code');
var Lab = require('lab');
var sinon = require('sinon');

var lab = exports.lab = Lab.script();

var afterEach = lab.afterEach;
var beforeEach = lab.beforeEach;
var describe = lab.describe;
var it = lab.it;
var Code = require('code');
var expect = Code.expect;
var it = lab.it;

var includes = require('../includes');

describe('includes', function () {

describe('stubbed Array.prototype.includes', function () {
beforeEach(function (done) {
Array.prototype.includes = sinon.stub().returns(true);
done();
});
afterEach(function (done) {
delete Array.prototype.includes;
done();
});
it('should defer to ES7 includes if defined', function (done) {
var result = includes([1, 2, 3], 2);
expect(result).to.equal(true);
expect(Array.prototype.includes.callCount).to.equal(1);
done();
});
});

describe('without fromIndex', function () {
var someObj = {};
var tests = [
// array, searchItem, expectedResult
[[1, 2, 3], 'a', false],
Expand All @@ -25,11 +48,23 @@ describe('includes', function () {
[[1, 2, 3], 4, false],
[[1, 2, 3], 0, false],
[[1, 2, 3], -2, false],
[['a', 'b', 'c'], 'b', true],
[['a', 'b', 'c'], 'e', false],
[['a', 'b', someObj, 'c'], someObj, true],
[['a', 'b', 'c'], someObj, false],
[[], 2, false],
[[], '', false],
[[], 0, false],
[[NaN, 0, 0], NaN , true],
[[NaN, 0, 0], NaN, true],
[[NaN, 0, 0], 0, true],
[[-0, 0, 0], 0, true],
[[-0, 0, 0], -0, true],
[[0, 0, 0], -0, true],
[[0, NaN, 0, 0], NaN, true],
[[0, NaN, 0, 0], 0, true],
[[0, -0, 0, 0], 0, true],
[[0, -0, 0, 0], -0, true],
[[0, 0, 0, 0], -0, true]
];

describe('compose', function () {
Expand All @@ -52,9 +87,11 @@ describe('includes', function () {
done();
});
});

});

describe('with fromIndex', function () {
var someObj = {};
var tests = [
// array, searchItem, fromIndex, expectedResult
[[1, 2, 3], 'a', 1, false],
Expand All @@ -65,11 +102,25 @@ describe('includes', function () {
[[1, 2, 3], 4, 0, false],
[[1, 2, 3], 0, 0, false],
[[1, 2, 3], -2, 0, false],
[['a', 'b', 'c'], 'b', 0, true],
[['a', 'b', 'c'], 'e', 0, false],
[['a', 'b', someObj, 'c'], someObj, 0, true],
[['a', 'b', 'c'], someObj, 0, false],
[[], 2, 1000, false],
[[], '', 1000, false],
[[], 0, 1000, false],
[[NaN, 0, 0], NaN, 0, true],
[[-0, 0, 0], 0, 0, true],
[[NaN, 0, 0], NaN, 0, true],
[[NaN, 0, 0], 0, 0, true],
[[-0, 0, 0], 0, 0, true],
[[-0, 0, 0], -0, 0, true],
[[0, 0, 0], -0, 0, true],
[[0, NaN, 0, 0], NaN, 0, true],
[[0, NaN, 0, 0], 0, 0, true],
[[0, -0, 0, 0], 0, 0, true],
[[0, -0, 0, 0], -0, 0, true],
[[0, 0, 0, 0], -0, 0, true],
[[1, 2, 3, 4, 5], 4, 10, false],
[[1, 2, 3, 4, 5], 4, -10, true], // fromIndex < 0, cast to 0
[[1, 2, 3, 4, 5], 4, -3, true],
Expand Down

0 comments on commit c022c6e

Please sign in to comment.