In the angular docs directives section
They give roughly this example of how to make a drag object.
My question is how would you test THEIR EXAMPLE / implementation:
var startX = 0, startY = 0; scope.x = 0; scope.y = 0; element.css({ top: scope.y, left: scope.x }); element.on('mousedown', function(event) { // Prevent default dragging of selected content event.preventDefault(); startX = event.pageX - scope.x; startY = event.pageY - scope.y; $document.on('mousemove', mousemove); $document.on('mouseup', mouseup); }); function mousemove(event) { scope.y = event.pageY - startY; scope.x = event.pageX - startX; element.css({ top: scope.y + 'px', left: scope.x + 'px' }); } function mouseup() { $document.off('mousemove', mousemove); $document.off('mouseup', mouseup); } }
but this only works for one event.
The angular example uses mousedown to add mousemove and mouseup event listeners, and https://stackoverflow.com/a/166328/ uses the triggerHandler--, which prevents bubbling / distribution.
right now I have (roughly):
describe('on mousedown it', function(){ it('moves a thing', function(){ expect(scope.x).toBe(0); element.triggerHandler({type: 'mousedown', pageX: 0, pageY:0}); element.triggerHandler({type: 'mousemove', pageX:10, pageY:10); expect(scope.x).toBe(10); }); });
Test
does not work. scope.x is written as 0. what to do?
angularjs unit-testing angularjs-directive jasmine
Andrew Luhring
source share