summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2016-02-10 21:23:48 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2016-02-10 21:23:48 +0100
commit64784ffc12d1c4cffd23cd94a5430e3fc1544861 (patch)
treef19e17e492ee2b4127fe7c37ac377de3b4096ff7 /spec
parenta794401cda156048d921c575ac180aa704d0cd6c (diff)
parent2bf4788597959684f39e3e17777149a9ccb8ceab (diff)
downloadsvg.js-64784ffc12d1c4cffd23cd94a5430e3fc1544861.tar.gz
svg.js-64784ffc12d1c4cffd23cd94a5430e3fc1544861.zip
Merge branch 'master' into 3.0.0
Diffstat (limited to 'spec')
-rw-r--r--spec/spec/element.js80
-rw-r--r--spec/spec/group.js24
-rw-r--r--spec/spec/point.js120
-rw-r--r--spec/spec/regex.js53
-rw-r--r--spec/spec/text.js35
5 files changed, 264 insertions, 48 deletions
diff --git a/spec/spec/element.js b/spec/spec/element.js
index 10b8b75..96a5ff7 100644
--- a/spec/spec/element.js
+++ b/spec/spec/element.js
@@ -1,5 +1,5 @@
describe('Element', function() {
-
+
beforeEach(function() {
draw.attr('viewBox', null)
})
@@ -7,7 +7,7 @@ describe('Element', function() {
afterEach(function() {
draw.clear()
})
-
+
it('should create a circular reference on the node', function() {
var rect = draw.rect(100,100)
expect(rect.node.instance).toBe(rect)
@@ -19,10 +19,10 @@ describe('Element', function() {
expect(rect.native()).toBe(rect.node)
})
})
-
+
describe('attr()', function() {
var rect
-
+
beforeEach(function() {
rect = draw.rect(100,100)
})
@@ -103,11 +103,11 @@ describe('Element', function() {
describe('id()', function() {
var rect
-
+
beforeEach(function() {
rect = draw.rect(100,100)
})
-
+
it('gets the value if the id attribute without an argument', function() {
expect(rect.id()).toBe(rect.attr('id'))
})
@@ -116,7 +116,7 @@ describe('Element', function() {
expect(rect.attr('id')).toBe('new_id')
})
})
-
+
describe('style()', function() {
it('sets the style with key and value arguments', function() {
var rect = draw.rect(100,100).style('cursor', 'crosshair')
@@ -149,14 +149,14 @@ describe('Element', function() {
expect(rect.style()).toBe('')
})
})
-
+
describe('transform()', function() {
var rect, ctm
-
+
beforeEach(function() {
rect = draw.rect(100,100)
})
-
+
it('gets the current transformations', function() {
expect(rect.transform()).toEqual(new SVG.Matrix(rect).extract())
})
@@ -246,7 +246,7 @@ describe('Element', function() {
describe('untransform()', function() {
var circle
-
+
beforeEach(function() {
circle = draw.circle(100).translate(50, 100)
})
@@ -265,11 +265,11 @@ describe('Element', function() {
describe('ctm()', function() {
var rect
-
+
beforeEach(function() {
rect = draw.rect(100, 100)
})
-
+
it('gets the current transform matrix of the element', function() {
rect.translate(10, 20)
expect(rect.ctm().toString()).toBe('matrix(1,0,0,1,10,20)')
@@ -278,7 +278,7 @@ describe('Element', function() {
expect(rect.ctm() instanceof SVG.Matrix).toBeTruthy()
})
})
-
+
describe('data()', function() {
it('sets a data attribute and convert value to json', function() {
var rect = draw.rect(100,100).data('test', 'value')
@@ -310,10 +310,10 @@ describe('Element', function() {
it('maintains data type for an object', function() {
var rect = draw.rect(100,100).data('test', { string: 'value', array: [1,2,3] })
expect(typeof rect.data('test')).toBe('object')
- expect(Array.isArray(rect.data('test').array)).toBe(true)
+ expect(Array.isArray(rect.data('test').array)).toBe(true)
})
})
-
+
describe('remove()', function() {
it('removes an element and return it', function() {
var rect = draw.rect(100,100)
@@ -345,7 +345,7 @@ describe('Element', function() {
expect(rect.parent()).toBe(group)
})
})
-
+
describe('rbox()', function() {
it('returns an instance of SVG.RBox', function() {
var rect = draw.rect(100,100)
@@ -372,14 +372,14 @@ describe('Element', function() {
expect(box.height).toBe(210)
})
})
-
+
describe('doc()', function() {
it('returns the parent document', function() {
var rect = draw.rect(100,100)
expect(rect.doc()).toBe(draw)
})
})
-
+
describe('parent()', function() {
it('contains the parent svg', function() {
var rect = draw.rect(100,100)
@@ -402,7 +402,7 @@ describe('Element', function() {
expect(rect.parent('.test')).toBe(group1)
})
})
-
+
describe('parents()', function() {
it('returns array of parent up to but not including the dom element filtered by type', function() {
var group1 = draw.group().addClass('test')
@@ -415,7 +415,7 @@ describe('Element', function() {
expect(rect.parents().length).toBe(3)
})
})
-
+
describe('clone()', function() {
var rect, group, circle
@@ -463,20 +463,20 @@ describe('Element', function() {
var rectIndex = draw.children().indexOf(rect)
rect.replace(circle)
-
+
expect(rectIndex).toBe(draw.children().indexOf(circle))
})
it('removes the original element', function() {
var rect = draw.rect(100,100).center(321,567).fill('#f06')
rect.replace(draw.circle(200))
-
+
expect(draw.has(rect)).toBe(false)
})
it('returns the new element', function() {
var circle = draw.circle(200)
var element = draw.rect(100,100).center(321,567).fill('#f06').replace(circle)
-
+
expect(element).toBe(circle)
})
})
@@ -616,4 +616,36 @@ describe('Element', function() {
})
})
})
+
+ describe('writeDataToDom()', function() {
+ it('set all properties in el.dom to the svgjs:data attribute', function(){
+ var rect = draw.rect(100,100)
+ rect.dom.foo = 'bar'
+ rect.dom.number = new SVG.Number('3px')
+
+ rect.writeDataToDom()
+
+ expect(rect.attr('svgjs:data')).toBe('{"foo":"bar","number":"3px"}')
+ })
+ })
+
+ describe('setData()', function() {
+ it('read all data from the svgjs:data attribute and assign it to el.dom', function(){
+ var rect = draw.rect(100,100)
+
+ rect.attr('svgjs:data', '{"foo":"bar","number":"3px"}')
+ rect.setData(JSON.parse(rect.attr('svgjs:data')))
+
+ expect(rect.dom.foo).toBe('bar')
+ expect(rect.dom.number).toBe('3px')
+ })
+ })
+
+ describe('point()', function() {
+ it('creates a point from screen coordinates transformed in the elements space', function(){
+ var rect = draw.rect(100,100)
+ expect(rect.point(2,5).x).toBeCloseTo(-6)
+ expect(rect.point(2,5).y).toBeCloseTo(-3)
+ })
+ })
})
diff --git a/spec/spec/group.js b/spec/spec/group.js
index 88772e0..63335ae 100644
--- a/spec/spec/group.js
+++ b/spec/spec/group.js
@@ -5,7 +5,7 @@ describe('Group', function() {
group = draw.group().move(50, 50)
group.rect(100,100)
})
-
+
afterEach(function() {
draw.clear()
})
@@ -24,8 +24,13 @@ describe('Group', function() {
var box = group.gbox()
expect(box.x).toBe(13)
})
+ it('sets the value of x correctly when the first argument is a string number', function(){
+ group.x('123')
+ var box = group.gbox()
+ expect(box.x).toBe(123)
+ })
})
-
+
describe('y()', function() {
it('returns the value of y without an argument', function() {
expect(group.y()).toBe(50)
@@ -40,8 +45,13 @@ describe('Group', function() {
var box = group.gbox()
expect(box.y).toBe(15)
})
+ it('sets the value of y correctly when the first argument is a string number', function(){
+ group.y('124')
+ var box = group.gbox()
+ expect(box.y).toBe(124)
+ })
})
-
+
describe('cx()', function() {
it('returns the value of cx without an argument', function() {
expect(group.cx()).toBe(100)
@@ -52,7 +62,7 @@ describe('Group', function() {
expect(box.cx).toBe(123)
})
})
-
+
describe('cy()', function() {
it('returns the value of cy without an argument', function() {
expect(group.cy()).toBe(100)
@@ -70,7 +80,7 @@ describe('Group', function() {
expect(group.node.getAttribute('transform')).toBe('matrix(1,0,0,1,123,456)')
})
})
-
+
describe('center()', function() {
it('sets the cx and cy position', function() {
group.center(321,567)
@@ -103,6 +113,6 @@ describe('Group', function() {
expect(group.node.getAttribute('transform')).toBe('matrix(1,0,0,1,130,85)')
})
})
-
-}) \ No newline at end of file
+
+})
diff --git a/spec/spec/point.js b/spec/spec/point.js
new file mode 100644
index 0000000..ce28781
--- /dev/null
+++ b/spec/spec/point.js
@@ -0,0 +1,120 @@
+describe('Point', function() {
+ var point
+
+ describe('initialization', function() {
+
+ describe('without a source', function() {
+
+ point(function() {
+ matrix = new SVG.Point
+ })
+
+ it('creates a new point with default values', function() {
+ expect(point.x).toBe(0)
+ expect(point.y).toBe(0)
+ })
+
+ })
+
+ describe('with x and y given', function() {
+ it('creates a point with given values', function() {
+ var point = new SVG.Point(2,4)
+
+ expect(point.x).toBe(2)
+ expect(point.y).toBe(4)
+ })
+ })
+
+ describe('with array given', function() {
+ it('creates a point from array', function() {
+ var point = new SVG.Point([2,4])
+
+ expect(point.x).toBe(2)
+ expect(point.y).toBe(4)
+ })
+ })
+
+ describe('with object given', function() {
+ it('creates a point from object', function() {
+ var point = new SVG.Point({x:2,y:4})
+
+ expect(point.x).toBe(2)
+ expect(point.y).toBe(4)
+ })
+ })
+
+ describe('with SVG.Point given', function() {
+ it('creates a point from SVG.Point', function() {
+ var point = new SVG.Point(new SVG.Point(2,4))
+
+ expect(point.x).toBe(2)
+ expect(point.y).toBe(4)
+ })
+ })
+
+ describe('with native SVGPoint given', function() {
+ it('creates a point from native SVGPoint', function() {
+ var point = new SVG.Point(new SVG.Point(2,4).native())
+
+ expect(point.x).toBe(2)
+ expect(point.y).toBe(4)
+ })
+ })
+
+ })
+
+ describe('clone()', function() {
+ it('returns cloned point', function() {
+ var point1 = new SVG.Point(1,1)
+ , point2 = new SVG.Point(point1)
+
+ expect(point1).toEqual(point2)
+ expect(point1).not.toBe(point2)
+ })
+ })
+
+ describe('morph()', function() {
+ it('stores a given point for morphing', function() {
+ var point1 = new SVG.Point(1,1)
+ , point2 = new SVG.Matrix(2,2)
+
+ point1.morph(point2)
+
+ expect(point1.destination).toEqual(point2)
+ })
+ it('stores a clone, not the given matrix itself', function() {
+ var point1 = new SVG.Point(1,1)
+ , point2 = new SVG.Matrix(2,2)
+
+ point1.morph(point2)
+
+ expect(point1.destination).not.toBe(point2)
+ })
+ })
+
+ describe('at()', function() {
+ it('returns a morphed point at a given position', function() {
+ var point1 = new SVG.Point(1,1)
+ , point2 = new SVG.Point(2,2)
+ , matrix3 = matrix1.morph(matrix2).at(0.5)
+
+ expect(matrix3).toEqual(new SVG.Point(1.5, 1.5))
+ })
+ })
+
+ describe('transform()', function() {
+ it('returns a point transformed with given matrix', function() {
+ var point = new SVG.Point(1,5)
+ , matrix = new SVG.Matrix(0,0,1,0,0,1)
+
+ expect(point.transform(matrox)).toEqual(new SVG.Point(5,1))
+ })
+ }
+
+ describe('native()', function() {
+ it('returns native SVGPoint', function() {
+ expect(new SVG.Point().native() instanceof SVGPoint).toBeTruthy()
+ })
+ })
+
+}) \ No newline at end of file
diff --git a/spec/spec/regex.js b/spec/spec/regex.js
index f017059..9a14bec 100644
--- a/spec/spec/regex.js
+++ b/spec/spec/regex.js
@@ -1,23 +1,46 @@
describe('Regex', function() {
describe('matchers', function() {
- describe('unit', function() {
+
+ describe('numberAndUnit', function() {
var match
it('is true with a positive unit value', function() {
- match = ('10%').match(SVG.regex.unit)
+ match = ('10%').match(SVG.regex.numberAndUnit)
expect(match[1]).toBe('10')
- expect(match[2]).toBe('%')
+ expect(match[5]).toBe('%')
})
it('is true with a negative unit value', function() {
- match = ('-11%').match(SVG.regex.unit)
+ match = ('-11%').match(SVG.regex.numberAndUnit)
expect(match[1]).toBe('-11')
- expect(match[2]).toBe('%')
+ expect(match[5]).toBe('%')
})
it('is false with a positive unit value', function() {
- match = ('NotAUnit').match(SVG.regex.unit)
+ match = ('NotAUnit').match(SVG.regex.numberAndUnit)
expect(match).toBeNull()
})
+
+ it('is true with a number', function() {
+ ["1", "-1", "+15", "1.55", ".5", "5.", "1.3e2", "1E-4", "1e+12"].forEach(function(s) {
+ expect(SVG.regex.numberAndUnit.test(s)).toBeTruthy()
+ })
+ })
+ it('is false with a faulty number', function() {
+ ["+-1", "1.2.3", "1+1", "1e4.5", ".5.", "1f5", "."].forEach(function(s) {
+ expect(SVG.regex.numberAndUnit.test(s)).toBeFalsy()
+ })
+ })
+ it('is true with a number with unit', function() {
+ ["1px", "-1em", "+15%", "1.55s", ".5pt", "5.deg", "1.3e2rad", "1E-4grad", "1e+12cm"].forEach(function(s) {
+ expect(SVG.regex.numberAndUnit.test(s)).toBeTruthy()
+ })
+ })
+ it('is false with a faulty number or wrong unit', function() {
+ ["1em1", "-1eq,5"].forEach(function(s) {
+ expect(SVG.regex.numberAndUnit.test(s)).toBeFalsy()
+ })
+ })
+
})
})
@@ -46,6 +69,24 @@ describe('Regex', function() {
})
})
+ describe('isNumber', function() {
+
+ it('is true with a number', function() {
+ ["1", "-1", "+15", "1.55", ".5", "5.", "1.3e2", "1E-4", "1e+12"].forEach(function(s) {
+ expect(SVG.regex.isNumber.test(s)).toBeTruthy()
+ })
+ })
+
+ it('is false with a faulty number', function() {
+ ["1a", "+-1", "1.2.3", "1+1", "1e4.5", ".5.", "1f5", "."].forEach(function(s) {
+ expect(SVG.regex.isNumber.test(s)).toBeFalsy()
+ })
+ })
+
+ })
+
+
+
})
}) \ No newline at end of file
diff --git a/spec/spec/text.js b/spec/spec/text.js
index 39fba10..b60a97d 100644
--- a/spec/spec/text.js
+++ b/spec/spec/text.js
@@ -4,15 +4,15 @@
describe('Text', function() {
var text
-
+
beforeEach(function() {
text = draw.text(loremIpsum).size(5)
})
-
+
afterEach(function() {
draw.clear()
})
-
+
describe('x()', function() {
it('returns the value of x without an argument', function() {
expect(text.x()).toBe(0)
@@ -28,7 +28,7 @@ describe('Text', function() {
expect(box.x).toBeCloseTo(123)
})
})
-
+
describe('y()', function() {
it('returns the value of y without an argument', function() {
expect(text.y(0).y()).toBe(0)
@@ -46,7 +46,7 @@ describe('Text', function() {
expect(text.node.getAttribute('y')).toBe('40%')
})
})
-
+
describe('cx()', function() {
it('returns the value of cx without an argument', function() {
var box = text.bbox()
@@ -63,7 +63,7 @@ describe('Text', function() {
expect(box.cx).toBeCloseTo(123)
})
})
-
+
describe('cy()', function() {
it('returns the value of cy without an argument', function() {
var box = text.bbox()
@@ -75,7 +75,7 @@ describe('Text', function() {
expect(Math.round(box.cy * 10) / 10).toBe(345)
})
})
-
+
describe('move()', function() {
it('sets the x and y position', function() {
text.move(123,456)
@@ -84,7 +84,7 @@ describe('Text', function() {
expect(box.y).toBeCloseTo(456)
})
})
-
+
describe('center()', function() {
it('sets the cx and cy position', function() {
text.center(321, 567)
@@ -93,7 +93,7 @@ describe('Text', function() {
expect(box.cy).toBeCloseTo(567, 1)
})
})
-
+
describe('size()', function() {
it('should define the width and height of the element', function() {
text.size(50)
@@ -212,7 +212,7 @@ describe('Text', function() {
expect(text.length()).toBeCloseTo(text.lines().get(0).length() + text.lines().get(1).length() + text.lines().get(2).length(), 3)
})
})
-
+
describe('build()', function() {
it('enables adding multiple plain text nodes when given true', function() {
text.clear().build(true)
@@ -243,7 +243,20 @@ describe('Text', function() {
expect(text.node.childNodes[1]).toBe(undefined)
})
})
-
+
+ describe('setData()', function() {
+ it('read all data from the svgjs:data attribute and assign it to el.dom', function(){
+
+ text.attr('svgjs:data', '{"foo":"bar","leading":"3px"}')
+ text.setData(JSON.parse(text.attr('svgjs:data')))
+
+ expect(text.dom.foo).toBe('bar')
+ expect(text.dom.leading instanceof SVG.Number).toBeTruthy()
+ expect(text.dom.leading.value).toBe(3)
+ expect(text.dom.leading.unit).toBe('px')
+ })
+ })
+
})