diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2020-04-09 11:01:56 +1000 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2020-04-09 11:01:56 +1000 |
commit | 6a57b8cd9da1d0abc77b6ba4a2ce3d15fe675048 (patch) | |
tree | 106388badb426ae6b5f079de80440fe69e7dcbef /spec | |
parent | 36b65881bd090e105d19952d10b3681313daf2af (diff) | |
download | svg.js-6a57b8cd9da1d0abc77b6ba4a2ce3d15fe675048.tar.gz svg.js-6a57b8cd9da1d0abc77b6ba4a2ce3d15fe675048.zip |
fixes and tests
- fixed flatten and ungroup
- added position argument to ungroup, toParent and toRoot
- added tests for Container
Diffstat (limited to 'spec')
-rw-r--r-- | spec/SpecRunnerEs6.html | 21 | ||||
-rw-r--r-- | spec/spec/element.js | 2 | ||||
-rw-r--r-- | spec/spec/elements/Container.js | 131 | ||||
-rw-r--r-- | spec/spec/elements/Dom.js | 33 |
4 files changed, 168 insertions, 19 deletions
diff --git a/spec/SpecRunnerEs6.html b/spec/SpecRunnerEs6.html index e5629e9..0f5ff75 100644 --- a/spec/SpecRunnerEs6.html +++ b/spec/SpecRunnerEs6.html @@ -19,23 +19,26 @@ <body> <!-- include spec files here... --> - <script type="module" src="setupBrowser.js"></script> - <script type="module" src="spec/types/ArrayPolyfill.js"></script> - <script type="module" src="spec/types/Base.js"></script> - <script type="module" src="spec/types/Box.js"></script> - <script type="module" src="spec/utils/adopter.js"></script> - <script type="module" src="spec/utils/utils.js"></script> <script type="module" src="spec/elements/A.js"></script> + <script type="module" src="spec/elements/Circle.js"></script> + <script type="module" src="spec/elements/ClipPath.js"></script> + <script type="module" src="spec/elements/Container.js"></script> <script type="module" src="spec/elements/Dom.js"></script> + <script type="module" src="spec/elements/Element.js"></script> <script type="module" src="spec/elements/ForeignObject.js"></script> <script type="module" src="spec/elements/G.js"></script> <script type="module" src="spec/elements/Marker.js"></script> - <script type="module" src="spec/elements/Tspan.js"></script> + <script type="module" src="spec/elements/Svg.js"></script> <script type="module" src="spec/elements/Text.js"></script> <script type="module" src="spec/elememts/TextPath.js"></script> - <script type="module" src="spec/modules/core/textable.js"></script> + <script type="module" src="spec/elements/Tspan.js"></script> <script type="module" src="spec/modules/core/event.js"></script> - + <script type="module" src="spec/modules/core/textable.js"></script> + <script type="module" src="spec/types/ArrayPolyfill.js"></script> + <script type="module" src="spec/types/Base.js"></script> + <script type="module" src="spec/types/Box.js"></script> + <script type="module" src="spec/utils/adopter.js"></script> + <script type="module" src="spec/utils/utils.js"></script> </body> </html> diff --git a/spec/spec/element.js b/spec/spec/element.js index b1686c0..87f4481 100644 --- a/spec/spec/element.js +++ b/spec/spec/element.js @@ -457,7 +457,7 @@ describe('Element', function () { it('redirects to toParent(root)', function () { rect.toRoot() - expect(rect.toParent).toHaveBeenCalledWith(rect.root()) + expect(rect.toParent).toHaveBeenCalledWith(rect.root(), undefined) }) }) diff --git a/spec/spec/elements/Container.js b/spec/spec/elements/Container.js new file mode 100644 index 0000000..5d53eb1 --- /dev/null +++ b/spec/spec/elements/Container.js @@ -0,0 +1,131 @@ +/* globals describe, expect, it, beforeEach, jasmine, container */ + +import { Container, create, SVG } from '../../../src/main.js' + +const { any } = jasmine + +describe('Container.js', () => { + describe('()', () => { + it('creates a new object of type Container', () => { + expect(new Container(create('g'))).toEqual(any(Container)) + }) + }) + + let canvas + let rect1 + let group1 + let rect2 + let circle1 + let group2 + let circle2 + let group3 + let line1 + let line2 + let circle3 + let group4 + let rect3 + + beforeEach(() => { + canvas = SVG().addTo(container) + rect1 = canvas.rect(100, 100).id('rect1') + group1 = canvas.group().id('group1') + rect2 = group1.rect(100, 100).id('rect2') + circle1 = group1.circle(50).id('circle1') + group2 = group1.group().id('group2') + circle2 = group2.circle(50).id('circle2') + group3 = group2.group().id('group3') + line1 = group3.line(1, 1, 2, 2).id('line1') + line2 = group3.line(1, 1, 2, 2).id('line2') + circle3 = group2.circle(50).id('circle3') + group4 = canvas.group().id('group4') + rect3 = group4.rect(100, 100).id('rect3') + + /* should be: + canvas + rect1 + group1 + rect2 + circle1 + group2 + circle2 + group3 + line1 + line2 + circle3 + group4 + rect3 + */ + }) + + describe('flatten()', () => { + it('flattens the whole document when called on the root', () => { + canvas.flatten() + + expect(canvas.children()).toEqual([ rect1, rect2, circle1, circle2, line1, line2, circle3, rect3 ]) + }) + + it('flattens a group and places all children into its parent when called on a group - 1', () => { + group1.flatten() + + /* now should be: + canvas + rect1 + group1 + rect2 + circle1 + circle2 + line1 + line2 + circle3 + group4 + rect3 + */ + + expect(canvas.children()).toEqual([ rect1, group1, group4 ]) + expect(group1.children()).toEqual([ rect2, circle1, circle2, line1, line2, circle3 ]) + }) + + it('flattens a group and places all children into its parent when called on a group - 2', () => { + group2.flatten() + + /* now should be: + canvas + rect1 + group1 + rect2 + circle1 + group2 + circle2 + line1 + line2 + circle3 + group4 + rect3 + */ + + expect(group2.children()).toEqual([ circle2, line1, line2, circle3 ]) + }) + }) + + describe('ungroup()', () => { + it('ungroups a group and inserts all children in the correct order in the parent parent of the group', () => { + group1.ungroup() + + expect(canvas.children()).toEqual([ rect1, rect2, circle1, group2, group4 ]) + + group4.ungroup() + + expect(canvas.children()).toEqual([ rect1, rect2, circle1, group2, rect3 ]) + }) + + it('ungroups a group into another group and appends the elements to the other group', () => { + group1.ungroup(group4) + expect(group4.children()).toEqual([ rect3, rect2, circle1, group2 ]) + }) + + it('ungroups a group into another group at the specified position', () => { + group2.ungroup(group1, 1) + expect(group1.children()).toEqual([ rect2, circle2, group3, circle3, circle1 ]) + }) + }) +}) diff --git a/spec/spec/elements/Dom.js b/spec/spec/elements/Dom.js index 6689822..3227ba1 100644 --- a/spec/spec/elements/Dom.js +++ b/spec/spec/elements/Dom.js @@ -1,6 +1,6 @@ /* globals describe, expect, it, beforeEach, spyOn, jasmine, container */ -import { SVG, G, Rect, Svg, Dom, List, Fragment, Circle, Tspan } from '../../../src/main.js' +import { SVG, G, Rect, Svg, Dom, List, Fragment, Circle, Tspan, create } from '../../../src/main.js' import { getWindow } from '../../../src/utils/window.js' const { any, createSpy, objectContaining } = jasmine @@ -66,9 +66,7 @@ describe('Dom.js', function () { it('handles a node', () => { const g = new G() - const rect = new Rect() - const node = rect.node - delete rect.instance + const node = create('rect') g.add(node) expect(g.children().length).toBe(1) expect(g.get(0)).toEqual(any(Rect)) @@ -219,6 +217,10 @@ describe('Dom.js', function () { g.circle(100, 100) expect(g.first()).toBe(rect) }) + + it('returns null if no first child exists', () => { + expect(new G().first()).toBe(null) + }) }) describe('get()', () => { @@ -289,6 +291,12 @@ describe('Dom.js', function () { const rect = g.rect(100, 100) expect(g.index(rect)).toBe(1) }) + + it('returns -1 if element is no child', () => { + const g = new G() + const rect = new Rect() + expect(g.index(rect)).toBe(-1) + }) }) describe('last()', () => { @@ -298,6 +306,10 @@ describe('Dom.js', function () { const rect = g.rect(100, 100) expect(g.last()).toBe(rect) }) + + it('returns null if no last child exists', () => { + expect(new G().last()).toBe(null) + }) }) describe('parent()', () => { @@ -528,7 +540,13 @@ describe('Dom.js', function () { it('returns the parent when outerHtml = true', () => { const canvas = new Svg() const g = canvas.group() - expect(g.svg('<rect><circle>', true)).toBe(canvas) + expect(g.svg('<rect /><circle />', true)).toBe(canvas) + expect(canvas.children()).toEqual([ any(Rect), any(Circle) ]) + }) + + it('works without a parent', () => { + const canvas = new Svg() + expect(canvas.svg('<rect><circle>')).toBe(canvas) }) }) @@ -655,13 +673,10 @@ describe('Dom.js', function () { }) it('allows to pass an svg node as element', () => { - var g = new G() - const node = g.node - delete node.instance + const node = create('g') rect.wrap(node) expect(rect.parent()).toEqual(any(G)) expect(rect.parent().node).toBe(node) - expect(rect.parent()).not.toBe(g) expect(rect.parent().parent()).toBe(canvas) }) }) |