summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2020-04-05 20:51:35 +1000
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2020-04-05 20:51:35 +1000
commit099bf6efad9c9971d805c8496a63314d91a71692 (patch)
tree515abd93a158e69d2ebfed726797c3a0af9206d0 /spec
parent8a6b109353d1e3ff24b996ffa3f412fb2f94de90 (diff)
downloadsvg.js-099bf6efad9c9971d805c8496a63314d91a71692.tar.gz
svg.js-099bf6efad9c9971d805c8496a63314d91a71692.zip
added second parameter to `addTo` and `putIn`. Added deep clone parameter to `clone`. Tests for Dom
Diffstat (limited to 'spec')
-rw-r--r--spec/spec/elements/Dom.js250
1 files changed, 247 insertions, 3 deletions
diff --git a/spec/spec/elements/Dom.js b/spec/spec/elements/Dom.js
index 8004317..a1e2a6d 100644
--- a/spec/spec/elements/Dom.js
+++ b/spec/spec/elements/Dom.js
@@ -1,10 +1,254 @@
-/* globals describe, expect, it, beforeEach, jasmine, container */
+/* globals describe, expect, it, beforeEach, spyOn, jasmine, container */
-import { SVG, G, Rect, Svg } from '../../../src/main.js'
+import { SVG, G, Rect, Svg, Dom, List } from '../../../src/main.js'
import { getWindow } from '../../../src/utils/window.js'
-const { any } = jasmine
+const { any, createSpy, objectContaining } = jasmine
describe('Dom.js', function () {
+
+ describe('()', () => {
+ it('creates a new object of type Dom', () => {
+ const rect = new Rect()
+ expect(new Dom(rect.node)).toEqual(any(Dom))
+ })
+
+ it('sets passed attributes on the element', () => {
+ const rect = new Rect()
+ expect(new Dom(rect.node, { id: 'foo' }).id()).toBe('foo')
+ })
+
+ it('references the passed node on the instance', () => {
+ const rect = new Rect()
+ expect(new Dom(rect.node).node).toBe(rect.node)
+ })
+
+ it('sets the type according to the nodename', () => {
+ const rect = new Rect()
+ expect(new Dom(rect.node).type).toBe(rect.node.nodeName)
+ })
+ })
+
+ describe('add()', () => {
+ it('adds an element as child to the end with no second argument given', () => {
+ const g = new G()
+ g.add(new Rect())
+ const rect = new Rect()
+ g.add(rect)
+ expect(g.children().length).toBe(2)
+ expect(g.get(1)).toBe(rect)
+ })
+
+ it('adds an element at the specified position with second argument given', () => {
+ const g = new G()
+ g.add(new Rect())
+ g.add(new Rect())
+ const rect = new Rect()
+ g.add(rect, 1)
+ expect(g.children().length).toBe(3)
+ expect(g.get(1)).toBe(rect)
+ })
+
+ it('handles svg strings', () => {
+ const g = new G()
+ g.add('<rect>')
+ expect(g.children().length).toBe(1)
+ expect(g.get(0)).toEqual(any(Rect))
+ })
+
+ it('handles query selectors', () => {
+ const canvas = SVG().addTo(container)
+ const rect = canvas.rect(100, 100).addClass('test')
+ const g = canvas.group()
+ g.add('.test')
+ expect(g.children().length).toBe(1)
+ expect(g.get(0)).toBe(rect)
+ })
+ })
+
+ describe('addTo()', () => {
+ it('returns the current element', () => {
+ const g = new G()
+ const rect = new Rect()
+ expect(rect.addTo(g)).toBe(rect)
+ })
+
+ it('puts an element innto another element', () => {
+ const g = new G()
+ const rect = new Rect()
+ const spy = spyOn(g, 'put')
+ rect.addTo(g, 0)
+ expect(spy).toHaveBeenCalledWith(rect, 0)
+ })
+
+ it('works with svg strings', () => {
+ const rect = new Rect()
+ rect.addTo('<g>')
+ expect(rect.parent()).toEqual(any(G))
+ })
+
+ it('works with query selector', () => {
+ const canvas = SVG().addTo(container)
+ const rect = canvas.rect(100, 100)
+ const g = canvas.group().addClass('test')
+ rect.addTo('.test')
+ expect(g.children().length).toBe(1)
+ expect(g.get(0)).toBe(rect)
+ })
+ })
+
+ describe('children()', () => {
+ it('returns a List of all children', () => {
+ const g = new G()
+ const rect = g.rect(100, 100)
+ const circle = g.circle(100, 100)
+ const children = g.children()
+ expect(children).toEqual([ rect, circle ])
+ expect(children).toEqual(any(List))
+ })
+ })
+
+ describe('clear()', () => {
+ it('returns the current element', () => {
+ const g = new G()
+ g.rect(100, 100)
+ g.circle(100, 100)
+ expect(g.clear()).toBe(g)
+ })
+
+ it('removes all children from an element', () => {
+ const g = new G()
+ g.rect(100, 100)
+ g.circle(100, 100)
+ g.clear()
+ expect(g.children()).toEqual([])
+ })
+ })
+
+ describe('clone()', () => {
+ it('clones the current element and returns it', () => {
+ const rect = new Rect()
+ const clone = rect.clone()
+ expect(rect).not.toBe(clone)
+ expect(clone).toEqual(any(Rect))
+ expect(clone.type).toBe(rect.type)
+ })
+
+ it('also clones the children by default', () => {
+ const group = new G()
+ const rect = group.rect(100, 100)
+ const clone = group.clone()
+ expect(clone.get(0)).not.toBe(rect)
+ expect(clone.get(0)).toEqual(any(Rect))
+ })
+
+ it('does not clone the children when passing false', () => {
+ const group = new G()
+ group.rect(100, 100)
+ const clone = group.clone(false)
+ expect(clone.children()).toEqual([])
+ })
+
+ it('assigns a new id to the element and to child elements', () => {
+ const group = new G().id('group')
+ const rect = group.rect(100, 100).id('rect')
+ const clone = group.clone()
+ expect(clone.get(0).id()).not.toBe(rect.id())
+ expect(clone.id()).not.toBe(group.id())
+ })
+ })
+
+ describe('each()', () => {
+ it('iterates over all children and executes the passed function on then', () => {
+ const group = new G()
+ const group2 = group.group()
+ const circle = group.circle(100, 100)
+ const spy = createSpy('each')
+ group.each(spy)
+
+ expect(spy.calls.all()).toEqual([
+ objectContaining({ object: group2, args: [ 0, [ group2, circle ] ] }),
+ objectContaining({ object: circle, args: [ 1, [ group2, circle ] ] })
+ ])
+ })
+
+ it('iterates over all children recursively and executes the passed function on then when deep is true', () => {
+ const group = new G()
+ const group2 = group.group()
+ const rect = group2.rect(100, 100)
+ const circle = group.circle(100, 100)
+ const spy = createSpy('each')
+ group.each(spy, true)
+
+ expect(spy.calls.all()).toEqual([
+ objectContaining({ object: group2, args: [ 0, [ group2, circle ] ] }),
+ objectContaining({ object: rect, args: [ 0, [ rect ] ] }),
+ objectContaining({ object: circle, args: [ 1, [ group2, circle ] ] })
+ ])
+ })
+ })
+
+ describe('element()', () => {
+ it('creates an element of given type and appends it to the current element', () => {
+ const g = new G()
+ const el = g.element('title')
+ expect(el).toEqual(any(Dom))
+ expect(el.type).toBe('title')
+ })
+
+ it('sets the specified attributes passed as second argument', () => {
+ const g = new G()
+ const el = g.element('title', { id: 'foo' })
+ expect(el.id()).toBe('foo')
+ })
+ })
+
+ describe('first()', () => {
+ it('returns the first child', () => {
+ const g = new G()
+ const rect = g.rect(100, 100)
+ g.circle(100, 100)
+ expect(g.first()).toBe(rect)
+ })
+ })
+
+ describe('get()', () => {
+ it('returns the child at the given position', () => {
+ const g = new G()
+ const rect = g.rect(100, 100)
+ const circle = g.circle(100, 100)
+ expect(g.get(0)).toBe(rect)
+ expect(g.get(1)).toBe(circle)
+ })
+ })
+
+ describe('getEventHolder()', () => {
+ it('returns the node because it holds all events on the object', () => {
+ const dom = new Dom({})
+ expect(dom.getEventHolder()).toBe(dom.node)
+ })
+ })
+
+ describe('getEventTarget()', () => {
+ it('returns the node because it is the target of the event', () => {
+ const dom = new Dom({})
+ expect(dom.getEventTarget()).toBe(dom.node)
+ })
+ })
+
+ describe('has()', () => {
+ it('returns true if the element has the passed element as child', () => {
+ const g = new G()
+ const rect = g.rect(100, 100)
+ expect(g.has(rect)).toBe(true)
+ })
+
+ it('returns false if the element hasn\'t the passed element as child', () => {
+ const g = new G()
+ const rect = new Rect()
+ expect(g.has(rect)).toBe(false)
+ })
+ })
+
describe('parent()', () => {
var canvas, rect, group1, group2