})
})
+ 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)
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()
+ })
+ })
})
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 })
+ })
+ })
})
--- /dev/null
+/* 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))
+ })
+ })
+ })
+})
--- /dev/null
+/* 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')
+ })
+ })
+})
--- /dev/null
+/* 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)
+ })
+ })
+ })
+})
--- /dev/null
+/* 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;}')
+ })
+ })
+ })
+})
--- /dev/null
+/* 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 ])
+ })
+ })
+ })
+})
/* 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
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) {
registerMethods({
Container: {
// Create gradient element in defs
- gradient: wrapWithAttrCheck(function (type, block) {
- return this.defs().gradient(type, block)
- })
+ gradient (...args) {
+ return this.defs().gradient(...args)
+ }
},
// define gradient
Defs: {
-import { nodeOrNew, register, wrapWithAttrCheck } from '../utils/adopter.js'
+import { nodeOrNew, register } from '../utils/adopter.js'
import { registerMethods } from '../utils/methods.js'
import { unCamelCase } from '../utils/utils.js'
import Element from './Element.js'
}
registerMethods('Dom', {
- style: wrapWithAttrCheck(function (selector, obj) {
+ style (selector, obj) {
return this.put(new Style()).rule(selector, obj)
- }),
- fontface: wrapWithAttrCheck(function (name, src, params) {
+ },
+ fontface (name, src, params) {
return this.put(new Style()).font(name, src, params)
- })
+ }
})
register(Style, 'Style')