Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | import { addToList, forEach, getItem, print } from './hierarchicalListUtils';
describe('hierarchicalListUtils', function () {
let sharedList;
beforeEach(function () {
sharedList = [
['1.2.3.1', ['1.2.3.1.1', '1.2.3.1.2']],
'1.2.3.2',
['1.2.3.3', ['1.2.3.3.1', ['1.2.3.3.2', ['1.2.3.3.2.1', '1.2.3.3.2.2']]]],
];
});
describe('getItem', function () {
it('should retrieve elements from a list by index', function () {
expect(getItem(sharedList, 0)).toBe('1.2.3.1');
expect(getItem(sharedList, 1)).toBe('1.2.3.2');
expect(getItem(sharedList, 2)).toBe('1.2.3.3');
expect(getItem(sharedList, 3)).toBeUndefined();
});
it('should retrieve elements from a list by path', function () {
expect(getItem(sharedList, '0')).toBe('1.2.3.1');
expect(getItem(sharedList, '0/0')).toBe('1.2.3.1.1');
expect(getItem(sharedList, '0/1')).toBe('1.2.3.1.2');
expect(getItem(sharedList, '0/2')).toBeUndefined();
expect(getItem(sharedList, '1')).toBe('1.2.3.2');
expect(getItem(sharedList, '2')).toBe('1.2.3.3');
expect(getItem(sharedList, '2/0')).toBe('1.2.3.3.1');
expect(getItem(sharedList, '2/1')).toBe('1.2.3.3.2');
expect(getItem(sharedList, '2/2')).toBeUndefined();
expect(getItem(sharedList, '2/1/0')).toBe('1.2.3.3.2.1');
expect(getItem(sharedList, '2/1/1')).toBe('1.2.3.3.2.2');
expect(getItem(sharedList, '2/1/2')).toBeUndefined();
expect(getItem(sharedList, '3')).toBeUndefined();
});
});
describe('addToList', function () {
it('should support adding elements to a list hierarchically', function () {
const list = [];
addToList(list, '1.2.3.1', '1.2.3.1.1');
addToList(list, '1.2.3.1', '1.2.3.1.2');
addToList(list, '1.2.3.2');
addToList(list, '1.2.3.3', '1.2.3.3.1');
addToList(list, '1.2.3.3', '1.2.3.3.2', '1.2.3.3.2.1');
addToList(list, '1.2.3.3', '1.2.3.3.2', '1.2.3.3.2.2');
expect(list).toStrictEqual(sharedList);
});
it('should change leaf nodes into non-leaf nodes', function () {
const listw = [];
const listx = [['x.1', ['x.1.1', 'x.1.2']], 'x.2'];
const listy = [
['x.1', [['x.1.1', ['x.1.1.1']], 'x.1.2']],
['x.2', ['x.2.1']],
];
addToList(listw, 'x.1');
addToList(listw, 'x.1', 'x.1.1');
addToList(listw, 'x.1', 'x.1.2');
addToList(listw, 'x.2');
expect(listw).toStrictEqual(listx);
addToList(listw, 'x.2', 'x.2.1');
addToList(listw, 'x.1', 'x.1.1', 'x.1.1.1');
expect(listw).toStrictEqual(listy);
});
});
describe('forEach', function () {
it('should iterate through all leaf nodes of the tree', function () {
const fn = jest.fn();
forEach(sharedList, fn);
expect(fn).toHaveBeenCalledTimes(6);
expect(fn).nthCalledWith(1, '1.2.3.1', '1.2.3.1.1');
expect(fn).nthCalledWith(2, '1.2.3.1', '1.2.3.1.2');
expect(fn).nthCalledWith(3, '1.2.3.2');
expect(fn).nthCalledWith(4, '1.2.3.3', '1.2.3.3.1');
expect(fn).nthCalledWith(5, '1.2.3.3', '1.2.3.3.2', '1.2.3.3.2.1');
expect(fn).nthCalledWith(6, '1.2.3.3', '1.2.3.3.2', '1.2.3.3.2.2');
});
});
describe('print', function () {
it('should pretty-print the hierarchical list', function () {
expect(print(sharedList)).toBe(
'1.2.3.1\n' +
' 1.2.3.1.1\n' +
' 1.2.3.1.2\n' +
'1.2.3.2\n' +
'1.2.3.3\n' +
' 1.2.3.3.1\n' +
' 1.2.3.3.2\n' +
' 1.2.3.3.2.1\n' +
' 1.2.3.3.2.2\n'
);
});
});
});
|