diff --git a/src/app/cartography/shared/tools/moving-tool.spec.ts b/src/app/cartography/shared/tools/moving-tool.spec.ts new file mode 100644 index 00000000..33fc6b58 --- /dev/null +++ b/src/app/cartography/shared/tools/moving-tool.spec.ts @@ -0,0 +1,75 @@ +import { select } from "d3-selection"; +import { Context } from "../../../map/models/context"; +import { SVGSelection } from "../../../map/models/types"; +import { MovingTool } from "./moving-tool"; + + +describe('MovingTool', () => { + let tool: MovingTool; + let svg: SVGSelection; + let context: Context; + let node: SVGSelection; + let canvas: SVGSelection; + + beforeEach(() => { + tool = new MovingTool(); + + svg = select('body') + .append('svg') + .attr('width', 1000) + .attr('height', 1000); + + canvas = svg.append('g').attr('class', 'canvas'); + + node = canvas + .append('g') + .attr('class', 'node') + .attr('x', 10) + .attr('y', 20); + + + context = new Context(); + + tool.connect(svg, context); + tool.draw(svg, context); + tool.activate(); + + }); + + describe('MovingTool can move canvas', () => { + beforeEach(() => { + svg.node().dispatchEvent( + new MouseEvent('mousedown', { + clientX: 100, clientY: 100, relatedTarget: svg.node(), + screenY: 1024, screenX: 1024, view: window + }) + ); + + window.dispatchEvent(new MouseEvent('mousemove', {clientX: 200, clientY: 200})); + window.dispatchEvent(new MouseEvent('mouseup', {clientX: 200, clientY: 200, view: window})); + }); + + it('canvas should transformed', () => { + expect(canvas.attr('transform')).toEqual('translate(100, 100) scale(1)'); + }); + }); + + describe('MovingTool can be deactivated', () => { + beforeEach(() => { + tool.deactivate(); + + svg.node().dispatchEvent( + new MouseEvent('mousedown', { + clientX: 100, clientY: 100, relatedTarget: svg.node(), + screenY: 1024, screenX: 1024, view: window + }) + ); + + window.dispatchEvent(new MouseEvent('mousemove', {clientX: 200, clientY: 200})); + }); + + it('canvas cannot be transformed', () => { + expect(canvas.attr('transform')).toBeNull(); + }); + }); +}); diff --git a/src/app/cartography/shared/tools/moving-tool.ts b/src/app/cartography/shared/tools/moving-tool.ts index 873f1c56..f326f4fd 100644 --- a/src/app/cartography/shared/tools/moving-tool.ts +++ b/src/app/cartography/shared/tools/moving-tool.ts @@ -9,7 +9,7 @@ export class MovingTool { private zoom: ZoomBehavior; constructor() { - this.zoom = zoom() + this.zoom = zoom() .scaleExtent([1 / 2, 8]); } @@ -28,6 +28,7 @@ export class MovingTool { const self = this; const onZoom = function(this: SVGSVGElement) { + const canvas = self.selection.select("g.canvas"); const e: D3ZoomEvent = event; canvas.attr( @@ -36,7 +37,6 @@ export class MovingTool { `${self.context.getSize().height / 2 + e.transform.y}) scale(${e.transform.k})`); }; - this.zoom.on('zoom', onZoom); this.selection.call(this.zoom); }