AutoScrollSpec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { createCanvasEvent as canvasEvent } from '../../../util/MockEvents';
  2. /* global sinon */
  3. import {
  4. bootstrapDiagram,
  5. inject
  6. } from 'test/TestHelper';
  7. import dragModule from 'lib/features/dragging';
  8. import scrollModule from 'lib/features/auto-scroll';
  9. describe('features/auto-scroll - AutoScroll', function() {
  10. beforeEach(bootstrapDiagram({ modules: [ dragModule, scrollModule ] }));
  11. describe('scrolling canvas near borders', function() {
  12. // rely on defaults
  13. var scrollStep = 10;
  14. var scrollThresholdIn = 20;
  15. var scrollThresholdOut = 0;
  16. var scrollSpy;
  17. var clientRect;
  18. beforeEach(inject(function(dragging, autoScroll, canvas) {
  19. // given
  20. autoScroll.setOptions({
  21. // do not reapeat timeout
  22. scrollRepeatTimeout: 100000
  23. });
  24. scrollSpy = sinon.spy(canvas, 'scroll');
  25. dragging.init(canvasEvent({ x: 100, y: 100 }), 'foo');
  26. clientRect = canvas._container.getBoundingClientRect();
  27. }));
  28. it('not scrolled when inside the border', inject(function(dragging) {
  29. // when
  30. dragging.move(canvasEvent({
  31. x: scrollThresholdIn + 1,
  32. y: scrollThresholdIn + 1
  33. }));
  34. // then
  35. expect(scrollSpy).not.to.be.called;
  36. }));
  37. it('not scrolled when outside the border', inject(function(dragging) {
  38. // when
  39. dragging.move(canvasEvent({
  40. x: scrollThresholdOut - 1,
  41. y: scrollThresholdOut - 1
  42. }));
  43. // then
  44. expect(scrollSpy).not.to.be.called;
  45. }));
  46. it('stopped on cleanup', inject(function(dragging, autoScroll) {
  47. // given
  48. var stopScroll = sinon.spy(autoScroll, 'stopScroll');
  49. // when
  50. dragging.move(canvasEvent({
  51. x: scrollThresholdIn - 1,
  52. y: scrollThresholdIn - 1
  53. }));
  54. dragging.cancel();
  55. // then
  56. expect(scrollSpy).to.be.calledOnce;
  57. expect(stopScroll).to.be.calledTwice;
  58. }));
  59. it('stopped on moving outside the border', inject(function(dragging, autoScroll) {
  60. // given
  61. var stopScroll = sinon.spy(autoScroll, 'stopScroll');
  62. // when
  63. dragging.move(canvasEvent({
  64. x: scrollThresholdIn - 1,
  65. y: scrollThresholdIn - 1
  66. }));
  67. dragging.move(canvasEvent({
  68. x: scrollThresholdOut - 1,
  69. y: scrollThresholdOut - 1
  70. }));
  71. // then
  72. expect(scrollSpy).to.be.calledOnce;
  73. expect(stopScroll).to.be.calledTwice;
  74. }));
  75. it('left-down', inject(function(dragging, canvas) {
  76. // when
  77. dragging.move(canvasEvent({
  78. x: scrollThresholdIn - 1,
  79. y: (clientRect.height - scrollThresholdIn) + 1
  80. }));
  81. // then
  82. expect(scrollSpy).to.be.calledWith({ dx: scrollStep, dy: -scrollStep });
  83. }));
  84. it('left', inject(function(dragging) {
  85. // when
  86. dragging.move(canvasEvent({
  87. x: scrollThresholdIn - 1,
  88. y: scrollThresholdIn + 1
  89. }));
  90. // then
  91. expect(scrollSpy).to.be.calledWith({ dx: scrollStep, dy: 0 });
  92. }));
  93. it('left-up', inject(function(dragging) {
  94. // when
  95. dragging.move(canvasEvent({
  96. x: scrollThresholdIn - 1,
  97. y: scrollThresholdIn - 1
  98. }));
  99. // then
  100. expect(scrollSpy).to.be.calledWith({ dx: scrollStep, dy: scrollStep });
  101. }));
  102. it('up', inject(function(dragging) {
  103. // when
  104. dragging.move(canvasEvent({ x: scrollThresholdIn + 1, y: scrollThresholdIn - 1 }));
  105. // then
  106. expect(scrollSpy).to.be.calledWith({ dx: 0, dy: scrollStep });
  107. }));
  108. it('right-up', inject(function(dragging, canvas) {
  109. // when
  110. dragging.move(canvasEvent({
  111. x: (clientRect.width - scrollThresholdIn) + 1,
  112. y: scrollThresholdIn - 1
  113. }));
  114. // then
  115. expect(scrollSpy).to.be.calledWith({ dx: -scrollStep, dy: scrollStep });
  116. }));
  117. it('right', inject(function(dragging, canvas) {
  118. // when
  119. dragging.move(canvasEvent({
  120. x: (clientRect.width - scrollThresholdIn) + 1,
  121. y: scrollThresholdIn + 1
  122. }));
  123. // then
  124. expect(scrollSpy).to.be.calledWith({ dx: -scrollStep, dy: 0 });
  125. }));
  126. it('right-down', inject(function(dragging, canvas, mouseTracking) {
  127. // when
  128. dragging.move(canvasEvent({
  129. x: (clientRect.width - scrollThresholdIn) + 1,
  130. y: (clientRect.height - scrollThresholdIn) + 1
  131. }));
  132. // then
  133. expect(scrollSpy).to.be.calledWith({ dx: -scrollStep, dy: -scrollStep });
  134. }));
  135. it('down', inject(function(dragging, canvas, mouseTracking) {
  136. // when
  137. dragging.move(canvasEvent({
  138. x: scrollThresholdIn + 1,
  139. y: (clientRect.height - scrollThresholdIn) + 1
  140. }));
  141. // then
  142. expect(scrollSpy).to.be.calledWith({ dx: 0, dy: -scrollStep });
  143. }));
  144. });
  145. });