From: Ulrich-Matthias Schäfer Date: Sat, 25 Feb 2017 12:52:45 +0000 (+0100) Subject: added test cases to increase code coverage X-Git-Tag: 2.5.0~43 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e7c025d2a4f8f536432870ebac903e24392896f0;p=svg.js.git added test cases to increase code coverage --- diff --git a/spec/spec/array.js b/spec/spec/array.js index b7e2a29..e3bd926 100644 --- a/spec/spec/array.js +++ b/spec/spec/array.js @@ -92,10 +92,12 @@ describe('Array', function () { }) }) describe('at()', function() { - it('returns a new array instance', function() { + beforeEach(function() { arr1 = new SVG.Array([1,2,3,4]) arr2 = new SVG.Array([2,3,4,5]) - + }) + + it('returns a new array instance', function() { arr1.morph(arr2) start = arr1.at(0) @@ -108,20 +110,11 @@ describe('Array', function () { expect(end).not.toBe(arr2) }) it('morphs all values of the array', function() { - arr1 = new SVG.Array([1,2,3,4]) - arr2 = new SVG.Array([2,3,4,5]) - arr1.morph(arr2) - expect(arr1.at(0.5).value).toEqual([1.5, 2.5, 3.5, 4.5]) }) - it('returns array if no destination was specified', function() { - arr1 = new SVG.Array([1,2,3,4]) - - arr2 = arr1.at(0.5) - - expect(arr2.value).toEqual([1,2,3,4]) - expect(arr2).toBe(arr1) + it('returns itself if no destination was specified', function() { + expect(arr1.at(0.5)).toBe(arr1) }) }) }) @@ -187,6 +180,35 @@ describe('PointArray', function () { expect(array.value).toEqual([[1,2]]) }) + + describe('at()', function() { + var arr1, arr2 + + beforeEach(function() { + arr1 = new SVG.PointArray([[1,2],[3,4]]) + arr2 = new SVG.Array([[2,3],[4,5]]) + }) + + it('returns a new array instance', function() { + arr1.morph(arr2) + + start = arr1.at(0) + end = arr1.at(1) + + expect(start instanceof SVG.PointArray).toBeTruthy() + expect(start).not.toBe(arr1) + + expect(end instanceof SVG.PointArray).toBeTruthy() + expect(end).not.toBe(arr2) + }) + it('morphs all values of the array', function() { + arr1.morph(arr2) + expect(arr1.at(0.5).value).toEqual([[1.5, 2.5], [3.5, 4.5]]) + }) + it('returns itself if no destination was specified', function() { + expect(arr1.at(0.5)).toBe(arr1) + }) + }) }) describe('PathArray', function () { @@ -205,7 +227,21 @@ describe('PathArray', function () { expect(p2.toString()).toBe('M10 80C50 90 75 90 105 160S255 310 285 240T585 540Q637 550 680 620Z ') expect(p3.toString()).toBe('M80 80A45 45 0 0 0 125 125L125 80Z ') expect(p4.toString()).toBe('M215.458 245.23C215.458 245.23 292.861 245.23 309.73199999999997 245.23S405 216.451 405 138.054S329.581 15 287.9 15C246.21999999999997 15 147.97599999999997 15 117.21199999999999 15C86.45 15 15 60.65 15 134.084C15 207.518 111.259 246.221 129.122 246.221C146.984 246.221 215.458 245.23 215.458 245.23Z ') - + }) + + // this test is designed to cover a certain line but it doesnt work because of #608 + it('returns the valueOf when PathArray is given', function() { + var p = new SVG.PathArray('m10 10 h 80 v 80 h -80 l 300 400 z') + + expect((new SVG.PathArray(p)).value).toEqual(p.value) + }) + + it('can handle all formats which can be used', function() { + // when no command is specified after move, line is used automatically (specs say so) + expect(new SVG.PathArray('M10 10 80 80 30 30 Z').toString()).toBe('M10 10L80 80L30 30Z ') + + // parsing can handle 0.5.3.3.2 stuff + expect(new SVG.PathArray('M10 10L.5.5.3.3Z').toString()).toBe('M10 10L0.5 0.5L0.3 0.3Z ') }) describe('move()', function() { diff --git a/spec/spec/clip.js b/spec/spec/clip.js index eba8df4..c641c4c 100644 --- a/spec/spec/clip.js +++ b/spec/spec/clip.js @@ -30,6 +30,11 @@ describe('ClipPath', function() { it('references the clipped element in the clipPath target list', function() { expect(rect.clipper.targets.indexOf(rect) > -1).toBe(true) }) + + it('reuses clip element when clip was given', function() { + var clip = rect.clipper + expect(draw.rect(100,100).clipWith(clip).clipper).toBe(clip) + }) it('unclips all clipped elements when being removed', function() { rect.clipper.remove() diff --git a/spec/spec/color.js b/spec/spec/color.js index 6bae4c8..1e86544 100644 --- a/spec/spec/color.js +++ b/spec/spec/color.js @@ -76,6 +76,10 @@ describe('Color', function() { expect(morphed.g).toBe(102) expect(morphed.b).toBe(255) }) + + it('returns itself when no destination specified', function() { + expect(color.at(0.5)).toBe(color) + }) }) }) diff --git a/spec/spec/container.js b/spec/spec/container.js index cb6c84c..a027c5e 100644 --- a/spec/spec/container.js +++ b/spec/spec/container.js @@ -284,6 +284,26 @@ describe('Container', function() { expect(draw.get(3)).toBeNull() }) }) + + describe('first()', function() { + it('gets the first child', function() { + draw.clear() + var rect = draw.rect(100,100) + var circle = draw.circle(100) + var line = draw.line(0,0,100,100) + expect(draw.first()).toBe(rect) + }) + }) + + describe('last()', function() { + it('gets the last child', function() { + draw.clear() + var rect = draw.rect(100,100) + var circle = draw.circle(100) + var line = draw.line(0,0,100,100) + expect(draw.last()).toBe(line) + }) + }) describe('has()', function() { it('determines if a given element is a child of the parent', function() { @@ -320,6 +340,14 @@ describe('Container', function() { }) }) + describe('defs()', function() { + it('returns the defs from the svg', function() { + var g = draw.group() + expect(g.defs()).toBe(draw.doc().defs()) + expect(g.defs() instanceof SVG.Defs).toBeTruthy() + }) + }) + }) diff --git a/spec/spec/easing.js b/spec/spec/easing.js new file mode 100644 index 0000000..04690ac --- /dev/null +++ b/spec/spec/easing.js @@ -0,0 +1,22 @@ +describe('SVG.easing', function() { + var easedValues = { + '-':0.5, + '<>':0.5, + '>':0.7071, + '<':0.2929, + } + + ;['-', '<>', '<', '>'].forEach(function(el) { + describe(el, function() { + it('is 0 at 0', function() { + expect(SVG.easing[el](0)).toBe(0) + }) + it('is 1 at 1', function() { + expect(Math.round(SVG.easing[el](1)*1000)/1000).toBe(1) // we need to round cause for some reason at some point 1==0.999999999 + }) + it('is eased at 0.5', function() { + expect(SVG.easing[el](0.5)).toBeCloseTo(easedValues[el]) + }) + }) + }) +}) diff --git a/spec/spec/element.js b/spec/spec/element.js index 3482948..0237c94 100644 --- a/spec/spec/element.js +++ b/spec/spec/element.js @@ -702,6 +702,23 @@ describe('Element', function() { expect(rect.attr('svgjs:data')).toBe('{"foo":"bar","number":"3px"}') }) + it('recursively dumps the data', function() { + var g = draw.group() + rect = g.rect(100,100) + g.dom.foo = 'bar' + rect.dom.number = new SVG.Number('3px') + + g.writeDataToDom() + + expect(g.attr('svgjs:data')).toBe('{"foo":"bar"}') + expect(rect.attr('svgjs:data')).toBe('{"number":"3px"}') + }) + it('uses lines() instead of each() when dealing with text', function() { + var text = draw.text('Hello\nWorld') + text.writeDataToDom() + expect(text.attr('svgjs:data')).toBe('{"leading":"1.3"}') + expect(text.lines().first().attr('svgjs:data')).toBe('{"newLined":true}') + }) }) describe('setData()', function() { diff --git a/spec/spec/event.js b/spec/spec/event.js index 5390472..3b07520 100644 --- a/spec/spec/event.js +++ b/spec/spec/event.js @@ -291,10 +291,17 @@ describe('Event', function() { describe('on()', function() { - it('attaches and event to the element', function() { + it('attaches an event to the element', function() { dispatchEvent(rect.on('event', action), 'event') expect(toast).toBe('ready') }) + it('attaches an event to a non svg element', function() { + var body = document.getElementsByTagName('body')[0] + SVG.on(body, 'event', action) + body.dispatchEvent(new CustomEvent('event')) + expect(toast).toBe('ready') + SVG.off(body, 'event', action) + }) it('attaches multiple handlers on different element', function() { var listenerCnt = SVG.listeners.length diff --git a/spec/spec/mask.js b/spec/spec/mask.js index ab367a0..a7cd6d1 100644 --- a/spec/spec/mask.js +++ b/spec/spec/mask.js @@ -31,6 +31,11 @@ describe('Mask', function() { expect(rect.masker.targets.indexOf(rect) > -1).toBe(true) }) + it('reuses mask element when mask was given', function() { + var mask = rect.masker + expect(draw.rect(100,100).maskWith(mask).masker).toBe(mask) + }) + it('unmasks all masked elements when being removed', function() { rect.masker.remove() expect(rect.attr('mask')).toBe(undefined) diff --git a/spec/spec/matrix.js b/spec/spec/matrix.js index 8b281ac..1c74ac2 100644 --- a/spec/spec/matrix.js +++ b/spec/spec/matrix.js @@ -113,6 +113,17 @@ describe('Matrix', function() { }) }) + + describe('clone()', function() { + it('returns a clone of the matrix', function() { + var matrix = new SVG.Matrix(2, 0, 0, 5, 0, 0) + , clone = matrix.clone() + expect(matrix).not.toBe(clone) + for(var i in 'abcdef') { + expect(matrix[i]).toEqual(clone[i]) + } + }) + }) describe('morph()', function() { it('stores a given matrix for morphing', function() { @@ -143,6 +154,10 @@ describe('Matrix', function() { expect(matrix2.toString()).toBe('matrix(1,0,0,1,4,3)') expect(matrix3.toString()).toBe('matrix(1.5,0,0,3,2,1.5)') }) + it('returns itself when no destination specified', function() { + var matrix = new SVG.Matrix(2, 0, 0, 5, 0, 0) + expect(matrix.at(0.5)).toBe(matrix) + }) }) describe('multiply()', function() { diff --git a/spec/spec/number.js b/spec/spec/number.js index e999187..58c14bd 100644 --- a/spec/spec/number.js +++ b/spec/spec/number.js @@ -237,6 +237,9 @@ describe('Number', function() { it('use the unit of this number as the unit of the returned number when the destination number as no unit', function() { expect(expect(new SVG.Number('100s').morph(50).at(0.5).unit).toBe('s')) }) + it('returns itself when no destination specified', function() { + expect(number.at(0.5)).toBe(number) + }) }) }) diff --git a/spec/spec/point.js b/spec/spec/point.js index 79745af..ae10ec8 100644 --- a/spec/spec/point.js +++ b/spec/spec/point.js @@ -75,7 +75,7 @@ describe('Point', function() { describe('clone()', function() { it('returns cloned point', function() { var point1 = new SVG.Point(1,1) - , point2 = new SVG.Point(point1) + , point2 = point1.clone() expect(point1).toEqual(point2) expect(point1).not.toBe(point2) @@ -117,6 +117,10 @@ describe('Point', function() { expect(point3).toEqual(new SVG.Point(1.5, 1.5)) }) + it('returns itself when no destination specified', function() { + var point = new SVG.Point(1,1) + expect(point.at(0.4)).toBe(point) + }) }) describe('transform()', function() { diff --git a/spec/spec/svg.js b/spec/spec/svg.js index 63df0b9..311a69a 100644 --- a/spec/spec/svg.js +++ b/spec/spec/svg.js @@ -83,4 +83,27 @@ describe('SVG', function() { }) }) + describe('prepare()', function() { + var drawing, wrapper, parser + + beforeEach(function() { + wrapper = document.createElement('div') + document.getElementsByTagName('body')[0].appendChild(wrapper) + drawing = SVG(wrapper) + + parser = document.getElementsByTagName('body')[0].lastChild + }) + + it('creates a parser element when calling SVG()', function() { + expect(SVG.parser.draw.node.nodeName).toBe('svg') + }) + it('hides the parser', function() { + expect(SVG.parser.draw.node.getAttribute('style')).toBe('opacity: 0; position: fixed; left: 100%; top: 100%; overflow: hidden;') + }) + it('holds polyline and path', function() { + expect(SVG.select('polyline', SVG.parser.draw.node).first().type).toBe('polyline') + expect(SVG.select('path', SVG.parser.draw.node).first().type).toBe('path') + }) + }) + }) \ No newline at end of file diff --git a/spec/spec/viewbox.js b/spec/spec/viewbox.js index 8ee8fd8..2175778 100644 --- a/spec/spec/viewbox.js +++ b/spec/spec/viewbox.js @@ -145,6 +145,10 @@ describe('Viewbox', function() { expect(viewbox2.toString()).toBe('50 -100 300 300') expect(viewbox3.toString()).toBe('30 0 250 300') }) + it('returns itself when no destination given', function() { + var viewbox = new SVG.ViewBox(10, 100, 200, 300) + expect(viewbox.at(0.5)).toBe(viewbox) + }) }) }) \ No newline at end of file