diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-13 21:10:27 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-13 21:10:27 +0100 |
commit | 5a72b487125beaa6b7bd81584fb9d35c4ffc43be (patch) | |
tree | cf297b91bf9e99ce21a1ff7d6936a2830e320935 /spec | |
parent | 05ba3ddc990e6d22d1bac7e23b72613a355287bc (diff) | |
download | svg.js-5a72b487125beaa6b7bd81584fb9d35c4ffc43be.tar.gz svg.js-5a72b487125beaa6b7bd81584fb9d35c4ffc43be.zip |
Round up everything for the last 2.x release2.7.0
### Fixed
- fixed calling `parent()` on `documentFragment`s children (#927)
- parser is not focusable anymore (#908)
- `SVG.Element.click(null)` correctly unbinds the event (#878)
- fix memory leak (#905)
### Added
- `SVG.Set` now accepts another Set as input (#893)
- `on()/off()` accepts multiple event names as input (backport from 3.0)
Diffstat (limited to 'spec')
-rw-r--r-- | spec/spec/event.js | 337 | ||||
-rw-r--r-- | spec/spec/set.js | 19 |
2 files changed, 61 insertions, 295 deletions
diff --git a/spec/spec/event.js b/spec/spec/event.js index 0b9fe5b..1fad4aa 100644 --- a/spec/spec/event.js +++ b/spec/spec/event.js @@ -18,266 +18,39 @@ describe('Event', function() { }) if (!this.isTouchDevice) { - - describe('click()', function() { - it('attaches an onclick event to the node of the element', function() { - rect.click(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'click', action) - }) - it('fires the event on click', function() { - rect.click(action).fire('click') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.click(action).fire('click') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.click(action)).toBe(rect) - }) - }) - - describe('dblclick()', function() { - it('attaches an ondblclick event to the node of the element', function() { - rect.dblclick(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'dblclick', action) - }) - it('fires the event on dblclick', function() { - rect.dblclick(action).fire('dblclick') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.dblclick(action).fire('dblclick') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.dblclick(action)).toBe(rect) - }) - }) - - describe('mousedown()', function() { - it('attaches an onmousedown event to the node of the element', function() { - rect.mousedown(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'mousedown', action) - }) - it('fires the event on mousedown', function() { - rect.mousedown(action).fire('mousedown') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.mousedown(action).fire('mousedown') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.mousedown(action)).toBe(rect) - }) - }) - - describe('mouseup()', function() { - it('attaches an onmouseup event to the node of the element', function() { - rect.mouseup(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'mouseup', action) - }) - it('fires the event on mouseup', function() { - rect.mouseup(action).fire('mouseup') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.mouseup(action).fire('mouseup') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.mouseup(action)).toBe(rect) + [ 'click' + , 'dblclick' + , 'mousedown' + , 'mouseup' + , 'mouseover' + , 'mouseout' + , 'mousemove' + , 'mouseenter' + , 'mouseleave' + ].forEach(function(event) { + describe(event+'()', function() { + it('calls `on()` with '+event+' as event', function() { + rect[event](action) + expect(SVG.on).toHaveBeenCalledWith(rect, event, action) + }) }) }) - - describe('mouseover()', function() { - it('attaches an onmouseover event to the node of the element', function() { - rect.mouseover(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'mouseover', action) - }) - it('fires the event on mouseover', function() { - rect.mouseover(action).fire('mouseover') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.mouseover(action).fire('mouseover') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.mouseover(action)).toBe(rect) - }) - }) - - describe('mouseout()', function() { - it('attaches an onmouseout event to the node of the element', function() { - rect.mouseout(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'mouseout', action) - }) - it('fires the event on mouseout', function() { - rect.mouseout(action).fire('mouseout') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.mouseout(action).fire('mouseout') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.mouseout(action)).toBe(rect) - }) - }) - - describe('mousemove()', function() { - it('attaches an onmousemove event to the node of the element', function() { - rect.mousemove(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'mousemove', action) - }) - it('fires the event on mousemove', function() { - rect.mousemove(action).fire('mousemove') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.mousemove(action).fire('mousemove') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.mousemove(action)).toBe(rect) - }) - }) - - /*describe('mouseenter()', function() { - it('attaches an onmouseenter event to the node of the element', function() { - expect(typeof rect.node.onmouseenter).not.toBe('function') - rect.mouseenter(action) - expect(typeof rect.node.onmouseenter).toBe('function') - }) - it('fires the event on mouseenter', function() { - rect.mouseenter(action).fire('mouseenter') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.mouseenter(action).fire('mouseenter') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.mouseenter(action)).toBe(rect) - }) - }) - - describe('mouseleave()', function() { - it('attaches an onmouseleave event to the node of the element', function() { - expect(typeof rect.node.onmouseleave).not.toBe('function') - rect.mouseleave(action) - expect(typeof rect.node.onmouseleave).toBe('function') - }) - it('fires the event on mouseleave', function() { - rect.mouseleave(action).fire('mouseleave') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.mouseleave(action).fire('mouseleave') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.mouseleave(action)).toBe(rect) - }) - })*/ - } else { - - describe('touchstart()', function() { - it('attaches an ontouchstart event to the node of the element', function() { - rect.touchstart(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'touchstart', action) - }) - it('fires the event on touchstart', function() { - rect.touchstart(action).fire('touchstart') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.touchstart(action).fire('touchstart') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.touchstart(action)).toBe(rect) - }) - }) - - describe('touchmove()', function() { - it('attaches an ontouchmove event to the node of the element', function() { - rect.touchmove(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'touchmove', action) - }) - it('fires the event on touchmove', function() { - rect.touchmove(action).fire('touchmove') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.touchmove(action).fire('touchmove') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.touchmove(action)).toBe(rect) - }) - }) - - describe('touchleave()', function() { - it('attaches an ontouchleave event to the node of the element', function() { - rect.touchleave(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'touchleave', action) - }) - it('fires the event on touchleave', function() { - rect.touchleave(action).fire('touchleave') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.touchleave(action).fire('touchleave') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.touchleave(action)).toBe(rect) + [ 'touchstart' + , 'touchmove' + , 'touchleave' + , 'touchend' + , 'touchcancel' + ].forEach(function(event) { + describe(event+'()', function() { + it('calls `on()` with '+event+' as event', function() { + rect[event](action) + expect(SVG.on).toHaveBeenCalledWith(rect, event, action) + }) }) }) - - describe('touchend()', function() { - it('attaches an ontouchend event to the node of the element', function() { - rect.touchend(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'touchend', action) - }) - it('fires the event on touchend', function() { - rect.touchend(action).fire('touchend') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.touchend(action).fire('touchend') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.touchend(action)).toBe(rect) - }) - }) - - describe('touchcancel()', function() { - it('attaches an ontouchcancel event to the node of the element', function() { - rect.touchcancel(action) - expect(SVG.on).toHaveBeenCalledWith(rect.node, 'touchcancel', action) - }) - it('fires the event on touchcancel', function() { - rect.touchcancel(action).fire('touchcancel') - expect(toast).toBe('ready') - }) - it('applies the element as context', function() { - rect.touchcancel(action).fire('touchcancel') - expect(context).toBe(rect) - }) - it('returns the called element', function() { - expect(rect.touchcancel(action)).toBe(rect) - }) - }) - } - describe('on()', function() { it('attaches an event to the element', function() { @@ -292,39 +65,32 @@ describe('Event', function() { SVG.off(el, 'event', action) }) it('attaches multiple handlers on different element', function() { - var listenerCnt = SVG.listeners.length - - var rect2 = draw.rect(100,100); - var rect3 = draw.rect(100,100); + var rect2 = draw.rect(100, 100) + var rect3 = draw.rect(100, 100) rect.on('event', action) rect2.on('event', action) rect3.on('event', function(){ butter = 'melting' }) rect3.on('event', action) - expect(Object.keys(SVG.listeners[SVG.handlerMap.indexOf(rect.node)]['event']['*']).length).toBe(1) // 1 listener on rect - expect(Object.keys(SVG.listeners[SVG.handlerMap.indexOf(rect2.node)]['event']['*']).length).toBe(1) // 1 listener on rect2 - expect(Object.keys(SVG.listeners[SVG.handlerMap.indexOf(rect3.node)]['event']['*']).length).toBe(2) // 2 listener on rect3 - - expect(SVG.listeners.length).toBe(listenerCnt + 3) // added listeners on 3 different elements + expect(Object.keys(rect._events['event']['*']).length).toBe(1) // 1 listener on rect + expect(Object.keys(rect2._events['event']['*']).length).toBe(1) // 1 listener on rect2 + expect(Object.keys(rect3._events['event']['*']).length).toBe(2) // 2 listener on rect3 }) - if('attaches a handler to a namespaced event', function(){ - var listenerCnt = SVG.listeners.length - - var rect2 = draw.rect(100,100); - var rect3 = draw.rect(100,100); + it('attaches a handler to a namespaced event', function(){ + var rect2 = draw.rect(100, 100) + var rect3 = draw.rect(100, 100) rect.on('event.namespace1', action) rect2.on('event.namespace2', action) rect3.on('event.namespace3', function(){ butter = 'melting' }) rect3.on('event', action) - expect(Object.keys(SVG.listeners[SVG.handlerMap.indexOf(rect.node)]['event']['*'])).toBeUndefined() // no global listener on rect - expect(Object.keys(SVG.listeners[SVG.handlerMap.indexOf(rect.node)]['event']['namespace1']).length).toBe( 1) // 1 namespaced listener on rect - expect(Object.keys(SVG.listeners[SVG.handlerMap.indexOf(rect2.node)]['event']['namespace2']).length).toBe(1) // 1 namespaced listener on rect - expect(Object.keys(SVG.listeners[SVG.handlerMap.indexOf(rect3.node)]['event']['*']).length).toBe(1) // 1 gobal listener on rect3 - expect(Object.keys(SVG.listeners[SVG.handlerMap.indexOf(rect3.node)]['event']['namespace3']).length).toBe(1) // 1 namespaced listener on rect3 - expect(SVG.listeners.length).toBe(listenerCnt + 3) // added listeners on 3 different elements + expect(rect._events['event']['*']).toBeUndefined() // no global listener on rect + expect(Object.keys(rect._events['event']['namespace1']).length).toBe( 1) // 1 namespaced listener on rect + expect(Object.keys(rect2._events['event']['namespace2']).length).toBe(1) // 1 namespaced listener on rect2 + expect(Object.keys(rect3._events['event']['*']).length).toBe(1) // 1 gobal listener on rect3 + expect(Object.keys(rect3._events['event']['namespace3']).length).toBe(1) // 1 namespaced listener on rect3 }) it('applies the element as context', function() { rect.on('event', action).fire('event') @@ -336,7 +102,7 @@ describe('Event', function() { }) it('stores the listener for future reference', function() { rect.on('event', action) - expect(SVG.listeners[SVG.handlerMap.indexOf(rect.node)]['event']['*'][action._svgjsListenerId]).not.toBeUndefined() + expect(rect._events['event']['*'][action._svgjsListenerId]).not.toBeUndefined() }) it('returns the called element', function() { expect(rect.on('event', action)).toBe(rect) @@ -351,8 +117,8 @@ describe('Event', function() { }) it('detaches a specific event listener, all other still working', function() { - rect2 = draw.rect(100,100); - rect3 = draw.rect(100,100); + rect2 = draw.rect(100,100) + rect3 = draw.rect(100,100) rect.on('event', action) rect2.on('event', action) @@ -360,7 +126,7 @@ describe('Event', function() { rect.off('event', action) - expect(Object.keys(SVG.listeners[SVG.handlerMap.indexOf(rect.node)]['event']['*']).length).toBe(0) + expect(Object.keys(rect._events['event']['*']).length).toBe(0) rect.fire('event') expect(toast).toBeNull() @@ -371,11 +137,11 @@ describe('Event', function() { rect3.fire('event') expect(butter).toBe('melting') - expect(SVG.listeners[SVG.handlerMap.indexOf(rect.node)]['event']['*'][action]).toBeUndefined() + expect(rect._events['event']['*'][action]).toBeUndefined() }) it('detaches a specific namespaced event listener, all other still working', function() { - rect2 = draw.rect(100,100); - rect3 = draw.rect(100,100); + rect2 = draw.rect(100,100) + rect3 = draw.rect(100,100) rect.on('event.namespace', action) rect2.on('event.namespace', action) @@ -383,7 +149,8 @@ describe('Event', function() { rect.off('event.namespace', action) - expect(Object.keys(SVG.listeners[SVG.handlerMap.indexOf(rect.node)]['event']['namespace']).length).toBe(0) + expect(Object.keys(rect._events['event']['namespace']).length).toBe(0) + expect(Object.keys(rect2._events['event']['namespace']).length).toBe(1) rect.fire('event') expect(toast).toBeNull() @@ -394,7 +161,7 @@ describe('Event', function() { rect3.fire('event') expect(butter).toBe('melting') - expect(SVG.listeners[SVG.handlerMap.indexOf(rect.node)]['event']['namespace'][action]).toBeUndefined() + expect(rect._events['event']['namespace'][action]).toBeUndefined() }) it('detaches all listeners for a specific namespace', function() { rect.on('event', action) @@ -413,7 +180,7 @@ describe('Event', function() { rect.fire('event') expect(toast).toBeNull() expect(butter).toBeNull() - expect(SVG.listeners[SVG.handlerMap.indexOf(rect.node)]['event']).toBeUndefined() + expect(rect._events['event']).toBeUndefined() }) it('detaches all listeners without an argument', function() { rect.on('event', action) @@ -423,7 +190,7 @@ describe('Event', function() { rect.fire('click') expect(toast).toBeNull() expect(butter).toBeNull() - expect(SVG.listeners[SVG.handlerMap.indexOf(rect.node)]).toBeUndefined() + expect(Object.keys(rect._events).length).toBe(0) }) it('returns the called element', function() { expect(rect.off('event', action)).toBe(rect) @@ -439,7 +206,7 @@ describe('Event', function() { expect('Should not error out').toBe(true) } - expect(SVG.handlerMap[SVG.handlerMap.indexOf(rect.node)]).toBe(undefined) + expect(Object.keys(rect._events).length).toBe(0) }) }) diff --git a/spec/spec/set.js b/spec/spec/set.js index 50c3126..5532abf 100644 --- a/spec/spec/set.js +++ b/spec/spec/set.js @@ -23,7 +23,15 @@ describe('Set', function() { var members = [1, 2, 4] expect(draw.set(members).valueOf()).toBe(members) - }) + }) + + it('creates a set when passing another set', function() { + var set = new SVG.Set([1, 2, 4]) + var set2 = new SVG.Set(set) + + expect(set.valueOf()).not.toBe(set2.valueOf()) + expect(set.valueOf()).toEqual(set2.valueOf()) + }) describe('add()', function() { it('returns the set instance', function() { @@ -204,12 +212,3 @@ describe('Set', function() { }) }) - - - - - - - - - |