Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #75 from NuCivic/more-utils-tests
Browse files Browse the repository at this point in the history
Add more tests coverage for src/utils.js.
  • Loading branch information
topicus authored Apr 13, 2017
2 parents a4c3804 + 4153062 commit df1ce74
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 23 deletions.
6 changes: 0 additions & 6 deletions .babelrc

This file was deleted.

3 changes: 0 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ jobs:
working_directory: ~/react-nvd3
steps:
- checkout
- run: mkdir -p ~/react-nvd3/artifacts
- run: npm install
- run: ./node_modules/gulp/bin/gulp.js jest
- store_artifacts:
path: ~/react-nvd3/artifacts
5 changes: 2 additions & 3 deletions dist/react-nvd3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,6 @@ return /******/ (function(modules) { // webpackBootstrap

exports.includes = includes;
exports.negate = negate;
exports.filterObject = filterObject;
exports.pick = pick;
exports.without = without;
exports.isPlainObject = isPlainObject;
Expand Down Expand Up @@ -1399,7 +1398,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

/**
* It replace all the {type:'function', name: 'nameOffunction'}
* It replaces all the {type:'function', name: 'nameOffunction'}
* ocurrences in a give object by the functions stored
* in the {context} with the name {name}
* @param {Object} o The original object to be patched
Expand Down Expand Up @@ -1454,7 +1453,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

function isCallable(value) {
return value && typeof value === 'function';
return Boolean(value && typeof value === 'function');
}

/***/ },
Expand Down
2 changes: 1 addition & 1 deletion dist/react-nvd3.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,11 @@
"peerDependencies": {
"react": ">=0.14.0",
"react-dom": ">=0.14.0"
},
"babel": {
"presets": [
"es2015",
"react"
]
}
}
8 changes: 4 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function negate(f) {
* @param {Function} predicate
* @return {Object}
*/
export function filterObject(obj, keys, predicate) {
function filterObject(obj, keys, predicate) {
let result = {};
let ks = Object.keys(obj);

Expand Down Expand Up @@ -79,7 +79,7 @@ export function isPlainObject(obj){
}

/**
* It replace all the {type:'function', name: 'nameOffunction'}
* It replaces all the {type:'function', name: 'nameOffunction'}
* ocurrences in a give object by the functions stored
* in the {context} with the name {name}
* @param {Object} o The original object to be patched
Expand Down Expand Up @@ -132,5 +132,5 @@ export function propsByPrefix(prefix, props) {
}

export function isCallable(value) {
return value && typeof value === 'function';
}
return Boolean(value && typeof value === 'function');
}
137 changes: 131 additions & 6 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import {
includes,
negate
negate,
pick,
without,
isPlainObject,
bindFunctions,
getValueFunction,
isCallable,
} from '../src/utils.js';

describe("The utils.includes function", () => {
describe("Function utils.includes(array, item)", () => {
beforeEach(() => {
self.TestData = [ 0, 1, 2, 3];
});
Expand All @@ -13,17 +19,16 @@ describe("The utils.includes function", () => {
expect(includes(self.TestData, 0)).toBe.true;
});

it('should locate a value if it does not exists', () => {
let TestData = [ 0, 1, 2, 3];
it('should not locate a value if it does not exists', () => {
expect(includes(self.TestData, 'zero')).toBe.false;
});

it('should Throw if non array is checked', () => {
it('should Throw TypeError', () => {
expect(includes).toThrow(TypeError);
});
});

describe("The utils.negate function", () => {
describe("Function utils.negate(f)", () => {
it('should make a positive producer be a negative producer', () => {
let negator = negate(function(a) {return true;});
expect(negator('a')).toBe.false;
Expand All @@ -34,3 +39,123 @@ describe("The utils.negate function", () => {
expect(negator('a')).toBe.true;
});
});

describe("Function utils.pick(obj, keys)", () => {
it('should return empty if empty is passed', () => {
expect(pick({}, ["a", "b"])).toEqual({});
});

it('should return empty object if no keys are passed', () => {
expect(pick({a: 1, b: 2}, [])).toEqual({});
});

it('should return only picked keys', () => {
expect(pick({a: 1, b: 2}, ["a"])).toEqual({a: 1});
});

it('should return object if all keys are passed', () => {
expect(pick({a: 1, b: 2}, ["a", "b", "c"])).toEqual({a: 1, b: 2});
});
});

describe("Function utils.without(obj, keys)", () => {
it('should return empty if empty is passed', () => {
expect(without({}, ["a", "b"])).toEqual({});
});

it('should return empty object if all keys are passed', () => {
expect(without({a: 1, b: 2}, ["a", "b", "c"])).toEqual({});
});

it('should return only unpicked keys', () => {
expect(without({a: 1, b: 2}, ["a"])).toEqual({b: 2});
});

it('should return object if all not keys passed', () => {
expect(without({a: 1, b: 2}, ["c"])).toEqual({a: 1, b: 2});
});
});

describe("Function utils.isPlainObject(obj)", () => {
it("should verify Objects as plain objects", () => {
let myObject = new Object();
expect(isPlainObject(myObject)).toBe(true);
});

it("should verify {} as a plain objects", () => {
expect(isPlainObject({a: 1, b: {}})).toBe(true);
});

it("should not verify String as a plain objects", () => {
let myString = new String("Hello");
expect(isPlainObject(myString)).toBe(false);
});
});

describe("Function utils.bindFunctions(obj, handlers)", () => {
it("It should handle empty object and empty handlers", () => {
expect(bindFunctions({}, {})).toEqual({});
});

it("It should handle empty object with any handler", () => {
expect(bindFunctions({}, {a: () => {}})).toEqual({});
});

it("It should bind functions to their references", () => {
let data = {a: {name: "a", type: "function"}};
let handlers = {a: () => {return true}};

expect(bindFunctions(data, handlers).a()).toBe(true);
});

it("It should bind functions to a list of references", () => {
let data = [{a: {name: "a", type: "function"}}];
let handlers = {a: () => {return true}};

expect(bindFunctions(data, handlers)[0].a()).toBe(true);
});

it("It should bind functions to internal references", () => {
let data = [{a: {a: {name: "a", type: "function"}}}];
let handlers = {a: () => {return true}};

expect(bindFunctions(data, handlers)[0].a.a()).toBe(true);
});
});

describe("Function utils.getValueFunction(v, _default)", () => {
it("It should ignore arguments that are functions", () => {
let f = (a) => {return a * a;}
let g = getValueFunction(f)
expect(g(2)).toBe(4);
});

it("It should return getter function for non function arguments", () => {
let a = "name"
let f = getValueFunction(a)
let testObject = {name: "react-nvd3"}
expect(f(testObject)).toBe("react-nvd3");
});

it("It should try the _default as a backup getter if first does not work", () => {
let a = "_x"
let b = "name"
let f = getValueFunction(a, b)
let testObject = {name: "react-nvd3"}
expect(f(testObject)).toBe("react-nvd3");
});
});

describe("Function utils.isCallable(value)", () => {
it("should determine functions are callable", () => {
let f = () => {return 1};
expect(isCallable(f)).toBe(true);
});

it("should determine non functions are not callable", () => {
expect(isCallable(undefined)).toBe(false);
expect(isCallable(1)).toBe(false);
expect(isCallable({})).toBe(false);
expect(isCallable("")).toBe(false);
});
});

0 comments on commit df1ce74

Please sign in to comment.