diff options
author | Saivan <savian@me.com> | 2018-03-02 12:48:43 +1100 |
---|---|---|
committer | Saivan <savian@me.com> | 2018-03-02 12:48:43 +1100 |
commit | a208ed5c5de4b654efc54c2bcbe8dc7d4875392d (patch) | |
tree | d435e0e1e1b926f2fb3484717728b21f96c7a78e /spec | |
parent | 1582edb4236628fbc7875242f159c16283b769c2 (diff) | |
parent | c4373581ab49dff509a857eb4e896f00fc81add6 (diff) | |
download | svg.js-a208ed5c5de4b654efc54c2bcbe8dc7d4875392d.tar.gz svg.js-a208ed5c5de4b654efc54c2bcbe8dc7d4875392d.zip |
Fixed some tests involving transforms and elements
Diffstat (limited to 'spec')
-rw-r--r-- | spec/SpecRunner.html | 9 | ||||
-rw-r--r-- | spec/spec/doc.js | 21 | ||||
-rw-r--r-- | spec/spec/element.js | 3 | ||||
-rw-r--r-- | spec/spec/event.js | 111 | ||||
-rw-r--r-- | spec/spec/nested.js | 13 | ||||
-rw-r--r-- | spec/spec/svg.js | 4 |
6 files changed, 83 insertions, 78 deletions
diff --git a/spec/SpecRunner.html b/spec/SpecRunner.html index 1ecef12..62e9121 100644 --- a/spec/SpecRunner.html +++ b/spec/SpecRunner.html @@ -71,15 +71,14 @@ <script src="spec/gradient.js"></script> <script src="spec/group.js"></script>--> <script src="spec/helper.js"></script> - <!-- <script src="spec/hyperlink.js"></script> + <!--<script src="spec/hyperlink.js"></script> <script src="spec/image.js"></script> <script src="spec/line.js"></script> <script src="spec/marker.js"></script> <script src="spec/mask.js"></script> --> <script src="spec/matrix.js"></script> - <!-- <script src="spec/memory.js"></script> + <!-- <script src="spec/number.js"></script> <script src="spec/nested.js"></script> - <script src="spec/number.js"></script> <script src="spec/path.js"></script> <script src="spec/pattern.js"></script> <script src="spec/point.js"></script> @@ -96,8 +95,8 @@ <script src="spec/transformations.js"></script> <script src="spec/tspan.js"></script> <script src="spec/use.js"></script> - <script src="spec/utils.js"></script> - <script src="spec/viewbox.js"></script> --> + <script src="spec/utils.js"></script> --> + <!-- <script src="spec/viewbox.js"></script> --> --> </body> </html> diff --git a/spec/spec/doc.js b/spec/spec/doc.js index 1e1c54c..5accd5b 100644 --- a/spec/spec/doc.js +++ b/spec/spec/doc.js @@ -16,11 +16,11 @@ describe('Doc', function() { expect(draw instanceof SVG.Doc).toBe(true) }) - it('returns itself as Doc', function() { + it('returns itself as Doc when root', function() { expect(draw.doc()).toBe(draw) }) - it('has a defs element', function() { + it('has a defs element when root', function() { expect(draw.defs() instanceof SVG.Defs).toBe(true) }) @@ -33,6 +33,23 @@ describe('Doc', function() { }) }) + describe('isRoot()', function() { + it('returns true when the doc is not attached to dom', function() { + expect(SVG().isRoot()).toBe(true) + }) + it('returns true when its outer element is not an svg element', function () { + expect(SVG().addTo(document.createElement('div')).isRoot()).toBe(true) + }) + it('returns true when its the root element of the dom', function () { + if(parserInDoc) { + expect(draw.isRoot()).toBe(true) + } + }) + it('returns false when parent is svg element', function () { + expect(SVG().addTo(SVG()).isRoot()).toBe(false) + }) + }) + describe('remove()', function() { it('removes the doc from the dom only if doc is not root element', function() { var cnt = window.document.querySelectorAll('svg').length diff --git a/spec/spec/element.js b/spec/spec/element.js index 3f62e5a..ca8deff 100644 --- a/spec/spec/element.js +++ b/spec/spec/element.js @@ -485,11 +485,12 @@ describe('Element', function() { it('ungroups everything to the doc root when called on SVG.Doc / does not ungroup defs/parser', function() { draw.flatten() + expect(rect1.parent()).toBe(draw) expect(rect2.parent()).toBe(draw) expect(g1.node.parentNode).toBeFalsy() - expect(g1.node.parentNode).toBeFalsy() + expect(g2.node.parentNode).toBeFalsy() expect(nested.node.parentNode).toBeFalsy() expect(rect1.transform()).toEqual(jasmine.objectContaining({ diff --git a/spec/spec/event.js b/spec/spec/event.js index 0228fae..5f12172 100644 --- a/spec/spec/event.js +++ b/spec/spec/event.js @@ -10,7 +10,8 @@ describe('Event', function() { beforeEach(function() { rect = draw.rect(100, 100) - spyOn(SVG,'on').and.callThrough() + spyOn(SVG, 'on').and.callThrough() + spyOn(rect, 'dispatch').and.callThrough() }) afterEach(function() { @@ -25,8 +26,8 @@ describe('Event', function() { , 'mouseover' , 'mouseout' , 'mousemove' - // , 'mouseenter' -> not supported by IE - // , 'mouseleave' -> not supported by IE + , 'mouseenter' + , 'mouseleave' ].forEach(function(event) { describe(event+'()', function() { it('calls `on()` with '+event+' as event', function() { @@ -71,39 +72,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); + 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(Object.keys(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['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') @@ -115,7 +109,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) @@ -130,8 +124,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) @@ -139,7 +133,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() @@ -150,11 +144,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) @@ -162,7 +156,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() @@ -173,7 +168,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) @@ -192,7 +187,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) @@ -202,7 +197,20 @@ 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('detaches multiple listeners at once', function() { + rect2 = draw.rect(100,100) + rect3 = draw.rect(100,100) + + rect.on('event.namespace bla foo.bar otherfoo.bar keepthis', action) + rect.off('event.namespace bla .bar') + + expect(Object.keys(rect.events['event']).length).toBe(0) + expect(rect.events['bla']).toBeUndefined() + expect(Object.keys(rect.events['foo']).length).toBe(0) + expect(Object.keys(rect.events['otherfoo']).length).toBe(0) + expect(Object.keys(rect.events['keepthis']['*']).length).toBe(1) }) it('returns the called element', function() { expect(rect.off('event', action)).toBe(rect) @@ -218,12 +226,23 @@ 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) }) }) describe('fire()', function() { + it('calls dispatch with its parameters', function() { + var data = {} + rect.dispatch('event', data) + expect(rect.dispatch).toHaveBeenCalledWith('event', data) + }) + + it('returns the called element', function() { + expect(rect.fire('event')).toBe(rect) + }) + }) + describe('dispatch()', function() { beforeEach(function() { rect.on('event', action) }) @@ -234,9 +253,7 @@ describe('Event', function() { expect(toast).toBe('ready') expect(fruitsInDetail).toBe(null) }) - it('returns the called element', function() { - expect(rect.fire('event')).toBe(rect) - }) + it('fires event with additional data', function() { expect(fruitsInDetail).toBeNull() rect.fire('event', {apple:1}) @@ -248,27 +265,11 @@ describe('Event', function() { rect.fire(new window.CustomEvent('event')) expect(toast).toBe('ready') }) - it('makes the event cancelable', function() { + it('returns the dispatched event and makes it cancelable', function() { rect.on('event', function(e) { e.preventDefault() }) - rect.fire('event') - expect(rect._event.defaultPrevented).toBe(true) - }) - }) - - describe('event()', function() { - it('returns null when no event was fired', function() { - expect(rect.event()).toBe(null) - }) - it('returns the last fired event', function() { - var event = new window.CustomEvent('foo') - rect.fire(event) - expect(rect.event()).toBe(event) - - event = new window.CustomEvent('bar') - rect.fire(event) - expect(rect.event()).toBe(event) + expect(rect.dispatch('event').defaultPrevented).toBe(true) }) }) }) diff --git a/spec/spec/nested.js b/spec/spec/nested.js deleted file mode 100644 index 3113880..0000000 --- a/spec/spec/nested.js +++ /dev/null @@ -1,13 +0,0 @@ -describe('Nested', function() { - - afterEach(function() { - draw.clear() - }) - - describe('()', function() { - it('creates a nested svg of type SVG.Nested', function() { - expect(draw.nested() instanceof SVG.Nested).toBeTruthy() - }) - }) - -}) diff --git a/spec/spec/svg.js b/spec/spec/svg.js index 2485a32..ea51703 100644 --- a/spec/spec/svg.js +++ b/spec/spec/svg.js @@ -52,10 +52,10 @@ describe('SVG', function() { expect(SVG(rect).node).toBe(rect) }) - it('creates an instanceof SVG.Nested when importing a whole svg', function() { + it('creates an instanceof SVG.Doc when importing a whole svg', function() { var doc = SVG('<svg width="200"><rect></rect></svg>') - expect(doc instanceof SVG.Nested).toBe(true) + expect(doc instanceof SVG.Doc).toBe(true) expect(doc.node.nodeName).toBe('svg') expect(doc.width()).toBe(200) expect(doc.get(0).node.nodeName).toBe('rect') |