summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2020-04-10 17:59:56 +1000
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2020-04-10 17:59:56 +1000
commite497f9ccfb3107ba5c0991db5e1e07a2297948e6 (patch)
tree24874d4f674d1d9e8df806bcb940b338f7d3b7a1 /spec
parent6ac53822882f5cc852c8e8e41e9507333fed2180 (diff)
downloadsvg.js-e497f9ccfb3107ba5c0991db5e1e07a2297948e6.tar.gz
svg.js-e497f9ccfb3107ba5c0991db5e1e07a2297948e6.zip
added even more tests
Diffstat (limited to 'spec')
-rw-r--r--spec/spec/elements/G.js20
-rw-r--r--spec/spec/elements/Gradient.js26
-rw-r--r--spec/spec/elements/Pattern.js26
-rw-r--r--spec/spec/elements/Rect.js28
-rw-r--r--spec/spec/elements/Shape.js17
-rw-r--r--spec/spec/elements/Stop.js56
-rw-r--r--spec/spec/elements/Style.js85
-rw-r--r--spec/spec/elements/Symbol.js28
-rw-r--r--spec/spec/elements/Text.js22
9 files changed, 307 insertions, 1 deletions
diff --git a/spec/spec/elements/G.js b/spec/spec/elements/G.js
index d926256..b6850d5 100644
--- a/spec/spec/elements/G.js
+++ b/spec/spec/elements/G.js
@@ -65,6 +65,26 @@ describe('G.js', () => {
})
})
+ describe('dx()', () => {
+ it('calls dmove with dy=0 and returns itself', () => {
+ const canvas = SVG().addTo(container)
+ const g = canvas.group()
+ const spy = spyOn(g, 'dmove').and.callThrough()
+ expect(g.dx(10)).toBe(g)
+ expect(spy).toHaveBeenCalledWith(10, 0)
+ })
+ })
+
+ describe('dy()', () => {
+ it('calls dmove with dx=0 and returns itself', () => {
+ const canvas = SVG().addTo(container)
+ const g = canvas.group()
+ const spy = spyOn(g, 'dmove').and.callThrough()
+ expect(g.dy(10)).toBe(g)
+ expect(spy).toHaveBeenCalledWith(0, 10)
+ })
+ })
+
describe('move()', () => {
it('calls dmove() with the correct difference', () => {
const canvas = SVG().addTo(container)
diff --git a/spec/spec/elements/Gradient.js b/spec/spec/elements/Gradient.js
index 13e58a7..2d3120a 100644
--- a/spec/spec/elements/Gradient.js
+++ b/spec/spec/elements/Gradient.js
@@ -80,4 +80,30 @@ describe('Gradient.js', () => {
expect(gradient.url()).toBe('url("#foo")')
})
})
+
+ describe('Container', () => {
+ it('relays the call to defs', () => {
+ const canvas = new SVG()
+ const defs = canvas.defs()
+ const spy = spyOn(defs, 'gradient').and.callThrough()
+ const spy2 = createSpy('gradient')
+
+ canvas.gradient('linear', spy2)
+ expect(spy).toHaveBeenCalledWith('linear', spy2)
+ expect(spy2).toHaveBeenCalled()
+ })
+ })
+
+ describe('Defs', () => {
+ it('creates a pattern in the defs', () => {
+ const canvas = new SVG()
+ const defs = canvas.defs()
+ const spy = createSpy('gradient')
+ const gradient = defs.gradient('linear', spy)
+ expect(gradient).toEqual(any(Gradient))
+ expect(gradient.type).toBe('linearGradient')
+ expect(defs.children()).toEqual([ gradient ])
+ expect(spy).toHaveBeenCalled()
+ })
+ })
})
diff --git a/spec/spec/elements/Pattern.js b/spec/spec/elements/Pattern.js
index fa6c0b8..432a86e 100644
--- a/spec/spec/elements/Pattern.js
+++ b/spec/spec/elements/Pattern.js
@@ -73,4 +73,30 @@ describe('Pattern.js', () => {
expect(pattern.url()).toBe('url("#foo")')
})
})
+
+ describe('Container', () => {
+ it('relays the call to defs', () => {
+ const canvas = new SVG()
+ const defs = canvas.defs()
+ const spy = spyOn(defs, 'pattern').and.callThrough()
+ const spy2 = createSpy('pattern')
+
+ canvas.pattern(100, 100, spy2)
+ expect(spy).toHaveBeenCalledWith(100, 100, spy2)
+ expect(spy2).toHaveBeenCalled()
+ })
+ })
+
+ describe('Defs', () => {
+ it('creates a pattern in the defs and sets its size and position', () => {
+ const canvas = new SVG()
+ const defs = canvas.defs()
+ const spy = createSpy('pattern')
+ const pattern = defs.pattern(100, 100, spy)
+ expect(pattern).toEqual(any(Pattern))
+ expect(defs.children()).toEqual([ pattern ])
+ expect(spy).toHaveBeenCalled()
+ expect(pattern.attr([ 'x', 'y', 'width', 'height' ])).toEqual({ x: 0, y: 0, width: 100, height: 100 })
+ })
+ })
})
diff --git a/spec/spec/elements/Rect.js b/spec/spec/elements/Rect.js
new file mode 100644
index 0000000..230078f
--- /dev/null
+++ b/spec/spec/elements/Rect.js
@@ -0,0 +1,28 @@
+/* globals describe, expect, it, jasmine */
+
+import { Rect, G } from '../../../src/main.js'
+
+const { any } = jasmine
+
+describe('Rect.js', () => {
+ describe('()', () => {
+ it('creates a new object of type Rect', () => {
+ expect(new Rect()).toEqual(any(Rect))
+ })
+
+ it('sets passed attributes on the element', () => {
+ expect(new Rect({ id: 'foo' }).id()).toBe('foo')
+ })
+ })
+
+ describe('Container', () => {
+ describe('rect()', () => {
+ it('creates a rect with given size', () => {
+ const group = new G()
+ const rect = group.rect(100, 100)
+ expect(rect.attr([ 'width', 'height' ])).toEqual({ width: 100, height: 100 })
+ expect(rect).toEqual(any(Rect))
+ })
+ })
+ })
+})
diff --git a/spec/spec/elements/Shape.js b/spec/spec/elements/Shape.js
new file mode 100644
index 0000000..3d1976e
--- /dev/null
+++ b/spec/spec/elements/Shape.js
@@ -0,0 +1,17 @@
+/* globals describe, expect, it, jasmine */
+
+import { Shape, create } from '../../../src/main.js'
+
+const { any } = jasmine
+
+describe('Rect.js', () => {
+ describe('()', () => {
+ it('creates a new object of type Shape', () => {
+ expect(new Shape(create('rect'))).toEqual(any(Shape))
+ })
+
+ it('sets passed attributes on the element', () => {
+ expect(new Shape(create('rect'), { id: 'foo' }).id()).toBe('foo')
+ })
+ })
+})
diff --git a/spec/spec/elements/Stop.js b/spec/spec/elements/Stop.js
new file mode 100644
index 0000000..b6f331d
--- /dev/null
+++ b/spec/spec/elements/Stop.js
@@ -0,0 +1,56 @@
+/* globals describe, expect, it, jasmine */
+
+import { Stop, Gradient } from '../../../src/main.js'
+
+const { any } = jasmine
+
+describe('Stop.js', () => {
+ describe('()', () => {
+ it('creates a new object of type Stop', () => {
+ expect(new Stop()).toEqual(any(Stop))
+ })
+
+ it('sets passed attributes on the element', () => {
+ expect(new Stop({ id: 'foo' }).id()).toBe('foo')
+ })
+ })
+
+ describe('update()', () => {
+ it('sets offset, color and opacity with 3 arguments given', () => {
+ const stop = new Stop()
+ stop.update(0.1, '#ffffff', 0.5)
+ expect(stop.attr('offset')).toBe(0.1)
+ expect(stop.attr('stop-color')).toBe('#ffffff')
+ expect(stop.attr('stop-opacity')).toBe(0.5)
+ })
+
+ it('sets offset, color and opacity with object given', () => {
+ const stop = new Stop()
+ stop.update({ offset: 0.1, color: '#ffffff', opacity: 0.5 })
+ expect(stop.attr('offset')).toBe(0.1)
+ expect(stop.attr('stop-color')).toBe('#ffffff')
+ expect(stop.attr('stop-opacity')).toBe(0.5)
+ })
+ })
+
+ describe('Gradient', () => {
+ describe('stop()', () => {
+ it('creates a stop in the gradient with 3 arguments', () => {
+ const gradient = new Gradient('linear')
+ const stop = gradient.stop(0.1, '#ffffff', 0.5)
+ expect(stop).toEqual(any(Stop))
+ expect(stop.attr('offset')).toBe(0.1)
+ expect(stop.attr('stop-color')).toBe('#ffffff')
+ expect(stop.attr('stop-opacity')).toBe(0.5)
+ })
+
+ it('creates stop in the gradient with object given', () => {
+ const gradient = new Gradient('linear')
+ const stop = gradient.stop({ offset: 0.1, color: '#ffffff', opacity: 0.5 })
+ expect(stop.attr('offset')).toBe(0.1)
+ expect(stop.attr('stop-color')).toBe('#ffffff')
+ expect(stop.attr('stop-opacity')).toBe(0.5)
+ })
+ })
+ })
+})
diff --git a/spec/spec/elements/Style.js b/spec/spec/elements/Style.js
new file mode 100644
index 0000000..7b69dd3
--- /dev/null
+++ b/spec/spec/elements/Style.js
@@ -0,0 +1,85 @@
+/* globals describe, expect, it, jasmine */
+
+import { Style, G } from '../../../src/main.js'
+
+const { any } = jasmine
+
+describe('Style.js', () => {
+ describe('()', () => {
+ it('creates a new object of type Style', () => {
+ expect(new Style()).toEqual(any(Style))
+ })
+
+ it('sets passed attributes on the element', () => {
+ expect(new Style({ id: 'foo' }).id()).toBe('foo')
+ })
+ })
+
+ describe('addText()', () => {
+ it('appends a string to the current textContent and returns itself', () => {
+ const style = new Style()
+ expect(style.addText('foo').node.textContent).toBe('foo')
+ expect(style.addText('bar').node.textContent).toBe('foobar')
+ expect(style.addText('foobar')).toBe(style)
+ })
+
+ it('appends an empty string if nothing passed', () => {
+ const style = new Style()
+ expect(style.addText().node.textContent).toBe('')
+ })
+ })
+
+ describe('font()', () => {
+ it('adds a font-face rule to load a custom font and returns itself', () => {
+ const style = new Style()
+ expect(style.font('fontName', 'url')).toBe(style)
+ expect(style.node.textContent).toBe('@font-face{font-family:fontName;src:url;}')
+ })
+
+ it('adds extra parameters if wanted', () => {
+ const style = new Style()
+ style.font('fontName', 'url', { foo: 'bar' })
+ expect(style.node.textContent).toBe('@font-face{font-family:fontName;src:url;foo:bar;}')
+ })
+ })
+
+ describe('rule()', () => {
+ it('adds a css rule', () => {
+ const style = new Style()
+ expect(style.rule('#id', { fontSize: 15 })).toBe(style)
+ expect(style.node.textContent).toBe('#id{font-size:15;}')
+ })
+
+ it('adds only selector when no obj was given', () => {
+ const style = new Style()
+ style.rule('#id')
+ expect(style.node.textContent).toBe('#id')
+ })
+
+ it('adds nothing if no selector was given', () => {
+ const style = new Style()
+ style.rule()
+ expect(style.node.textContent).toBe('')
+ })
+ })
+
+ describe('Container', () => {
+ describe('style()', () => {
+ it('creates a style element in the container and adds a rule', () => {
+ const g = new G()
+ const style = g.style('#id', { fontSize: 15 })
+ expect(style).toEqual(any(Style))
+ expect(style.node.textContent).toBe('#id{font-size:15;}')
+ })
+ })
+
+ describe('fontface()', () => {
+ it('creates a style element in the container and adds a font-face rule', () => {
+ const g = new G()
+ const style = g.fontface('fontName', 'url', { foo: 'bar' })
+ expect(style).toEqual(any(Style))
+ expect(style.node.textContent).toBe('@font-face{font-family:fontName;src:url;foo:bar;}')
+ })
+ })
+ })
+})
diff --git a/spec/spec/elements/Symbol.js b/spec/spec/elements/Symbol.js
new file mode 100644
index 0000000..12be314
--- /dev/null
+++ b/spec/spec/elements/Symbol.js
@@ -0,0 +1,28 @@
+/* globals describe, expect, it, jasmine */
+
+import { Symbol, G } from '../../../src/main.js'
+
+const { any } = jasmine
+
+describe('Symbol.js', () => {
+ describe('()', () => {
+ it('creates a new object of type Symbol', () => {
+ expect(new Symbol()).toEqual(any(Symbol))
+ })
+
+ it('sets passed attributes on the element', () => {
+ expect(new Symbol({ id: 'foo' }).id()).toBe('foo')
+ })
+ })
+
+ describe('Container', () => {
+ describe('symbol()', () => {
+ it('creates a symbol in the container', () => {
+ const g = new G()
+ const symbol = g.symbol()
+ expect(symbol).toEqual(any(Symbol))
+ expect(g.children()).toEqual([ symbol ])
+ })
+ })
+ })
+})
diff --git a/spec/spec/elements/Text.js b/spec/spec/elements/Text.js
index ad47bbe..f28c256 100644
--- a/spec/spec/elements/Text.js
+++ b/spec/spec/elements/Text.js
@@ -1,6 +1,6 @@
/* globals describe, expect, it, spyOn jasmine, container */
-import { Text, Number as SVGNumber, SVG, G } from '../../../src/main.js'
+import { Text, Number as SVGNumber, SVG, G, Path, TextPath } from '../../../src/main.js'
const { any } = jasmine
@@ -30,11 +30,31 @@ describe('Text.js', () => {
expect(text.get(2).node.textContent).toBe('going')
})
+ it('increases dy after empty line', () => {
+ const canvas = SVG().addTo(container)
+ const text = canvas.text('Hello World\n\nHow is it\ngoing')
+ expect(text.children().length).toBe(4)
+ expect(text.get(0).node.textContent).toBe('Hello World')
+ expect(text.get(1).node.textContent).toBe('')
+ expect(text.get(2).node.textContent).toBe('How is it')
+ expect(text.get(3).node.textContent).toBe('going')
+ expect(text.get(2).dy()).toBe(text.get(3).dy() * 2)
+ })
+
it('returns the correct text with newlines', () => {
const text = new Text().text('Hello World\nHow is it\ngoing')
expect(text.text()).toBe('Hello World\nHow is it\ngoing')
})
+ it('returns the correct text with newlines and skips textPaths', () => {
+ const path = new Path()
+ const text = new Text()
+ const textPath = text.text('Hello World\nHow is it\ngoing').path(path)
+ textPath.children().addTo(text)
+ text.add(new TextPath(), 3)
+ expect(text.text()).toBe('Hello World\nHow is it\ngoing')
+ })
+
it('executes passed block', () => {
const text = new Text()
text.text(function (t) {