KeyboardMoveSelectionSpec.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import {
  6. forEach
  7. } from 'min-dash';
  8. import modelingModule from 'lib/features/modeling';
  9. import keyboardMoveSelectionModule from 'lib/features/keyboard-move-selection';
  10. import { createKeyEvent } from 'test/util/KeyEvents';
  11. var KEYS = {
  12. ArrowUp: 'up',
  13. Up: 'up',
  14. ArrowLeft: 'left',
  15. Left: 'left',
  16. ArrowRight: 'right',
  17. Right: 'right',
  18. ArrowDown: 'down',
  19. Down: 'down'
  20. };
  21. var shape1, shape2;
  22. describe('features/keyboard-move-selection', function() {
  23. describe('default config', function() {
  24. var BASE_SPEED = 1;
  25. var ACCELERATED_SPEED = 10;
  26. beforeEach(bootstrapDiagram({
  27. modules: [
  28. modelingModule,
  29. keyboardMoveSelectionModule
  30. ],
  31. canvas: {
  32. deferUpdate: false
  33. }
  34. }));
  35. beforeEach(inject(setupShapes));
  36. describe('should default move without shift', function() {
  37. verifyMoves({ shiftKey: false }, BASE_SPEED);
  38. });
  39. describe('should move accelerated with shift', function() {
  40. verifyMoves({ shiftKey: true }, ACCELERATED_SPEED);
  41. });
  42. describe('should not move with control', function() {
  43. verifyMove({ ctrlKey: true }, 'left', 'ArrowLeft', { x: 0, y: 0 });
  44. });
  45. describe('should not move with meta', function() {
  46. verifyMove({ metaKey: true }, 'left', 'ArrowLeft', { x: 0, y: 0 });
  47. });
  48. });
  49. describe('custom config', function() {
  50. var CUSTOM_SPEED = 23;
  51. var CUSTOM_SPEED_ACCELERATED = 77;
  52. beforeEach(bootstrapDiagram({
  53. modules: [
  54. modelingModule,
  55. keyboardMoveSelectionModule
  56. ],
  57. canvas: {
  58. deferUpdate: false
  59. },
  60. keyboardMoveSelection: {
  61. moveSpeed: CUSTOM_SPEED,
  62. moveSpeedAccelerated: CUSTOM_SPEED_ACCELERATED
  63. }
  64. }));
  65. beforeEach(inject(setupShapes));
  66. describe('should move with custom speed', function() {
  67. verifyMove({ shiftKey: false }, 'left', 'ArrowLeft', { x: -CUSTOM_SPEED, y: 0 });
  68. });
  69. describe('should move with custom accelerated speed', function() {
  70. verifyMove({ shiftKey: true }, 'left', 'ArrowLeft', { x: -CUSTOM_SPEED_ACCELERATED, y: 0 });
  71. });
  72. });
  73. describe('api', function() {
  74. beforeEach(bootstrapDiagram({
  75. modules: [
  76. modelingModule,
  77. keyboardMoveSelectionModule
  78. ],
  79. canvas: {
  80. deferUpdate: false
  81. }
  82. }));
  83. beforeEach(inject(setupShapes));
  84. it('should provide #moveSelection(direction, false)', inject(
  85. function(keyboardMoveSelection) {
  86. // when
  87. keyboardMoveSelection.moveSelection('down', false);
  88. // then
  89. expect(shape1.x).to.eql(10);
  90. expect(shape1.y).to.eql(10 + 1);
  91. expect(shape2.x).to.eql(150);
  92. expect(shape2.y).to.eql(10 + 1);
  93. }
  94. ));
  95. it('should provide #moveSelection(direction, true)', inject(
  96. function(keyboardMoveSelection) {
  97. // when
  98. keyboardMoveSelection.moveSelection('down', true);
  99. // then
  100. expect(shape1.x).to.eql(10);
  101. expect(shape1.y).to.eql(10 + 10);
  102. expect(shape2.x).to.eql(150);
  103. expect(shape2.y).to.eql(10 + 10);
  104. }
  105. ));
  106. });
  107. });
  108. // helpers ////////
  109. function setupShapes(canvas, selection) {
  110. shape1 = canvas.addShape({
  111. id: 'shape1',
  112. x: 10,
  113. y: 10,
  114. width: 100,
  115. height: 100
  116. });
  117. shape2 = canvas.addShape({
  118. id: 'shape2',
  119. x: 150,
  120. y: 10,
  121. width: 100,
  122. height: 100
  123. });
  124. selection.select([
  125. shape1,
  126. shape2
  127. ]);
  128. }
  129. function delta(direction, speed) {
  130. switch (direction) {
  131. case 'left': return { x: -speed, y: 0 };
  132. case 'right': return { x: speed, y: 0 };
  133. case 'up': return { x: 0, y: -speed };
  134. case 'down': return { x: 0, y: speed };
  135. default: throw new Error('illegal direction');
  136. }
  137. }
  138. function verifyMove(modifier, direction, key, expectedDelta) {
  139. it('<' + key + '> --> ' + direction, inject(function(keyboard) {
  140. // given
  141. var event = createKeyEvent(key, modifier);
  142. // when
  143. keyboard._keyHandler(event);
  144. // then
  145. expect(shape1.x).to.eql(10 + expectedDelta.x);
  146. expect(shape1.y).to.eql(10 + expectedDelta.y);
  147. expect(shape2.x).to.eql(150 + expectedDelta.x);
  148. expect(shape2.y).to.eql(10 + expectedDelta.y);
  149. }));
  150. }
  151. function verifyMoves(modifier, speed, expectedDelta) {
  152. forEach(KEYS, function(direction, key) {
  153. var d = expectedDelta || delta(direction, speed);
  154. verifyMove(modifier, direction, key, d);
  155. });
  156. }