level_0
int64 0
10k
| index
int64 0
0
| repo_id
stringlengths 22
152
| file_path
stringlengths 41
203
| content
stringlengths 11
11.5M
|
---|---|---|---|---|
9,588 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/addItem.test.ts | import addItem from './addItem';
describe('addItem()', () => {
it('adds item to empty array', () => {
const item = 'item to add';
const result = addItem([], item);
expect(result).toStrictEqual([item]);
});
it('appends item to ends of array', () => {
const array = ['item 1', 'item 2', 'item 3'];
const item = 'item to add';
const result = addItem(array, item);
expect(result).toStrictEqual([...array, item]);
});
});
|
9,590 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/arraysContainSameElements.test.ts | import arraysContainSameElements from './arraysContainSameElements';
describe('arraysContainSameElements()', () => {
it('returns true for empty arrays', () => {
const result = arraysContainSameElements([], []);
expect(result).toBeTruthy();
});
it('returns false if arrays are of different length', () => {
const array1 = ['item 1', 'item 2', 'item 3'];
const array2 = ['item 1', 'item 2'];
const result = arraysContainSameElements(array1, array2);
expect(result).toBeFalsy();
});
it('returns true if items are the same and are in the same order', () => {
const array1 = ['item 1', 'item 2', 'item 3'];
const array2 = ['item 1', 'item 2', 'item 3'];
const result = arraysContainSameElements(array1, array2);
expect(result).toBeTruthy();
});
it('returns true if items are the same but out of order', () => {
const array1 = ['item 1', 'item 2', 'item 3'];
const array2 = ['item 1', 'item 3', 'item 2'];
const result = arraysContainSameElements(array1, array2);
expect(result).toBeTruthy();
});
});
|
9,592 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/capitalize.test.ts | import capitalize from './capitalize';
describe('capitalize()', () => {
it('returns undefined when called with undefined', () => {
expect(capitalize(undefined)).toBe(undefined);
});
it('returns an empty string when an empty string is provided', () => {
expect(capitalize('')).toBe('');
});
it('capitalizes a single letter', () => {
expect(capitalize('a')).toBe('A');
});
it('capitalizes only the first letter', () => {
expect(capitalize('abc')).toBe('Abc');
});
it('capitalizes the first letter even if it is already a capital', () => {
expect(capitalize('Abc')).toBe('Abc');
});
it(`doesn't affect other capital letters`, () => {
expect(capitalize('ABc')).toBe('ABc');
});
});
|
9,594 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/chunk.test.ts | import chunk from './chunk';
describe('chunk()', () => {
it('creates an array of chunks of a given other array of fixed size', () => {
const output = chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], 3);
const expected = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
];
expect(output).toEqual(expected);
});
it('defaults to a chunk size of 1 ', () => {
const output = chunk(['a', 'b', 'c']);
const expected = [['a'], ['b'], ['c']];
expect(output).toEqual(expected);
});
it('creates the last chunk equal to the size of remaining elements if not exactly divisible by the chunk size', () => {
const output = chunk(['a', 'b', 'c', 'd', 'e'], 2);
const expected = [['a', 'b'], ['c', 'd'], ['e']];
expect(output).toEqual(expected);
});
it("doesn't mutate the input array", () => {
const input = ['a', 'b', 'c'];
chunk(input);
expect(input).toEqual(['a', 'b', 'c']);
});
it('returns an empty array given no input', () => {
const output = chunk();
expect(output).toEqual([]);
});
});
|
9,596 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/clamp.test.ts | import clamp from './clamp';
describe('clamp()', () => {
it('returns the exact value passed in if it already lies between min & max', () => {
expect(clamp(7, 0, 10)).toBe(7);
});
it('returns the min if the value passed in is lower than min', () => {
expect(clamp(-2, 0, 10)).toBe(0);
});
it('returns the max if the value passed in is higher than max', () => {
expect(clamp(12, 0, 10)).toBe(10);
});
});
|
9,598 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/clsx.test.ts | import clsx from './clsx';
describe('clsx()', () => {
it('handles array arguments', () => {
const result = clsx(['a', 'b']);
expect(result).toBe('a b');
});
it('returns empty string when empty array is passed', () => {
const result = clsx([]);
expect(result).toBe('');
});
it('returns empty string when empty string is passed', () => {
const result = clsx('');
expect(result).toBe('');
});
it('joins strings correctly', () => {
const result = clsx('a', 'b');
expect(result).toBe('a b');
});
it('trims empty space', () => {
const result = clsx('foo bar', ' ', 'foobar');
expect(result).toBe('foo bar foobar');
});
it('keeps only non-blank strings', () => {
const result = clsx(null, '', false, undefined, 'foobar', ' ', true);
expect(result).toBe('foobar');
});
});
|
9,600 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/compare.test.ts | import compare from './compare';
describe('compare()', () => {
it('returns 1 if a is greater than b', () => {
const result = compare(2, 1);
expect(result).toBe(1);
});
it('returns -1 if a is greater than b', () => {
const result = compare(1, 2);
expect(result).toBe(-1);
});
it('returns 0 if a is equal to b', () => {
const result = compare(1, 1);
expect(result).toBe(0);
});
it('acts as a numeric comparator between two values, consistent with the array.prototype.sort api', () => {
const input = [3, 8, 9, 1, 4, 6, 7, 5, 2];
const output = input.sort(compare);
const expected = [1, 2, 3, 4, 5, 6, 7, 8, 9];
expect(output).toEqual(expected);
});
});
|
9,602 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/debounce.test.ts | import debounce from './debounce';
describe('debounce()', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
});
afterAll(() => {
jest.useRealTimers();
});
it('delays invoking function until after wait time', () => {
const functionToDebounce = jest.fn();
const wait = 1000;
const debouncedFunction = debounce(functionToDebounce, wait);
debouncedFunction();
expect(functionToDebounce).not.toHaveBeenCalled();
// Call function again just before the wait time expires
jest.advanceTimersByTime(wait - 1);
debouncedFunction();
expect(functionToDebounce).not.toHaveBeenCalled();
// fast-forward time until 1 millisecond before the function should be invoked
jest.advanceTimersByTime(wait - 1);
expect(functionToDebounce).not.toHaveBeenCalled();
// fast-forward until 1st call should be executed
jest.advanceTimersByTime(1);
expect(functionToDebounce).toHaveBeenCalledTimes(1);
});
it('does not invoke function if already invoked', () => {
const functionToDebounce = jest.fn();
const wait = 1000;
const debouncedFunction = debounce(functionToDebounce, wait);
debouncedFunction();
jest.advanceTimersByTime(wait);
expect(functionToDebounce).toHaveBeenCalledTimes(1);
functionToDebounce.mockClear();
jest.advanceTimersByTime(wait);
expect(functionToDebounce).not.toHaveBeenCalled();
});
describe('options', () => {
describe('immediate', () => {
it('defaults to false and does not invoke function immediately', () => {
const functionToDebounce = jest.fn();
const wait = 1000;
const debouncedFunction = debounce(functionToDebounce, wait);
debouncedFunction();
expect(functionToDebounce).not.toHaveBeenCalled();
});
it('invokes function immediately if true', () => {
const functionToDebounce = jest.fn();
const wait = 1000;
const debouncedFunction = debounce(functionToDebounce, wait, { leading: true });
debouncedFunction();
expect(functionToDebounce).toHaveBeenCalled();
});
});
});
describe('abort()', () => {
it('does not invoke function if abort is called before wait time expires', () => {
const functionToDebounce = jest.fn();
const wait = 1000;
const debouncedFunction = debounce(functionToDebounce, wait);
debouncedFunction();
// Call function again just before the wait time expires
jest.advanceTimersByTime(wait - 1);
debouncedFunction.cancel();
// fast-forward until 1st call should be executed
jest.advanceTimersByTime(1);
expect(functionToDebounce).not.toHaveBeenCalled();
});
});
});
|
9,604 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/diff.test.ts | import diff from './diff';
describe('diff()', () => {
it('finds all values that are present in a given first array but not present in a given second array', () => {
const output = diff([1, null, 'a', true], [1, undefined, 'a', false]);
const expected = [null, true];
expect(output).toEqual(expected);
});
});
|
9,606 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/getRandomString.test.ts | import { disableRandomMock, initRandomMock } from '@proton/testing/lib/mockRandomValues';
import getRandomString, { DEFAULT_CHARSET } from './getRandomString';
describe('getRandomString()', () => {
const getConsecutiveArray = (length: number) => [...Array(length).keys()];
const mockedRandomValues = jest
.fn()
.mockImplementation((array: Uint32Array) => Uint32Array.from(getConsecutiveArray(array.length)));
beforeAll(() => initRandomMock(mockedRandomValues));
afterAll(() => disableRandomMock());
describe('length', () => {
it('returns throw an error when length is negative', () => {
expect(() => getRandomString(-1)).toThrow();
});
it('returns an empty string when length is 0', () => {
const result = getRandomString(0);
expect(result).toBe('');
});
it('returns a string of required length', () => {
const lengths = [1, 2, 3, 5, 8, 13];
const results = lengths.map((length) => getRandomString(length));
expect(results.map((result) => result.length)).toStrictEqual(lengths);
});
});
describe('charset', () => {
it('defaults the charset', () => {
const result = getRandomString(DEFAULT_CHARSET.length);
expect(mockedRandomValues).toHaveBeenCalled();
expect(result).toBe(DEFAULT_CHARSET);
});
it('returns characters from the defined charset', () => {
const charset = 'qwerty';
const result = getRandomString(charset.length, charset);
expect(result).toBe(charset);
});
});
});
|
9,608 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/getSafeErrorObject.test.ts | import { getSafeArray, getSafeErrorObject, getSafeObject, getSafeValue } from './getSafeErrorObject';
describe('getSafeValue', () => {
const error = new Error('blah');
const testCases = [
{ name: 'number', value: 3, expected: 3 },
{ name: 'string', value: 'abc', expected: 'abc' },
{ name: 'boolean', value: true, expected: true },
{ name: 'null', value: null, expected: null },
{ name: 'undefined', value: undefined, expected: undefined },
{ name: 'array', value: [1, 2, 3], expected: [1, 2, 3] },
{ name: 'array (deep)', value: [1, 2, 3, [4, 5]], expected: [1, 2, 3, [4, 5]] },
{ name: 'object', value: { key: 'value' }, expected: { key: 'value' } },
{
name: 'object (deep)',
value: { key: 'value', other: { stuff: [1, 2, 3] } },
expected: { key: 'value', other: { stuff: [1, 2, 3] } },
},
{ name: 'error', value: error, expected: getSafeErrorObject(error) },
{ name: 'unsupported type', value: Symbol('oh no'), expected: undefined },
];
testCases.forEach((testCase) => {
it(`should accept '${testCase.name}'`, () => {
expect(getSafeValue(testCase.value)).toStrictEqual(testCase.expected);
});
});
});
describe('getSafeErrorObject', () => {
it(`should accept errors`, () => {
const value = new Error('blah');
const expected = { name: 'Error', message: 'blah', cause: undefined };
const result = getSafeValue(value);
expect(result).toHaveProperty('stack');
expect(result).toMatchObject(expected);
});
it(`should accept errors with cause`, () => {
const value = new Error('blah', { cause: { something: true } });
const expected = { name: 'Error', message: 'blah', cause: { something: true } };
const result = getSafeValue(value);
expect(result).toHaveProperty('stack');
expect(result).toMatchObject(expected);
});
it(`should accept errors with nested errors in cause`, () => {
const value = new Error('blah', { cause: { e: new Error('oh no') } });
const expected = {
name: 'Error',
message: 'blah',
cause: { e: { name: 'Error', message: 'oh no', cause: undefined } },
};
const result = getSafeValue(value);
expect(result).toHaveProperty('stack');
expect(result).toHaveProperty('cause.e.stack');
expect(result).toMatchObject(expected);
});
});
describe('getSafeObject', () => {
const testCases = [
{ name: 'object', value: { key: 'value' }, expected: { key: 'value' } },
{
name: 'object (deep)',
value: { key: 'value', other: { stuff: [1, 2, 3] } },
expected: { key: 'value', other: { stuff: [1, 2, 3] } },
},
];
testCases.forEach((testCase) => {
it(`should accept '${testCase.name}'`, () => {
expect(getSafeObject(testCase.value)).toStrictEqual(testCase.expected);
});
});
});
describe('getSafeArray', () => {
const testCases = [
{ name: 'array', value: [1, 2, 3], expected: [1, 2, 3] },
{ name: 'array (deep)', value: [1, 2, 3, [4, 5]], expected: [1, 2, 3, [4, 5]] },
{ name: 'array (objects)', value: [1, 2, 3, [{ blah: true }]], expected: [1, 2, 3, [{ blah: true }]] },
];
testCases.forEach((testCase) => {
it(`should accept '${testCase.name}'`, () => {
expect(getSafeArray(testCase.value)).toStrictEqual(testCase.expected);
});
});
});
|
9,610 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/groupWith.test.ts | import groupWith from './groupWith';
describe('groupWith', () => {
it('groups items of an array into a two dimensional array based on a condition of whether or not two items should be grouped', () => {
expect(groupWith((a, b) => a === b, [1, 1, 1, 2, 2, 3])).toEqual([[1, 1, 1], [2, 2], [3]]);
});
it('defaults to use an empty array', () => {
expect(groupWith((a, b) => a === b)).toEqual([]);
});
it('returns an empty array if no items are provided in the to-be-grouped array', () => {
expect(groupWith((x) => x, [])).toEqual([]);
});
it('returns an empty array if no items pass the grouping condition', () => {
expect(groupWith(() => false, [1, 2, 3])).toEqual([]);
});
});
|
9,612 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/identity.test.ts | import identity from './identity';
describe('identity()', () => {
it('returns the value as reference', () => {
const value = {};
expect(identity(value)).toBe(value);
});
});
|
9,614 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/isArrayOfUint8Array.test.ts | import isArrayOfUint8Array from './isArrayOfUint8Array';
describe('isArrayOfUint8Array()', () => {
it('returns true if array is empty', () => {
const result = isArrayOfUint8Array([]);
expect(result).toBe(true);
});
it('returns true if every item is an instance of Uint8Array', () => {
const result = isArrayOfUint8Array([new Uint8Array(1), new Uint8Array(2), new Uint8Array(3)]);
expect(result).toBe(true);
});
it('returns false if any item is not an instance of Uint8Array', () => {
const result = isArrayOfUint8Array([new Uint8Array(1), 'not instance of Uint8Array', new Uint8Array(3)]);
expect(result).toBe(false);
});
});
|
9,616 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/isBetween.test.ts | import isBetween from './isBetween';
describe('isBetween()', () => {
it('returns true if a given number is between two other given number', () => {
expect(isBetween(5, -10, 10)).toBe(true);
});
it('returns true if a given number is exactly equal to the min', () => {
expect(isBetween(-10, -10, 10)).toBe(true);
});
it('returns false if a given number is exactly equal to the max', () => {
expect(isBetween(10, -10, 10)).toBe(false);
});
it('returns false if a given number is outside two other given number', () => {
expect(isBetween(20, -10, 10)).toBe(false);
});
});
|
9,618 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/isFunction.test.ts | import isFunction from './isFunction';
it('should return true for class', () => {
const result = isFunction(class Any {});
expect(result).toEqual(true);
});
it('should return true for function', () => {
const result = isFunction(() => {});
expect(result).toEqual(true);
});
it('should return true for generator ', () => {
const result = isFunction(function* () {}); // eslint-disable-line @typescript-eslint/no-empty-function
expect(result).toEqual(true);
});
it('should return false for non-functions', () => {
expect(isFunction([1, 2, 3])).toEqual(false);
expect(isFunction({ a: 1, b: 2, c: 3 })).toEqual(false);
expect(isFunction(true)).toEqual(false);
});
|
9,620 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/isLastWeek.test.ts | import isLastWeek from './isLastWeek';
describe('isLastWeek', () => {
beforeEach(() => {
jest.useFakeTimers().setSystemTime(new Date('2023-03-27T20:00:00'));
});
afterEach(() => {
jest.useRealTimers();
});
it('should return true if the date is in the last week', () => {
const date = new Date(2023, 2, 20); // March 20, 2023 is in the last week
expect(isLastWeek(date)).toBe(true);
});
it('should return false if the date is not in the last week', () => {
const date = new Date(2022, 2, 27); // March 27, 2022 is not in the last week
expect(isLastWeek(date)).toBe(false);
});
it('should handle weekStartsOn option correctly', () => {
const date = new Date(2023, 2, 20); // March 20, 2023 is in the last week, regardless of weekStartsOn value
// With Monday is the start of the week
expect(isLastWeek(date, { weekStartsOn: 1 })).toBe(true);
});
it('should return true if the date is in the last week and given as a timestamp', () => {
const timestamp = new Date(2023, 2, 20).getTime(); // March 20, 2023 is in the last week
expect(isLastWeek(timestamp)).toBe(true);
});
});
|
9,622 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/isTruthy.test.ts | import isTruthy from './isTruthy';
describe('isTruthy()', () => {
it('tells whether a value is JavaScript "truthy" or not', () => {
expect(isTruthy(false)).toBe(false);
expect(isTruthy(null)).toBe(false);
expect(isTruthy(0)).toBe(false);
expect(isTruthy(undefined)).toBe(false);
expect(isTruthy([])).toBe(true);
expect(isTruthy({})).toBe(true);
expect(isTruthy(true)).toBe(true);
expect(isTruthy(1)).toBe(true);
expect(isTruthy(-1)).toBe(true);
});
});
|
9,627 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/lastItem.test.ts | import lastItem from './lastItem';
describe('lastItem()', () => {
it('should return undefined when array is empty', () => {
const result = lastItem([]);
expect(result).toBe(undefined);
});
it('should return only item when array is of length 1', () => {
const item = 'item';
const result = lastItem([item]);
expect(result).toBe(item);
});
it('should return last item when array is of length greater than 1', () => {
const item = 'item';
const result = lastItem(['1', '2', item]);
expect(result).toBe(item);
});
});
|
9,629 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/mergeUint8Arrays.test.ts | import mergeUint8Arrays from './mergeUint8Arrays';
describe('mergeUint8Arrays()', () => {
it('returns empty array if arrays is empty', () => {
const arrays: Uint8Array[] = [];
const result = mergeUint8Arrays(arrays);
expect(result).toStrictEqual(new Uint8Array());
});
it('merges Uint8Array', () => {
const arrays = [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])];
const result = mergeUint8Arrays(arrays);
expect(result).toStrictEqual(new Uint8Array([1, 2, 3, 4, 5, 6]));
});
});
|
9,631 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/mod.test.ts | import mod from './mod';
describe('mod()', () => {
it('should return a positive remainder', () => {
expect(mod(-4, 3)).toEqual(2);
expect(mod(-3, 3)).toEqual(0);
expect(mod(-2, 3)).toEqual(1);
expect(mod(-1, 3)).toEqual(2);
expect(mod(0, 3)).toEqual(0);
expect(mod(1, 3)).toEqual(1);
expect(mod(2, 3)).toEqual(2);
expect(mod(3, 3)).toEqual(0);
expect(mod(4, 3)).toEqual(1);
});
});
|
9,633 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/move.test.ts | import move from './move';
describe('move()', () => {
it('should return a new array', () => {
const list = [1, 2, 3, 4, 5];
expect(move(list, 0, 0) !== list).toBeTruthy();
});
it('should correctly move elements to new positions', () => {
const list = [1, 2, 3, 4, 5];
expect(move(list, 3, 0)).toEqual([4, 1, 2, 3, 5]);
});
it('should be able to handle negative indices', () => {
const list = [1, 2, 3, 4, 5];
expect(move(list, -1, 0)).toEqual([5, 1, 2, 3, 4]);
expect(move(list, 1, -2)).toEqual([1, 3, 4, 2, 5]);
expect(move(list, -3, -4)).toEqual([1, 3, 2, 4, 5]);
});
/**
* TODO:
* This case is arguably an incorrect behaviour of this util, however for the sake
* of not altering the public api at this time, I'm adding this test case to test
* for regressions in case of a modification / refactor & also because this is the
* current contract of this util.
*/
it('returns an array with one entry "undefined" if not provided a first argument', () => {
expect(move(undefined, 0, 0)).toEqual([undefined]);
});
});
|
9,635 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/noop.test.ts | import noop from './noop';
describe('noop()', () => {
it('returns undefined', () => {
expect(noop()).toBe(undefined);
});
});
|
9,637 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/orderBy.test.ts | import orderBy from './orderBy';
describe('orderBy()', () => {
it('orders an array of objects by the numeric value of a given property of the objects in the array', () => {
const arrayOfObjects = [{ id: 7 }, { id: 3 }, { id: 3 }, { id: 6 }, { id: 18 }, { id: 2 }];
const output = orderBy(arrayOfObjects, 'id');
const expected = [{ id: 2 }, { id: 3 }, { id: 3 }, { id: 6 }, { id: 7 }, { id: 18 }];
expect(output).toEqual(expected);
});
});
|
9,640 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/partition.test.ts | import partition from './partition';
describe('partition()', () => {
it('returns empty arrays if array is empty', () => {
const array: string[] = [];
const predicate = (item: string): item is string => {
return typeof item === 'string';
};
const result = partition(array, predicate);
expect(result).toStrictEqual([[], []]);
});
it('partitions items that fit the predicate into the first array', () => {
const array: any[] = ['string 0', 'string 1', 0, 1, undefined, null, 'string 2', 2, 'string 3'];
const predicate = (item: string): item is string => {
return typeof item === 'string';
};
const result = partition(array, predicate);
expect(result).toStrictEqual([
['string 0', 'string 1', 'string 2', 'string 3'],
[0, 1, undefined, null, 2],
]);
});
});
|
9,642 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/percentOf.test.ts | import percentOf from './percentOf';
describe('percentOf()', () => {
it('returns the value of a percentage of another value', () => {
const output = percentOf(10, 200);
expect(output).toBe(20);
});
it("returns a decimal in case the percentage can't be resolved in integers", () => {
const output = percentOf(2.5, 10);
expect(Math.round(output)).not.toBe(output);
});
});
|
9,644 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/percentage.test.ts | import percentage from './percentage';
describe('percentage()', () => {
it('returns the percentage between an entire and a fraction', () => {
const output = percentage(500, 100);
expect(output).toBe(20);
});
it("returns a decimal in case the percentage can't be resolved in integers", () => {
const output = percentage(30, 10);
expect(Math.round(output)).not.toBe(output);
});
it('returns 0 if either inputs are 0', () => {
expect(percentage(0, 1)).toBe(0);
expect(percentage(1, 0)).toBe(0);
});
});
|
9,646 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/randomIntFromInterval.test.ts | import randomIntFromInterval from './randomIntFromInterval';
describe('randomIntFromInterval()', () => {
it('should be able to return min', () => {
jest.spyOn(Math, 'random').mockReturnValue(0);
const min = 1;
const max = 100;
const result = randomIntFromInterval(min, max);
expect(result).toBe(min);
});
it('should be able to return max', () => {
jest.spyOn(Math, 'random').mockReturnValue(
// Math.random does not output 1
0.9999999999999999
);
const min = 1;
const max = 100;
const result = randomIntFromInterval(min, max);
expect(result).toBe(max);
});
it('should return an evenish distribution', () => {
jest.spyOn(Math, 'random').mockReturnValue(0.5);
const min = 1;
const max = 100;
const result = randomIntFromInterval(min, max);
expect(result).toBe(51);
});
});
|
9,648 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/range.test.ts | import range from './range';
describe('range()', () => {
it('defaults to creating a specific array if no arguments are provided', () => {
expect(range()).toEqual([0]);
});
it('returns an empty array if end is before start', () => {
expect(range(10, 2)).toEqual([]);
});
it('returns an empty array if end is same as start', () => {
expect(range(2, 2)).toEqual([]);
});
// Here closed is the mathematical definition
it('has a closed start', () => {
const start = 0;
const result = range(start, 2);
expect(result[0]).toEqual(start);
});
// Here open is the mathematical definition
it('has an open end', () => {
const end = 2;
const result = range(0, end);
expect(result[result.length - 1]).toBeLessThan(end);
});
it('returns range with correct step', () => {
expect(range(-4, 5, 2)).toEqual([-4, -2, 0, 2, 4]);
expect(range(0, 2, 0.5)).toEqual([0, 0.5, 1, 1.5]);
});
});
|
9,650 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/remove.test.ts | import remove from './remove';
describe('remove()', () => {
it('removes an item from an array', () => {
const output = remove(['a', 'b', 'c'], 'b');
expect(output).toEqual(['a', 'c']);
});
it('removes only the first occurence should there be multiple', () => {
const output = remove(['a', 'b', 'c', 'b', 'd', 'b'], 'b');
expect(output).toEqual(['a', 'c', 'b', 'd', 'b']);
});
it('returns the original array if the given element does not occur in the input array', () => {
const input = ['a', 'b', 'c', 'd'];
const output = remove(input, 'e');
expect(output).toBe(input);
});
});
|
9,652 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/removeIndex.test.ts | import removeIndex from './removeIndex';
describe('removeIndex()', () => {
it('removes item at a given index from an array', () => {
const output = removeIndex(['a', 'b', 'c', 'd', 'e'], 2);
const expected = ['a', 'b', 'd', 'e'];
expect(output).toEqual(expected);
});
});
|
9,654 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/replace.test.ts | import replace from './replace';
describe('replace()', () => {
it('replaces an item from an array', () => {
const output = replace(['a', 'b', 'c'], 'b', 'x');
expect(output).toEqual(['a', 'x', 'c']);
});
it('replaces only the first occurence should there be multiple', () => {
const output = replace(['a', 'b', 'c', 'b', 'd', 'b'], 'b', 'x');
expect(output).toEqual(['a', 'x', 'c', 'b', 'd', 'b']);
});
it('returns the original array if the given element does not occur in the input array', () => {
const input = ['a', 'b', 'c', 'd'];
const output = replace(input, 'e', 'x');
expect(output).toBe(input);
});
});
|
9,656 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/shallowEqual.test.ts | import shallowEqual from './shallowEqual';
describe('shallowEqual()', () => {
it('returns true when comparing 2 empty arrays', () => {
const result = shallowEqual([], []);
expect(result).toBe(true);
});
it('returns true if arrays contain the same items', () => {
const result = shallowEqual(['item 0', 'item 1', 'item 2'], ['item 0', 'item 1', 'item 2']);
expect(result).toBe(true);
});
it('returns false if arrays are different lengths', () => {
const result = shallowEqual([], ['item 0']);
expect(result).toBe(false);
});
it('returns false if arrays contain different items', () => {
const result = shallowEqual(['item 0', 'item 1', 'item 2'], ['item 0', 'DIFFERENT item 1', 'item 2']);
expect(result).toBe(false);
});
});
|
9,658 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/shuffle.test.ts | import shuffle from './shuffle';
describe('shuffle()', () => {
it('returns empty array when empty array is passed', () => {
const result = shuffle([]);
expect(result).toStrictEqual([]);
});
it('returns same array when array of length 1 is passed', () => {
const input = [0];
const result = shuffle(input);
expect(result).toStrictEqual(input);
});
it('return array of same length', () => {
const input = [0, 1, 2, 3, 4, 5, 6];
const result = shuffle(input);
expect(result.length).toBe(input.length);
});
it('does not mutate items in the array', () => {
const input = [0, 1, 2, 3, 4, 5, 6];
const result = shuffle(input);
expect(result.sort()).toStrictEqual(input.sort());
});
it('shuffles as expected when random values are mocked', () => {
jest.spyOn(Math, 'random')
.mockReturnValueOnce(0.8824154514152932)
.mockReturnValueOnce(0.22451795440520217)
.mockReturnValueOnce(0.5352346169904075)
.mockReturnValueOnce(0.9121122157867988)
.mockReturnValueOnce(0.008603728182251968)
.mockReturnValueOnce(0.26845647050651644)
.mockReturnValueOnce(0.44258055272215036)
.mockReturnValueOnce(0.08296662925946618)
.mockReturnValueOnce(0.4341574602173227);
const input = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const result = shuffle(input);
expect(result).toStrictEqual([3, 9, 5, 7, 1, 0, 6, 4, 2, 8]);
});
describe('when array is of length 2', () => {
it('swaps items when random value is < 0.5', () => {
jest.spyOn(Math, 'random').mockReturnValue(0.49);
const input = [0, 1];
const result = shuffle(input);
expect(result).toStrictEqual([1, 0]);
});
it('does not swap items when random value is = 0.5', () => {
jest.spyOn(Math, 'random').mockReturnValue(0.5);
const input = [0, 1];
const result = shuffle(input);
expect(result).toStrictEqual(input);
});
it('does not swap items when random value is > 0.5', () => {
jest.spyOn(Math, 'random').mockReturnValue(0.51);
const input = [0, 1];
const result = shuffle(input);
expect(result).toStrictEqual(input);
});
});
});
|
9,660 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/throttle.test.ts | import throttle from './throttle';
describe('throttle()', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
});
afterAll(() => {
jest.useRealTimers();
});
it('invokes function at most once every wait milliseconds', () => {
const functionToThrottle = jest.fn();
const wait = 1000;
const throttledFunction = throttle(functionToThrottle, wait);
throttledFunction();
expect(functionToThrottle).toHaveBeenCalledTimes(1);
// Call function just before the wait time expires
jest.advanceTimersByTime(wait - 1);
throttledFunction();
expect(functionToThrottle).toHaveBeenCalledTimes(1);
// fast-forward until 1st call should be executed
jest.advanceTimersByTime(1);
expect(functionToThrottle).toHaveBeenCalledTimes(2);
throttledFunction();
expect(functionToThrottle).toHaveBeenCalledTimes(2);
jest.advanceTimersByTime(wait);
expect(functionToThrottle).toHaveBeenCalledTimes(3);
});
});
|
9,662 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/truncate.test.ts | import truncate, { DEFAULT_TRUNCATE_OMISSION } from './truncate';
describe('truncate()', () => {
it('returns empty sting if provided string is empty', () => {
expect(truncate('', 0)).toEqual('');
expect(truncate('', 1)).toEqual('');
expect(truncate('', 2)).toEqual('');
});
it('truncates to required length', () => {
const length = 3;
const result = truncate('abcd', length);
expect(result.length).toEqual(length);
});
describe('charsToDisplay', () => {
it('defaults to 50', () => {
const str = 'a'.repeat(51);
const result = truncate(str);
expect(result.length).toBe(50);
});
it('returns inputted string if charsToDisplay is the length of the string', () => {
const testStrings = ['a', 'ab', 'abc'];
const results = testStrings.map((str) => truncate(str, str.length));
expect(results).toStrictEqual(testStrings);
});
it('returns inputted string if charsToDisplay is more than the length of the string', () => {
const str = '12345';
const result = truncate(str, str.length + 1);
expect(result).toBe(str);
});
it('returns truncated string if charsToDisplay is less than the length of the string', () => {
const str = '12345';
const result = truncate(str, str.length - 1);
expect(result).toBe('123' + DEFAULT_TRUNCATE_OMISSION);
});
});
describe('omission', () => {
it('uses default omission if not provided', () => {
const result = truncate('ab', 1);
expect(result).toEqual(DEFAULT_TRUNCATE_OMISSION);
});
it('uses provided omission', () => {
const omission = 'omission';
const result = truncate('ab', 1, omission);
expect(result).toEqual(omission);
});
});
});
|
9,665 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/unary.test.ts | import unary from './unary';
describe('unary()', () => {
it('should handle functions with no arguments', () => {
const myFunction = () => {
return 'myFunction';
};
const unaryFunction = unary(myFunction);
expect(unaryFunction('I still require an argument')).toEqual('myFunction');
});
it('should handle functions with a single argument', () => {
const myFunction = (arg: string) => {
return arg;
};
const unaryFunction = unary(myFunction);
expect(unaryFunction('Argument')).toEqual('Argument');
});
it('should ensure only one argument is passed', () => {
const myFunction = (name: string, index?: number) => {
if (index === undefined) {
return `Ola ${name}`;
}
return `Ola ${name} - ${index}`;
};
const names = ['Joao', 'Felix', 'Tareixa'];
expect(names.map(myFunction)).toEqual(['Ola Joao - 0', 'Ola Felix - 1', 'Ola Tareixa - 2']);
expect(names.map(unary(myFunction))).toEqual(['Ola Joao', 'Ola Felix', 'Ola Tareixa']);
});
});
|
9,667 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/uncapitalize.test.ts | import uncapitalize from './uncapitalize';
describe('uncapitalize()', () => {
it('returns an empty string when an empty string is provided', () => {
expect(uncapitalize('')).toBe('');
});
it('uncapitalizes a single letter', () => {
expect(uncapitalize('A')).toBe('a');
});
it('uncapitalizes only the first letter', () => {
expect(uncapitalize('Abc')).toBe('abc');
});
it('uncapitalizes the first letter even if it is already lowercase', () => {
expect(uncapitalize('abc')).toBe('abc');
});
it(`doesn't affect other letters`, () => {
expect(uncapitalize('ABc')).toBe('aBc');
});
});
|
9,669 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/unique.test.ts | import unique from './unique';
describe('unique()', () => {
it('should return same', () => {
expect(unique([1, 2])).toEqual([1, 2]);
});
it('should only return unique items', () => {
expect(unique([1, 2, 1])).toEqual([1, 2]);
});
});
|
9,671 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/uniqueBy.test.ts | import uniqueBy from './uniqueBy';
describe('uniqueBy()', () => {
it('should only get unique items', () => {
const list = [{ foo: 'abc' }, { foo: 'bar' }, { foo: 'asd' }, { foo: 'bar' }, { foo: 'bar' }];
expect(uniqueBy(list, ({ foo }) => foo)).toEqual([{ foo: 'abc' }, { foo: 'bar' }, { foo: 'asd' }]);
});
it('should only get unique items', () => {
const list = [{ foo: 'abc' }, { foo: 'bar' }];
expect(uniqueBy(list, ({ foo }) => foo)).toEqual([{ foo: 'abc' }, { foo: 'bar' }]);
});
});
|
9,673 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/updateItem.test.ts | import updateItem from './updateItem';
describe('updateItem()', () => {
it('returns empty array if array is empty', () => {
const array: any[] = [];
const index = 1;
const newItem = 'new item';
const result = updateItem(array, index, newItem);
expect(result).toStrictEqual([]);
});
it('updates correct item', () => {
const array = ['item 0', 'item 1', 'item 2'];
const index = 1;
const newItem = 'new item';
const result = updateItem(array, index, newItem);
expect(result).toStrictEqual(['item 0', 'new item', 'item 2']);
});
it('does not update array if index does not exist', () => {
const array = ['item 0', 'item 1', 'item 2'];
const index = -1;
const newItem = 'new item';
const result = updateItem(array, index, newItem);
expect(result).toStrictEqual(array);
});
});
|
9,675 | 0 | petrpan-code/ProtonMail/WebClients/packages | petrpan-code/ProtonMail/WebClients/packages/utils/withDecimalPrecision.test.ts | import withDecimalPrecision from './withDecimalPrecision';
describe('withDecimalPrecision()', () => {
it('returns the same number when the precision is infinity', () => {
const list = [2.234, 5, 381712.0001, -1, -1.38, -0.99, -10282.12312];
expect(list.map((x) => withDecimalPrecision(x, Infinity))).toEqual(list);
});
it('is equivalent to Math.round when the precision is zero', () => {
const list = [2.234, 5, 0, -1, -1.38, -0.99, -10282.12312];
expect(list.map((x) => withDecimalPrecision(x, 0))).toEqual(list.map(Math.round));
});
it('returns 0 when the precision is minus infinity', () => {
const list = [2.234, 5, 0, -1, -1.38, -0.99, -10282.12312];
expect(list.map((x) => withDecimalPrecision(x, -Infinity))).toEqual(list.map(() => 0));
});
it('returns the expected values for some simple cases', () => {
const list = [122.234, 5, -0.54, -1, -1.387, 12.4491];
expect(list.map((x, i) => withDecimalPrecision(x, i - 2))).toEqual([100, 10, -1, -1, -1.39, 12.449]);
});
});
|
Subsets and Splits