diff --git a/wherehows-web/tests/helpers/arrays/functions.ts b/wherehows-web/tests/helpers/arrays/functions.ts new file mode 100644 index 0000000000..7259bf22a2 --- /dev/null +++ b/wherehows-web/tests/helpers/arrays/functions.ts @@ -0,0 +1,22 @@ +/** + * Generates a list of random floating point numbers between 0 and 100 exclusive + * @param {number} length the number of numbers to generate + * @return {Array} the list of random numbers + */ +const xRandomNumbers = (length: number): Array => Array.from(new Array(length)).map(() => Math.random() * 100); + +/** + * Converts a number to a string + * @param {number} num the number to convert + * @return {string} the number as a string + */ +const numToString = (num: number): string => num + ''; + +/** + * Checks if a value is a string + * @param {any} param the value to check + * @return {boolean} true, if the value is a string + */ +const isAString = (param: any): boolean => typeof param === 'string'; + +export { xRandomNumbers, numToString, isAString }; diff --git a/wherehows-web/tests/unit/utils/array-test.js b/wherehows-web/tests/unit/utils/array-test.js new file mode 100644 index 0000000000..24ca8182d1 --- /dev/null +++ b/wherehows-web/tests/unit/utils/array-test.js @@ -0,0 +1,47 @@ +import { module, test } from 'qunit'; +import { arrayMap, arrayFilter, isListUnique } from 'wherehows-web/utils/array'; +import { xRandomNumbers, numToString, isAString } from 'wherehows-web/tests/helpers/arrays/functions'; + +module('Unit | Utility | array'); +const assertionLength = 10; + +test('arrayMap is a function', function(assert) { + assert.ok(typeof arrayMap === 'function', 'module exports an array map function'); +}); + +test('arrayMap creates a valid map', function(assert) { + assert.expect(assertionLength); + + const numbers = xRandomNumbers(assertionLength); + const numbersToString = arrayMap(numToString); + const strings = numbersToString(numbers); + + strings.forEach(string => assert.ok(typeof string === 'string')); +}); + +test('arrayFilter is a function', function(assert) { + assert.ok(typeof arrayFilter === 'function', 'module exports an array filter function'); +}); + +test('arrayFilter creates a valid filter', function(assert) { + assert.expect(1); + + const numbers = xRandomNumbers(assertionLength); + const getStrings = arrayFilter(isAString); + + assert.ok(getStrings(numbers).length === 0, 'returns a empty list of strings'); +}); + +test('isListUnique is a function', function(assert) { + assert.ok(typeof isListUnique === 'function', 'module exports a isListUnique function'); +}); + +test('isListUnique correctly tests uniqueness of a list', function(assert) { + assert.expect(2); + + const listWithDuplicateNumbers = [1, 2, 3, 4, 1]; + const listWithoutDuplicateNumbers = [1, 2, 3, 4, 5]; + + assert.notOk(isListUnique(listWithDuplicateNumbers), `${listWithDuplicateNumbers} has duplicates`); + assert.ok(isListUnique(listWithoutDuplicateNumbers), `${listWithoutDuplicateNumbers} has no duplicates`); +});