mirror of
https://github.com/datahub-project/datahub.git
synced 2025-10-17 11:58:10 +00:00
adds tests for array convenience functions
This commit is contained in:
parent
d1ab20a499
commit
af60c21e5a
22
wherehows-web/tests/helpers/arrays/functions.ts
Normal file
22
wherehows-web/tests/helpers/arrays/functions.ts
Normal file
@ -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<number>} the list of random numbers
|
||||
*/
|
||||
const xRandomNumbers = (length: number): Array<number> => 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 };
|
47
wherehows-web/tests/unit/utils/array-test.js
Normal file
47
wherehows-web/tests/unit/utils/array-test.js
Normal file
@ -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`);
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user