From 06d7e0422018215b883f5c8285541199354cf103 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 28 Jan 2018 16:28:54 +0200 Subject: [PATCH] Added some tests --- snippets/bindAll.md | 4 +- test/anagrams/anagrams.test.js | 4 +- test/ary/ary.test.js | 4 +- test/attempt/attempt.js | 8 +++ test/attempt/attempt.test.js | 15 ++++ test/bind/bind.test.js | 8 ++- test/bindAll/bindAll.js | 4 +- test/bindAll/bindAll.test.js | 10 ++- test/bindKey/bindKey.test.js | 10 ++- test/byteSize/byteSize.test.js | 5 +- test/debounce/debounce.js | 10 +++ test/debounce/debounce.test.js | 13 ++++ test/overArgs/overArgs.js | 2 + test/overArgs/overArgs.test.js | 13 ++++ test/rearg/rearg.js | 8 +++ test/rearg/rearg.test.js | 13 ++++ test/testlog | 125 +++++++++++++++++++++++++++------ test/throttle/throttle.js | 21 ++++++ test/throttle/throttle.test.js | 13 ++++ 19 files changed, 257 insertions(+), 33 deletions(-) create mode 100644 test/attempt/attempt.js create mode 100644 test/attempt/attempt.test.js create mode 100644 test/debounce/debounce.js create mode 100644 test/debounce/debounce.test.js create mode 100644 test/overArgs/overArgs.js create mode 100644 test/overArgs/overArgs.test.js create mode 100644 test/rearg/rearg.js create mode 100644 test/rearg/rearg.test.js create mode 100644 test/throttle/throttle.js create mode 100644 test/throttle/throttle.test.js diff --git a/snippets/bindAll.md b/snippets/bindAll.md index 26093013c..5a38d2439 100644 --- a/snippets/bindAll.md +++ b/snippets/bindAll.md @@ -6,8 +6,8 @@ Use `Array.forEach()` to return a `function` that uses `Function.apply()` to app const bindAll = (obj, ...fns) => fns.forEach( fn => - (obj[fn] = function() { - return fn.apply(obj); + (f = obj[fn], obj[fn] = function() { + return f.apply(obj); }) ); ``` diff --git a/test/anagrams/anagrams.test.js b/test/anagrams/anagrams.test.js index 4318f4c46..a7987d63f 100644 --- a/test/anagrams/anagrams.test.js +++ b/test/anagrams/anagrams.test.js @@ -6,9 +6,11 @@ test('Testing anagrams', (t) => { //Please go to https://github.com/substack/tape t.true(typeof anagrams === 'function', 'anagrams is a Function'); t.deepEqual(anagrams('abc'), ['abc','acb','bac','bca','cab','cba'], "Generates all anagrams of a string"); + t.deepEqual(anagrams('a'), ['a'], "Works for single-letter strings"); + t.deepEqual(anagrams(''), [''], "Works for empty strings"); //t.deepEqual(anagrams(args..), 'Expected'); //t.equal(anagrams(args..), 'Expected'); //t.false(anagrams(args..), 'Expected'); //t.throws(anagrams(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/ary/ary.test.js b/test/ary/ary.test.js index 445b40618..065b18a96 100644 --- a/test/ary/ary.test.js +++ b/test/ary/ary.test.js @@ -5,9 +5,11 @@ test('Testing ary', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof ary === 'function', 'ary is a Function'); + const firstTwoMax = ary(Math.max, 2); + t.deepEquals([[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)), [6, 8, 10], 'Discards arguments with index >=n') //t.deepEqual(ary(args..), 'Expected'); //t.equal(ary(args..), 'Expected'); //t.false(ary(args..), 'Expected'); //t.throws(ary(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/attempt/attempt.js b/test/attempt/attempt.js new file mode 100644 index 000000000..6d079c71e --- /dev/null +++ b/test/attempt/attempt.js @@ -0,0 +1,8 @@ +const attempt = (fn, ...args) => { +try { +return fn(args); +} catch (e) { +return e instanceof Error ? e : new Error(e); +} +}; +module.exports = attempt \ No newline at end of file diff --git a/test/attempt/attempt.test.js b/test/attempt/attempt.test.js new file mode 100644 index 000000000..13ae65686 --- /dev/null +++ b/test/attempt/attempt.test.js @@ -0,0 +1,15 @@ +const test = require('tape'); +const attempt = require('./attempt.js'); + +test('Testing attempt', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof attempt === 'function', 'attempt is a Function'); + t.equals(attempt(() => 0), 0, 'Returns a value'); + t.true(attempt(() => {throw new Error()}) instanceof Error, 'Returns an error'); + //t.deepEqual(attempt(args..), 'Expected'); + //t.equal(attempt(args..), 'Expected'); + //t.false(attempt(args..), 'Expected'); + //t.throws(attempt(args..), 'Expected'); + t.end(); +}); diff --git a/test/bind/bind.test.js b/test/bind/bind.test.js index 6ad403682..e3ba1054c 100644 --- a/test/bind/bind.test.js +++ b/test/bind/bind.test.js @@ -5,9 +5,15 @@ test('Testing bind', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof bind === 'function', 'bind is a Function'); + function greet(greeting, punctuation) { + return greeting + ' ' + this.user + punctuation; + } + const freddy = { user: 'fred' }; + const freddyBound = bind(greet, freddy); + t.equals(freddyBound('hi', '!'),'hi fred!', 'Binds to an object context'); //t.deepEqual(bind(args..), 'Expected'); //t.equal(bind(args..), 'Expected'); //t.false(bind(args..), 'Expected'); //t.throws(bind(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/bindAll/bindAll.js b/test/bindAll/bindAll.js index cf66ad583..507a4a9bc 100644 --- a/test/bindAll/bindAll.js +++ b/test/bindAll/bindAll.js @@ -1,8 +1,8 @@ const bindAll = (obj, ...fns) => fns.forEach( fn => -(obj[fn] = function() { -return fn.apply(obj); +(f = obj[fn], obj[fn] = function() { +return f.apply(obj); }) ); module.exports = bindAll \ No newline at end of file diff --git a/test/bindAll/bindAll.test.js b/test/bindAll/bindAll.test.js index 513eae230..90c9e23a8 100644 --- a/test/bindAll/bindAll.test.js +++ b/test/bindAll/bindAll.test.js @@ -5,9 +5,17 @@ test('Testing bindAll', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof bindAll === 'function', 'bindAll is a Function'); + var view = { + label: 'docs', + 'click': function() { + return 'clicked ' + this.label; + } + } + bindAll(view, 'click'); + t.equal(view.click(), 'clicked docs', 'Binds to an object context') //t.deepEqual(bindAll(args..), 'Expected'); //t.equal(bindAll(args..), 'Expected'); //t.false(bindAll(args..), 'Expected'); //t.throws(bindAll(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/bindKey/bindKey.test.js b/test/bindKey/bindKey.test.js index 36619ebcd..2aed1fb1e 100644 --- a/test/bindKey/bindKey.test.js +++ b/test/bindKey/bindKey.test.js @@ -5,9 +5,17 @@ test('Testing bindKey', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof bindKey === 'function', 'bindKey is a Function'); + const freddy = { + user: 'fred', + greet: function(greeting, punctuation) { + return greeting + ' ' + this.user + punctuation; + } + }; + const freddyBound = bindKey(freddy, 'greet'); + t.equal(freddyBound('hi', '!'), 'hi fred!', 'Binds function to an object context'); //t.deepEqual(bindKey(args..), 'Expected'); //t.equal(bindKey(args..), 'Expected'); //t.false(bindKey(args..), 'Expected'); //t.throws(bindKey(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/byteSize/byteSize.test.js b/test/byteSize/byteSize.test.js index 32718fada..b79b83889 100644 --- a/test/byteSize/byteSize.test.js +++ b/test/byteSize/byteSize.test.js @@ -5,6 +5,9 @@ test('Testing byteSize', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof byteSize === 'function', 'byteSize is a Function'); + // Blob is not part of Node apparently? + //t.equal(byteSize('Hello World'), 11, 'Works for text'); + //t.equal(byteSize('😀'), 4, 'Works for emojis'); // Works only in browser // t.equal(byteSize('Hello World'), 11, "Returns the length of a string in bytes"); //t.deepEqual(byteSize(args..), 'Expected'); @@ -12,4 +15,4 @@ test('Testing byteSize', (t) => { //t.false(byteSize(args..), 'Expected'); //t.throws(byteSize(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/debounce/debounce.js b/test/debounce/debounce.js new file mode 100644 index 000000000..430bfeaed --- /dev/null +++ b/test/debounce/debounce.js @@ -0,0 +1,10 @@ +const debounce = (fn, wait = 0) => { +let inDebounce; +return function() { +const context = this, +args = arguments; +clearTimeout(inDebounce); +inDebounce = setTimeout(() => fn.apply(context, args), wait); +}; +}; +module.exports = debounce \ No newline at end of file diff --git a/test/debounce/debounce.test.js b/test/debounce/debounce.test.js new file mode 100644 index 000000000..f1a35323c --- /dev/null +++ b/test/debounce/debounce.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const debounce = require('./debounce.js'); + +test('Testing debounce', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof debounce === 'function', 'debounce is a Function'); + //t.deepEqual(debounce(args..), 'Expected'); + //t.equal(debounce(args..), 'Expected'); + //t.false(debounce(args..), 'Expected'); + //t.throws(debounce(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/overArgs/overArgs.js b/test/overArgs/overArgs.js new file mode 100644 index 000000000..41250eb89 --- /dev/null +++ b/test/overArgs/overArgs.js @@ -0,0 +1,2 @@ +const overArgs = (fn, transforms) => (...args) => fn(...args.map((val, i) => transforms[i](val))); +module.exports = overArgs \ No newline at end of file diff --git a/test/overArgs/overArgs.test.js b/test/overArgs/overArgs.test.js new file mode 100644 index 000000000..81c194c90 --- /dev/null +++ b/test/overArgs/overArgs.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const overArgs = require('./overArgs.js'); + +test('Testing overArgs', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof overArgs === 'function', 'overArgs is a Function'); + //t.deepEqual(overArgs(args..), 'Expected'); + //t.equal(overArgs(args..), 'Expected'); + //t.false(overArgs(args..), 'Expected'); + //t.throws(overArgs(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/rearg/rearg.js b/test/rearg/rearg.js new file mode 100644 index 000000000..b2f97244c --- /dev/null +++ b/test/rearg/rearg.js @@ -0,0 +1,8 @@ +const rearg = (fn, indexes) => (...args) => +fn( +...args.reduce( +(acc, val, i) => ((acc[indexes.indexOf(i)] = val), acc), +Array.from({ length: indexes.length }) +) +); +module.exports = rearg \ No newline at end of file diff --git a/test/rearg/rearg.test.js b/test/rearg/rearg.test.js new file mode 100644 index 000000000..9139d3f64 --- /dev/null +++ b/test/rearg/rearg.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const rearg = require('./rearg.js'); + +test('Testing rearg', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof rearg === 'function', 'rearg is a Function'); + //t.deepEqual(rearg(args..), 'Expected'); + //t.equal(rearg(args..), 'Expected'); + //t.false(rearg(args..), 'Expected'); + //t.throws(rearg(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/testlog b/test/testlog index 56314eed1..9c0f4f3cb 100644 --- a/test/testlog +++ b/test/testlog @@ -1,4 +1,4 @@ -Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) +Test log for: Sun Jan 28 2018 16:28:45 GMT+0200 (GTB Standard Time) > 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code > tape test/**/*.test.js | tap-spec @@ -8,6 +8,8 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) √ anagrams is a Function √ Generates all anagrams of a string + √ Works for single-letter strings + √ Works for empty strings Testing arrayToHtmlList @@ -17,6 +19,7 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) Testing ary √ ary is a Function + √ Discards arguments with index >=n Testing atob @@ -24,6 +27,12 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) √ atob("Zm9vYmFy") equals "foobar" √ atob("Z") returns "" + Testing attempt + + √ attempt is a Function + √ Returns a value + √ Returns an error + Testing average √ average is a Function @@ -52,14 +61,17 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) Testing bind √ bind is a Function + √ Binds to an object context Testing bindAll √ bindAll is a Function + √ Binds to an object context Testing bindKey √ bindKey is a Function + √ Binds function to an object context Testing bottomVisible @@ -202,6 +214,10 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) √ curries a Math.pow √ curries a Math.min + Testing debounce + + √ debounce is a Function + Testing decapitalize √ decapitalize is a Function @@ -959,6 +975,10 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) √ over is a Function + Testing overArgs + + √ overArgs is a Function + Testing palindrome √ palindrome is a Function @@ -1082,6 +1102,10 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) √ README is a Function + Testing rearg + + √ rearg is a Function + Testing redirect √ redirect is a Function @@ -1125,7 +1149,16 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) Testing round √ round is a Function - √ Rounds a number to a specified amount of digits. + √ round(1.005, 2) returns 1.01 + √ round(123.3423345345345345344, 11) returns 123.34233453453 + √ round(3.342, 11) returns 3.342 + √ round(1.005) returns 1 + √ round([1.005, 2]) returns NaN + √ round(string) returns NaN + √ round() returns NaN + √ round(132, 413, 4134) returns NaN + √ round({a: 132}, 413) returns NaN + √ round(123.3423345345345345344, 11) takes less than 2s to run Testing runAsync @@ -1295,6 +1328,10 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) √ takeWhile is a Function + Testing throttle + + √ throttle is a Function + Testing times √ times is a Function @@ -1306,10 +1343,15 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) Testing toCamelCase √ toCamelCase is a Function - √ Converts a string to camelCase - √ Converts a string to camelCase - √ Converts a string to camelCase - √ Converts a string to camelCase + √ toCamelCase('some_database_field_name') returns someDatabaseFieldName + √ toCamelCase('Some label that needs to be camelized') returns someLabelThatNeedsToBeCamelized + √ toCamelCase('some-javascript-property') return someJavascriptProperty + √ toCamelCase('some-mixed_string with spaces_underscores-and-hyphens') returns someMixedStringWithSpacesUnderscoresAndHyphens + √ toCamelCase() throws a error + √ toCamelCase([]) throws a error + √ toCamelCase({}) throws a error + √ toCamelCase(123) throws a error + √ toCamelCase(some-mixed_string with spaces_underscores-and-hyphens) takes less than 2s to run Testing toDecimalMark @@ -1323,10 +1365,15 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) Testing toKebabCase √ toKebabCase is a Function - √ string converts to snake case - √ string converts to snake case - √ string converts to snake case - √ string converts to snake case + √ toKebabCase('camelCase') returns camel-case + √ toKebabCase('some text') returns some-text + √ toKebabCase('some-mixed-string With spaces-underscores-and-hyphens') returns some-mixed-string-with-spaces-underscores-and-hyphens + √ toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') returns i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-some-xml-and-html + √ toKebabCase() return undefined + √ toKebabCase([]) throws an error + √ toKebabCase({}) throws an error + √ toKebabCase(123) throws an error + √ toKebabCase(IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML) takes less than 2s to run Testing tomorrow @@ -1343,19 +1390,30 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) Testing toSafeInteger √ toSafeInteger is a Function + √ Number(toSafeInteger(3.2)) is a number √ Converts a value to a safe integer - √ Converts a value to a safe integer - √ Converts a value to a safe integer - √ Converts a value to a safe integer - √ Converts a value to a safe integer + √ toSafeInteger('4.2') returns 4 + √ toSafeInteger(4.6) returns 5 + √ toSafeInteger([]) returns 0 + √ isNaN(toSafeInteger([1.5, 3124])) is true + √ isNaN(toSafeInteger('string')) is true + √ isNaN(toSafeInteger({})) is true + √ isNaN(toSafeInteger()) is true + √ toSafeInteger(Infinity) returns 9007199254740991 + √ toSafeInteger(3.2) takes less than 2s to run Testing toSnakeCase √ toSnakeCase is a Function - √ string converts to snake case - √ string converts to snake case - √ string converts to snake case - √ string converts to snake case + √ toSnakeCase('camelCase') returns camel_case + √ toSnakeCase('some text') returns some_text + √ toSnakeCase('some-mixed_string With spaces_underscores-and-hyphens') returns some_mixed_string_with_spaces_underscores_and_hyphens + √ toSnakeCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') returns i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_some_xml_and_html + √ toSnakeCase() returns undefined + √ toSnakeCase([]) throws an error + √ toSnakeCase({}) throws an error + √ toSnakeCase(123) throws an error + √ toSnakeCase(IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML) takes less than 2s to run Testing transform @@ -1387,7 +1445,17 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) Testing union √ union is a Function - √ Returns every element that exists in any of the two arrays once + √ union([1, 2, 3], [4, 3, 2]) returns [1, 2, 3, 4] + √ union('str', 'asd') returns [ 's', 't', 'r', 'a', 'd' ] + √ union([[], {}], [1, 2, 3]) returns [[], {}, 1, 2, 3] + √ union([], []) returns [] + √ union() throws an error + √ union(true, str) throws an error + √ union(false, true) throws an error + √ union(123, {}) throws an error + √ union([], {}) throws an error + √ union(undefined, null) throws an error + √ union([1, 2, 3], [4, 3, 2]) takes less than 2s to run Testing unionBy @@ -1400,7 +1468,18 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) Testing uniqueElements √ uniqueElements is a Function - √ Returns all unique values of an array + √ uniqueElements([1, 2, 2, 3, 4, 4, 5]) returns [1,2,3,4,5] + √ uniqueElements([1, 23, 53]) returns [1, 23, 53] + √ uniqueElements([true, 0, 1, false, false, undefined, null, '']) returns [true, 0, 1, false, false, undefined, null, ''] + √ uniqueElements() returns [] + √ uniqueElements(null) returns [] + √ uniqueElements(undefined) returns [] + √ uniqueElements('strt') returns ['s', 't', 'r'] + √ uniqueElements(1, 1, 2543, 534, 5) throws an error + √ uniqueElements({}) throws an error + √ uniqueElements(true) throws an error + √ uniqueElements(false) throws an error + √ uniqueElements([true, 0, 1, false, false, undefined, null]) takes less than 2s to run Testing untildify @@ -1524,8 +1603,8 @@ Test log for: Sun Jan 28 2018 14:28:04 GMT+0200 (GTB Standard Time) √ zipWith is a Function - total: 652 - passing: 652 - duration: 1.7s + total: 716 + passing: 716 + duration: 1.1s diff --git a/test/throttle/throttle.js b/test/throttle/throttle.js new file mode 100644 index 000000000..b26b6b74b --- /dev/null +++ b/test/throttle/throttle.js @@ -0,0 +1,21 @@ +const throttle = (fn, wait) => { +let inThrottle, lastFn, lastTime; +return function() { +const context = this, +args = arguments; +if (!inThrottle) { +fn.apply(context, args); +lastTime = Date.now(); +inThrottle = true; +} else { +clearTimeout(lastFn); +lastFn = setTimeout(function() { +if (Date.now() - lastTime >= wait) { +fn.apply(context, args); +lastTime = Date.now(); +} +}, wait - (Date.now() - lastTime)); +} +}; +}; +module.exports = throttle \ No newline at end of file diff --git a/test/throttle/throttle.test.js b/test/throttle/throttle.test.js new file mode 100644 index 000000000..4de75afdb --- /dev/null +++ b/test/throttle/throttle.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const throttle = require('./throttle.js'); + +test('Testing throttle', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof throttle === 'function', 'throttle is a Function'); + //t.deepEqual(throttle(args..), 'Expected'); + //t.equal(throttle(args..), 'Expected'); + //t.false(throttle(args..), 'Expected'); + //t.throws(throttle(args..), 'Expected'); + t.end(); +}); \ No newline at end of file