Outlook_Addin_LLM/node_modules/@fluentui/utilities/lib/array.test.js

155 lines
6.9 KiB
JavaScript

import { findIndex, find, createArray, removeIndex, flatten, replaceElement, addElementAtIndex, arraysEqual, } from './array';
describe('array utils tests', function () {
describe('findIndex tests', function () {
it('returns -1 when there is no match in the array', function () {
var array = [0, 1, 2];
var index = findIndex(array, function () { return false; });
expect(index).toEqual(-1);
});
it('should return the correct index when the predicate satisfies the condition', function () {
var array = [0, 1, 2];
var index = findIndex(array, function (elem) { return elem === 1; });
expect(index).toEqual(1);
});
it('should return the first index when repeated elements satisfy the predicate', function () {
var array = [0, 1, 2, 2];
var index = findIndex(array, function (elem) { return elem === 2; });
expect(index).toEqual(2);
});
});
describe('find tests', function () {
it('returns -1 when there is no match in the array', function () {
var array = [0, 1, 2];
var item = find(array, function () { return false; });
expect(item).toEqual(undefined);
});
it('should return the correct item when the predicate satisfies the condition', function () {
var array = [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }];
var item = find(array, function (elem) { return elem.id === 1; });
expect(item).toEqual(array[1]);
});
it('should return the first index when repeated elements satisfy the predicate', function () {
var array = [8, 9, 10, 11];
var item = find(array, function (elem) { return elem === 10; });
expect(item).toEqual(10);
});
});
describe('createArray tests', function () {
it('creates an array while invoking the callback', function () {
var result = createArray(4, function (index) { return String.fromCharCode('a'.charCodeAt(0) + index); });
expect(result).toEqual(['a', 'b', 'c', 'd']);
});
});
describe('removeIndex tests', function () {
it('should return a new array instead of mutating the existing array', function () {
var array = [0, 1, 2];
var result = removeIndex(array, 0);
expect(result).not.toBe(array);
});
it('should remove the first element of the array', function () {
var array = [0, 1, 2];
var result = removeIndex(array, 0);
expect(result).toEqual([1, 2]);
});
it('should remove the last element of the array', function () {
var array = [0, 1, 2];
var result = removeIndex(array, 2);
expect(result).toEqual([0, 1]);
});
it('should remove the element in the middle of the array', function () {
var array = [0, 1, 2];
var result = removeIndex(array, 1);
expect(result).toEqual([0, 2]);
});
});
describe('replaceElement tests', function () {
it('should return a new array instead of mutating the existing array', function () {
var array = [1, 2, 3];
var result = replaceElement(array, 3, 1);
expect(result).toEqual([1, 3, 3]);
expect(result).not.toBe(array);
});
it('should return a new array with the replaced element in the center', function () {
var array = ['Zero', 'One', 'Two', 'Three', 'Four'];
var result = replaceElement(array, 'owT', 2);
expect(result).toEqual(['Zero', 'One', 'owT', 'Three', 'Four']);
});
it('should return a new array with the first element replaced', function () {
var array = ['Zero', 'One', 'Two', 'Three', 'Four'];
var result = replaceElement(array, 'oreZ', 0);
expect(result).toEqual(['oreZ', 'One', 'Two', 'Three', 'Four']);
});
it('should return a new array with the last element replaced', function () {
var array = ['Zero', 'One', 'Two', 'Three', 'Four'];
var result = replaceElement(array, 'ruoF', 4);
expect(result).toEqual(['Zero', 'One', 'Two', 'Three', 'ruoF']);
});
});
describe('addElementAddIndex tests', function () {
it('should add an element at the start of the array', function () {
var array = [2, 3, 4];
var result = addElementAtIndex(array, 0, 1);
expect(result).toEqual([1, 2, 3, 4]);
});
it('should add an element at the end of the array', function () {
var array = [2, 3, 4];
var result = addElementAtIndex(array, 3, 5);
expect(result).toEqual([2, 3, 4, 5]);
});
it('should add the element in the middle of the array', function () {
var array = [2, 3, 4];
var result = addElementAtIndex(array, 2, 3.5);
expect(result).toEqual([2, 3, 3.5, 4]);
});
});
describe('flatten tests', function () {
it('does nothing for an empty array', function () {
var array = [];
var result = flatten(array);
expect(result).toEqual(array);
});
it('does nothing an array with a single element', function () {
var array = [1];
var result = flatten(array);
expect(result).toEqual(array);
});
it('does nothing for an array of numbers', function () {
var array = [1, 2, 3];
var result = flatten(array);
expect(result).toEqual(array);
});
it('flattens an array of arrays', function () {
var array = [[1, 2, 3], [4, 6, 8], [20]];
var result = flatten(array);
expect(result).toEqual([1, 2, 3, 4, 6, 8, 20]);
});
it('flattens an array with numbers and arrays of numbers', function () {
var array = [[1, 2, 3], [4, 6, 8], 20, 22, [25, 26, 28]];
var result = flatten(array);
expect(result).toEqual([1, 2, 3, 4, 6, 8, 20, 22, 25, 26, 28]);
});
});
describe('arraysEqual tests', function () {
it('two empty arrays are equal', function () {
var arr1 = [];
var arr2 = [];
expect(arraysEqual(arr1, arr2)).toEqual(true);
});
it('different length arrays are not equal', function () {
var arr1 = [1, 2];
var arr2 = [1];
expect(arraysEqual(arr1, arr2)).toEqual(false);
});
it('different value arrays are not equal', function () {
var arr1 = [1, 2];
var arr2 = [1, 3];
expect(arraysEqual(arr1, arr2)).toEqual(false);
});
it('two exact arrays are equal', function () {
var arr1 = [1, 2, 3];
var arr2 = [1, 2, 3];
expect(arraysEqual(arr1, arr2)).toEqual(true);
});
});
});
//# sourceMappingURL=array.test.js.map