UNPKG

@open-tender/utils

Version:

A library of utils for use with Open Tender applications that utilize our cloud-based Order API.

97 lines (96 loc) 4.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("../../utils"); describe('formatPercentage', () => { it('formats percentage when decimal is undefined', () => { expect((0, utils_1.formatPercentage)('0')).toBe('0'); expect((0, utils_1.formatPercentage)('0.45')).toBe('0.45'); expect((0, utils_1.formatPercentage)('0.5')).toBe('0.5'); expect((0, utils_1.formatPercentage)('1')).toBe('1'); expect((0, utils_1.formatPercentage)('1.55')).toBe('1.55'); }); it('formats percentage with 0 decimal points', () => { expect((0, utils_1.formatPercentage)('0', 0)).toBe('0'); expect((0, utils_1.formatPercentage)('0.45', 0)).toBe('0'); expect((0, utils_1.formatPercentage)('0.5', 0)).toBe('1'); expect((0, utils_1.formatPercentage)('1', 0)).toBe('1'); expect((0, utils_1.formatPercentage)('1.55', 0)).toBe('2'); }); it('formats percentage with 2 decimal points', () => { expect((0, utils_1.formatPercentage)('0', 2)).toBe('0.00'); expect((0, utils_1.formatPercentage)('0.45', 2)).toBe('0.45'); expect((0, utils_1.formatPercentage)('0.5', 2)).toBe('0.50'); expect((0, utils_1.formatPercentage)('1', 2)).toBe('1.00'); expect((0, utils_1.formatPercentage)('1.55', 2)).toBe('1.55'); }); }); describe('formatQuantity', () => { it('formats quantity', () => { expect((0, utils_1.formatQuantity)('0')).toBe('0'); expect((0, utils_1.formatQuantity)('0.45')).toBe('0'); expect((0, utils_1.formatQuantity)('0.5')).toBe('1'); expect((0, utils_1.formatQuantity)('1')).toBe('1'); expect((0, utils_1.formatQuantity)('1.55')).toBe('2'); }); }); describe('containsAny', () => { it('returns true when array contains any of the items', () => { expect((0, utils_1.containsAny)(['a', 'b', 'c'], ['a', 'd'])).toBe(true); expect((0, utils_1.containsAny)(['a', 'b', 'c'], ['d', 'e', 'f'])).toBe(false); expect((0, utils_1.containsAny)(['a', 'b', 'c'], ['a', 'b', 'c'])).toBe(true); expect((0, utils_1.containsAny)(['a', 'b', 'c'], ['a', 'b', 'c', 'd'])).toBe(true); }); it('returns false when array does not contain any of the items', () => { expect((0, utils_1.containsAny)(['a', 'b', 'c'], ['d', 'e'])).toBe(false); expect((0, utils_1.containsAny)(['a', 'b', 'c'], ['d', 'e', 'f'])).toBe(false); }); it('returns false when array is empty', () => { expect((0, utils_1.containsAny)([], ['a', 'b', 'c'])).toBe(false); }); it('returns true when items is empty', () => { expect((0, utils_1.containsAny)(['a', 'b', 'c'], [])).toBe(true); expect((0, utils_1.containsAny)([], [])).toBe(true); }); }); describe('containsAll', () => { it('returns true when array contains all of the items', () => { expect((0, utils_1.containsAll)(['a', 'b', 'c'], ['a', 'b'])).toBe(true); expect((0, utils_1.containsAll)(['a', 'b', 'c'], ['a', 'b', 'c'])).toBe(true); }); it('returns false when array does not contain all of the items', () => { expect((0, utils_1.containsAll)(['a', 'b', 'c'], ['a', 'e'])).toBe(false); expect((0, utils_1.containsAll)(['a', 'b', 'c'], ['d', 'e', 'f'])).toBe(false); expect((0, utils_1.containsAll)(['a', 'b', 'c'], ['a', 'b', 'c', 'd'])).toBe(false); }); it('returns false when array is empty', () => { expect((0, utils_1.containsAll)([], ['a', 'b', 'c'])).toBe(false); }); it('returns true when items is empty', () => { expect((0, utils_1.containsAll)(['a', 'b', 'c'], [])).toBe(true); expect((0, utils_1.containsAll)([], [])).toBe(true); }); }); describe('range', () => { it('returns numbers array', () => { expect((0, utils_1.range)(5)).toStrictEqual([0, 1, 2, 3, 4]); expect((0, utils_1.range)(1, 5)).toStrictEqual([1, 2, 3, 4]); expect((0, utils_1.range)(0, 20, 5)).toStrictEqual([0, 5, 10, 15]); expect((0, utils_1.range)(0, -4, -1)).toStrictEqual([0, -1, -2, -3]); }); }); describe('sample', () => { it('returns undefined for an empty array', () => { expect((0, utils_1.sample)([])).toBeUndefined(); }); it('returns a random element from the array', () => { const numbers = [1, 2, 3, 4, 5]; const result = (0, utils_1.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 = (0, utils_1.sample)(numbers); expect(numbers).toContain(result); expect(numbers.indexOf(result)).not.toBe(-1); }); });