From 0be3c6837d3730f8be82f64e5f65866a55f09099 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 24 Jun 2015 17:42:02 +0100 Subject: [PATCH] Add utils UTs. Remove unused functions. --- lib/utils.js | 14 ----- spec/unit/utils.spec.js | 135 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 14 deletions(-) create mode 100644 spec/unit/utils.spec.js diff --git a/lib/utils.js b/lib/utils.js index 51d305cc2..f7b1547b8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -215,20 +215,6 @@ module.exports.checkObjectHasNoAdditionalKeys = function(obj, allowedKeys) { } }; -/** - * Assigns all the properties in src to dst. If these properties are Objects, - * then both src and dst will refer to the same thing. - * @param {Object} src The object to copy properties from. - * @param {Object} dst The object to write properties to. - */ -module.exports.shallowCopy = function(src, dst) { - for (var i in src) { - if (src.hasOwnProperty(i)) { - dst[i] = src[i]; - } - } -}; - /** * Deep copy the given object. The object MUST NOT have circular references and * MUST NOT have functions. diff --git a/spec/unit/utils.spec.js b/spec/unit/utils.spec.js new file mode 100644 index 000000000..d8be6ceb1 --- /dev/null +++ b/spec/unit/utils.spec.js @@ -0,0 +1,135 @@ +"use strict"; +var utils = require("../../lib/utils"); +var testUtils = require("../test-utils"); + +describe("utils", function() { + beforeEach(function() { + testUtils.beforeEach(this); + }); + + describe("encodeParams", function() { + it("should url encode and concat with &s", function() { + var params = { + foo: "bar", + baz: "beer@" + }; + expect(utils.encodeParams(params)).toEqual( + "foo=bar&baz=beer%40" + ); + }); + }); + + describe("encodeUri", function() { + it("should replace based on object keys and url encode", function() { + var path = "foo/bar/%something/%here"; + var vals = { + "%something": "baz", + "%here": "beer@" + }; + expect(utils.encodeUri(path, vals)).toEqual( + "foo/bar/baz/beer%40" + ); + }); + }); + + describe("forEach", function() { + it("should be invoked for each element", function() { + var arr = []; + utils.forEach([55, 66, 77], function(element) { + arr.push(element); + }); + expect(arr).toEqual([55, 66, 77]); + }); + }); + + describe("findElement", function() { + it("should find only 1 element if there is a match", function() { + var matchFn = function() { return true; }; + var arr = [55, 66, 77]; + expect(utils.findElement(arr, matchFn)).toEqual(55); + }); + it("should be able to find in reverse order", function() { + var matchFn = function() { return true; }; + var arr = [55, 66, 77]; + expect(utils.findElement(arr, matchFn, true)).toEqual(77); + }); + it("should find nothing if the function never returns true", function() { + var matchFn = function() { return false; }; + var arr = [55, 66, 77]; + expect(utils.findElement(arr, matchFn)).toBeFalsy(); + }); + }); + + describe("removeElement", function() { + it("should remove only 1 element if there is a match", function() { + var matchFn = function() { return true; }; + var arr = [55, 66, 77]; + utils.removeElement(arr, matchFn); + expect(arr).toEqual([66, 77]); + }); + it("should be able to remove in reverse order", function() { + var matchFn = function() { return true; }; + var arr = [55, 66, 77]; + utils.removeElement(arr, matchFn, true); + expect(arr).toEqual([55, 66]); + }); + it("should remove nothing if the function never returns true", function() { + var matchFn = function() { return false; }; + var arr = [55, 66, 77]; + utils.removeElement(arr, matchFn); + expect(arr).toEqual(arr); + }); + }); + + describe("isFunction", function() { + it("should return true for functions", function() { + expect(utils.isFunction([])).toBe(false); + expect(utils.isFunction([5, 3, 7])).toBe(false); + expect(utils.isFunction()).toBe(false); + expect(utils.isFunction(null)).toBe(false); + expect(utils.isFunction({})).toBe(false); + expect(utils.isFunction("foo")).toBe(false); + expect(utils.isFunction(555)).toBe(false); + + expect(utils.isFunction(function() {})).toBe(true); + var s = { foo: function() {} }; + expect(utils.isFunction(s.foo)).toBe(true); + }); + }); + + describe("isArray", function() { + it("should return true for arrays", function() { + expect(utils.isArray([])).toBe(true); + expect(utils.isArray([5, 3, 7])).toBe(true); + + expect(utils.isArray()).toBe(false); + expect(utils.isArray(null)).toBe(false); + expect(utils.isArray({})).toBe(false); + expect(utils.isArray("foo")).toBe(false); + expect(utils.isArray(555)).toBe(false); + expect(utils.isArray(function() {})).toBe(false); + }); + }); + + describe("checkObjectHasKeys", function() { + it("should throw for missing keys", function() { + expect(function() { utils.checkObjectHasKeys({}, ["foo"]); }).toThrow(); + expect(function() { utils.checkObjectHasKeys({ + foo: "bar" + }, ["foo"]); }).not.toThrow(); + }); + }); + + describe("checkObjectHasNoAdditionalKeys", function() { + it("should throw for extra keys", function() { + expect(function() { utils.checkObjectHasNoAdditionalKeys({ + foo: "bar", + baz: 4 + }, ["foo"]); }).toThrow(); + + expect(function() { utils.checkObjectHasNoAdditionalKeys({ + foo: "bar" + }, ["foo"]); }).not.toThrow(); + }); + }); +});