123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- import {
- bootstrapDiagram,
- inject
- } from 'test/TestHelper';
- import {
- forEach
- } from 'min-dash';
- import modelingModule from 'lib/features/modeling';
- import keyboardMoveSelectionModule from 'lib/features/keyboard-move-selection';
- import { createKeyEvent } from 'test/util/KeyEvents';
- var KEYS = {
- ArrowUp: 'up',
- Up: 'up',
- ArrowLeft: 'left',
- Left: 'left',
- ArrowRight: 'right',
- Right: 'right',
- ArrowDown: 'down',
- Down: 'down'
- };
- var shape1, shape2;
- describe('features/keyboard-move-selection', function() {
- describe('default config', function() {
- var BASE_SPEED = 1;
- var ACCELERATED_SPEED = 10;
- beforeEach(bootstrapDiagram({
- modules: [
- modelingModule,
- keyboardMoveSelectionModule
- ],
- canvas: {
- deferUpdate: false
- }
- }));
- beforeEach(inject(setupShapes));
- describe('should default move without shift', function() {
- verifyMoves({ shiftKey: false }, BASE_SPEED);
- });
- describe('should move accelerated with shift', function() {
- verifyMoves({ shiftKey: true }, ACCELERATED_SPEED);
- });
- describe('should not move with control', function() {
- verifyMove({ ctrlKey: true }, 'left', 'ArrowLeft', { x: 0, y: 0 });
- });
- describe('should not move with meta', function() {
- verifyMove({ metaKey: true }, 'left', 'ArrowLeft', { x: 0, y: 0 });
- });
- });
- describe('custom config', function() {
- var CUSTOM_SPEED = 23;
- var CUSTOM_SPEED_ACCELERATED = 77;
- beforeEach(bootstrapDiagram({
- modules: [
- modelingModule,
- keyboardMoveSelectionModule
- ],
- canvas: {
- deferUpdate: false
- },
- keyboardMoveSelection: {
- moveSpeed: CUSTOM_SPEED,
- moveSpeedAccelerated: CUSTOM_SPEED_ACCELERATED
- }
- }));
- beforeEach(inject(setupShapes));
- describe('should move with custom speed', function() {
- verifyMove({ shiftKey: false }, 'left', 'ArrowLeft', { x: -CUSTOM_SPEED, y: 0 });
- });
- describe('should move with custom accelerated speed', function() {
- verifyMove({ shiftKey: true }, 'left', 'ArrowLeft', { x: -CUSTOM_SPEED_ACCELERATED, y: 0 });
- });
- });
- describe('api', function() {
- beforeEach(bootstrapDiagram({
- modules: [
- modelingModule,
- keyboardMoveSelectionModule
- ],
- canvas: {
- deferUpdate: false
- }
- }));
- beforeEach(inject(setupShapes));
- it('should provide #moveSelection(direction, false)', inject(
- function(keyboardMoveSelection) {
- // when
- keyboardMoveSelection.moveSelection('down', false);
- // then
- expect(shape1.x).to.eql(10);
- expect(shape1.y).to.eql(10 + 1);
- expect(shape2.x).to.eql(150);
- expect(shape2.y).to.eql(10 + 1);
- }
- ));
- it('should provide #moveSelection(direction, true)', inject(
- function(keyboardMoveSelection) {
- // when
- keyboardMoveSelection.moveSelection('down', true);
- // then
- expect(shape1.x).to.eql(10);
- expect(shape1.y).to.eql(10 + 10);
- expect(shape2.x).to.eql(150);
- expect(shape2.y).to.eql(10 + 10);
- }
- ));
- });
- });
- // helpers ////////
- function setupShapes(canvas, selection) {
- shape1 = canvas.addShape({
- id: 'shape1',
- x: 10,
- y: 10,
- width: 100,
- height: 100
- });
- shape2 = canvas.addShape({
- id: 'shape2',
- x: 150,
- y: 10,
- width: 100,
- height: 100
- });
- selection.select([
- shape1,
- shape2
- ]);
- }
- function delta(direction, speed) {
- switch (direction) {
- case 'left': return { x: -speed, y: 0 };
- case 'right': return { x: speed, y: 0 };
- case 'up': return { x: 0, y: -speed };
- case 'down': return { x: 0, y: speed };
- default: throw new Error('illegal direction');
- }
- }
- function verifyMove(modifier, direction, key, expectedDelta) {
- it('<' + key + '> --> ' + direction, inject(function(keyboard) {
- // given
- var event = createKeyEvent(key, modifier);
- // when
- keyboard._keyHandler(event);
- // then
- expect(shape1.x).to.eql(10 + expectedDelta.x);
- expect(shape1.y).to.eql(10 + expectedDelta.y);
- expect(shape2.x).to.eql(150 + expectedDelta.x);
- expect(shape2.y).to.eql(10 + expectedDelta.y);
- }));
- }
- function verifyMoves(modifier, speed, expectedDelta) {
- forEach(KEYS, function(direction, key) {
- var d = expectedDelta || delta(direction, speed);
- verifyMove(modifier, direction, key, d);
- });
- }
|