@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
95 lines (94 loc) • 4.18 kB
JavaScript
import { formatPercentage, formatQuantity, containsAll, containsAny, range, sample } from '../../utils';
describe('formatPercentage', () => {
it('formats percentage when decimal is undefined', () => {
expect(formatPercentage('0')).toBe('0');
expect(formatPercentage('0.45')).toBe('0.45');
expect(formatPercentage('0.5')).toBe('0.5');
expect(formatPercentage('1')).toBe('1');
expect(formatPercentage('1.55')).toBe('1.55');
});
it('formats percentage with 0 decimal points', () => {
expect(formatPercentage('0', 0)).toBe('0');
expect(formatPercentage('0.45', 0)).toBe('0');
expect(formatPercentage('0.5', 0)).toBe('1');
expect(formatPercentage('1', 0)).toBe('1');
expect(formatPercentage('1.55', 0)).toBe('2');
});
it('formats percentage with 2 decimal points', () => {
expect(formatPercentage('0', 2)).toBe('0.00');
expect(formatPercentage('0.45', 2)).toBe('0.45');
expect(formatPercentage('0.5', 2)).toBe('0.50');
expect(formatPercentage('1', 2)).toBe('1.00');
expect(formatPercentage('1.55', 2)).toBe('1.55');
});
});
describe('formatQuantity', () => {
it('formats quantity', () => {
expect(formatQuantity('0')).toBe('0');
expect(formatQuantity('0.45')).toBe('0');
expect(formatQuantity('0.5')).toBe('1');
expect(formatQuantity('1')).toBe('1');
expect(formatQuantity('1.55')).toBe('2');
});
});
describe('containsAny', () => {
it('returns true when array contains any of the items', () => {
expect(containsAny(['a', 'b', 'c'], ['a', 'd'])).toBe(true);
expect(containsAny(['a', 'b', 'c'], ['d', 'e', 'f'])).toBe(false);
expect(containsAny(['a', 'b', 'c'], ['a', 'b', 'c'])).toBe(true);
expect(containsAny(['a', 'b', 'c'], ['a', 'b', 'c', 'd'])).toBe(true);
});
it('returns false when array does not contain any of the items', () => {
expect(containsAny(['a', 'b', 'c'], ['d', 'e'])).toBe(false);
expect(containsAny(['a', 'b', 'c'], ['d', 'e', 'f'])).toBe(false);
});
it('returns false when array is empty', () => {
expect(containsAny([], ['a', 'b', 'c'])).toBe(false);
});
it('returns true when items is empty', () => {
expect(containsAny(['a', 'b', 'c'], [])).toBe(true);
expect(containsAny([], [])).toBe(true);
});
});
describe('containsAll', () => {
it('returns true when array contains all of the items', () => {
expect(containsAll(['a', 'b', 'c'], ['a', 'b'])).toBe(true);
expect(containsAll(['a', 'b', 'c'], ['a', 'b', 'c'])).toBe(true);
});
it('returns false when array does not contain all of the items', () => {
expect(containsAll(['a', 'b', 'c'], ['a', 'e'])).toBe(false);
expect(containsAll(['a', 'b', 'c'], ['d', 'e', 'f'])).toBe(false);
expect(containsAll(['a', 'b', 'c'], ['a', 'b', 'c', 'd'])).toBe(false);
});
it('returns false when array is empty', () => {
expect(containsAll([], ['a', 'b', 'c'])).toBe(false);
});
it('returns true when items is empty', () => {
expect(containsAll(['a', 'b', 'c'], [])).toBe(true);
expect(containsAll([], [])).toBe(true);
});
});
describe('range', () => {
it('returns numbers array', () => {
expect(range(5)).toStrictEqual([0, 1, 2, 3, 4]);
expect(range(1, 5)).toStrictEqual([1, 2, 3, 4]);
expect(range(0, 20, 5)).toStrictEqual([0, 5, 10, 15]);
expect(range(0, -4, -1)).toStrictEqual([0, -1, -2, -3]);
});
});
describe('sample', () => {
it('returns undefined for an empty array', () => {
expect(sample([])).toBeUndefined();
});
it('returns a random element from the array', () => {
const numbers = [1, 2, 3, 4, 5];
const result = sample(numbers);
expect(numbers.includes(result)).toBe(true);
});
it('returns only one of the elements of the array', () => {
const numbers = [1, 2, 3, 4, 5];
const result = sample(numbers);
expect(numbers).toContain(result);
expect(numbers.indexOf(result)).not.toBe(-1);
});
});