basicprimitives
Version:
Basic Primitives Diagrams for JavaScript - data visualization components library that implements organizational chart and multi-parent dependency diagrams, contains implementations of JavaScript Controls and PDF rendering plugins.
186 lines (164 loc) • 5.28 kB
JavaScript
import KeyboardNavigationManager from './KeyboardNavigationManager';
import Rect from '../graphics/structs/Rect';
import { OrientationType } from '../enums';
var layout1 = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
];
var layout2 = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
];
function getKeyboardNavigationManager(items) {
var result = KeyboardNavigationManager();
for (var index = 0; index < items.length; index += 1) {
var item = items[index];
result.addRect(new Rect(item[0], item[1], item[2], item[3]), index);
}
return result;
}
function testSequence(items, cursorItem, steps) {
var manager = getKeyboardNavigationManager(items);
var result = [];
for (var index = 0, len = steps.length; index < len; index += 1) {
var direction = steps[index];
switch (direction) {
case OrientationType.Top:
cursorItem = manager.getItemAbove(cursorItem);
result.push(cursorItem);
break;
case OrientationType.Bottom:
cursorItem = manager.getItemBelow(cursorItem);
result.push(cursorItem);
break;
case OrientationType.Left:
cursorItem = manager.getItemOnLeft(cursorItem);
result.push(cursorItem);
break;
case OrientationType.Right:
cursorItem = manager.getItemOnRight(cursorItem);
result.push(cursorItem);
break;
}
}
return result;
}
test('getNavigationLevels validation function', () => {
var manager = getKeyboardNavigationManager(layout1);
var result = manager.getNavigationLevels();
var expectedResult = [
[],
[],
[],
[],
[]
];
expect(result).toEqual(expectedResult);
});
test('getItemOnRight function', () => {
var result = testSequence(layout1, 0, [OrientationType.Right]);
expect(result).toEqual([1]);
});
test('getItemOnLeft function', () => {
var result = testSequence(layout1, 11, [OrientationType.Left]);
expect(result).toEqual([10]);
});
test('getItemBelow function', () => {
var result = testSequence(layout1, 3, [OrientationType.Bottom]);
expect(result).toEqual([15]);
});
test('getItemBelow function', () => {
var result = testSequence(layout1, 7, [OrientationType.Top]);
expect(result).toEqual([5]);
});
test('getItemAbove function should return nearest item at the row above on the left', () => {
var result = testSequence(layout2, 3, [OrientationType.Top]);
expect(result).toEqual([0]);
});
test('getItemAbove function should return nearest item on the right', () => {
var result = testSequence(layout1, 13, [OrientationType.Top]);
expect(result).toEqual([11]);
});
test('getItemAbove function should return the item it gets if it belongs to top row', () => {
var result = testSequence(layout1, 4, [OrientationType.Top]);
expect(result).toEqual([4]);
});
test('Navigation into multi-row item and back should return cursor to original item', () => {
var result = testSequence(layout1, 21, [
OrientationType.Left,
OrientationType.Right
]);
expect(result).toEqual([15, 21]);
});
test('Navigation into multi-row item and back should return cursor to original item', () => {
var result = testSequence(layout1, 21, [
OrientationType.Left,
OrientationType.Left,
OrientationType.Right,
OrientationType.Right
]);
expect(result).toEqual([15, 0, 15, 21]);
});
test('Navigation across multi-row item and back should return cursor to original item', () => {
var result = testSequence(layout1, 18, [
OrientationType.Right,
OrientationType.Right,
OrientationType.Left,
OrientationType.Left
]);
expect(result).toEqual([12, 13, 12, 18]);
});
test('Navigation along multi-row item up should move cursor to its top row', () => {
var result = testSequence(layout1, 12, [
OrientationType.Left,
OrientationType.Top,
OrientationType.Right
]);
expect(result).toEqual([8, 8, 9]);
});
test('Navigation along multi-row item down should move cursor to its bottom row', () => {
var result = testSequence(layout1, 1, [
OrientationType.Left,
OrientationType.Bottom,
OrientationType.Right,
OrientationType.Right
]);
expect(result).toEqual([0, 0, 15, 21]);
});