forked from svgdotjs/svg.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.js
More file actions
executable file
·208 lines (164 loc) · 5.21 KB
/
Copy pathset.js
File metadata and controls
executable file
·208 lines (164 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
describe('Set', function() {
var set, e1, e2, e3, e4, e5
beforeEach(function() {
draw.attr('viewBox', null)
set = draw.set()
e1 = draw.rect(100,100).attr('id', 'e1').move(200,250)
e2 = draw.ellipse(100,100).attr('id', 'e2')
e3 = draw.line(0,0,100,100).attr('id', 'e3')
e4 = draw.circle(50).attr('id', 'e4')
e5 = draw.polyline('0,0 10,20 30,50 80,100').attr('id', 'e5')
})
afterEach(function() {
draw.clear()
})
it('creates the set method on SVG.Container instances', function() {
expect(draw.set() instanceof SVG.Set).toBeTruthy()
})
it('creates a set with initial value', function() {
var members = [1, 2, 4]
expect(draw.set(members).valueOf()).toBe(members)
})
describe('add()', function() {
it('returns the set instance', function() {
expect(set.add(e1)).toBe(set)
})
it('stores given element', function() {
set.add(e1).add(e2).add(e3)
expect(set.valueOf()).toEqual([e1,e2,e3])
expect(set.members.length).toBe(3)
})
it('accepts multiple elements at once', function() {
set.add(e1, e2, e3, e4, e5)
expect(set.valueOf()).toEqual([e1, e2, e3, e4, e5])
expect(set.members.length).toBe(5)
})
})
describe('remove()', function() {
it('returns the set instance', function() {
set.add(e1)
expect(set.remove(e1)).toBe(set)
})
it('removes given element', function() {
set.add(e1).add(e2).add(e3).remove(e2)
expect(set.valueOf()).toEqual([e1,e3])
expect(set.members.length).toBe(2)
})
})
describe('each()', function() {
it('returns the set instance', function() {
expect(set.each(function(){})).toBe(set)
})
it('iterates over all members of the set', function() {
var ids = []
set.add(e1).add(e2).add(e3)
set.each(function() {
ids.push(this.attr('id'))
})
expect(ids.length).toBe(3)
expect(ids).toEqual(['e1','e2','e3'])
})
})
describe('clear()', function() {
it('returns the set instance', function() {
expect(set.clear()).toBe(set)
})
it('removes all members from set', function() {
set.add(e1).add(e2).add(e3).add(e4).add(e5).clear()
expect(set.members.length).toBe(0)
})
})
describe('get()', function() {
it('returns member at given index', function() {
set.add(e1).add(e2).add(e3).add(e4).add(e5)
expect(set.get(2)).toBe(e3)
})
})
describe('first()', function() {
it('returns first member', function() {
set.add(e1).add(e2).add(e3).add(e4).add(e5)
expect(set.first()).toBe(e1)
})
})
describe('last()', function() {
it('returns last member', function() {
set.add(e1).add(e2).add(e3).add(e4).add(e5)
expect(set.last()).toBe(e5)
})
})
describe('has()', function() {
it('checks if a given element is present in set', function() {
set.add(e1).add(e2).add(e3).add(e4).add(e5)
expect(set.has(e4)).toBeTruthy()
})
})
describe('index()', function() {
it('returns the index of a given element within the set', function() {
set.add(e1).add(e2).add(e3).add(e5)
expect(set.index(e1)).toBe(0)
expect(set.index(e2)).toBe(1)
expect(set.index(e3)).toBe(2)
expect(set.index(e4)).toBe(-1)
expect(set.index(e5)).toBe(3)
})
})
describe('valueOf()', function() {
it('returns the members array', function() {
set.add(e1)
expect(set.valueOf()).toBe(set.members)
})
})
describe('bbox()', function() {
it('returns the bounding box of all elements', function() {
set.add(e1).add(e2).add(e3).add(e4).add(e5)
var box = set.bbox()
expect(box.x).toBe(0)
expect(box.y).toBe(0)
expect(box.width).toBe(300)
expect(box.height).toBe(350)
})
it('returns an instance of SVG.BBox', function() {
set.add(e1).add(e2).add(e3).add(e4).add(e5)
expect(set.bbox() instanceof SVG.BBox).toBeTruthy()
})
it('returns an empty bounding box wiht no members', function() {
var box = set.bbox()
expect(box.x).toBe(0)
expect(box.y).toBe(0)
expect(box.width).toBe(0)
expect(box.height).toBe(0)
})
})
describe('method alias', function() {
describe('attr()', function() {
it('is applied to every member of the set', function() {
var fills = []
set.add(e1).add(e2).add(e3).add(e4).add(e5).attr('fill', '#ff0099')
set.each(function() {
fills.push(this.attr('fill'))
})
expect(fills).toEqual(['#ff0099','#ff0099','#ff0099','#ff0099','#ff0099'])
})
})
})
describe('method inheritance', function() {
beforeEach(function() {
SVG.extend(SVG.Element, {
orange: function() {
this.fill('#ff6600')
}
})
})
it('inherits newly added element methods after initialisation', function() {
expect(typeof set.orange).toBe('function')
})
it('applies newly inherited methods properly to members', function() {
var fills = []
set.add(e1).add(e2).add(e3).add(e4).add(e5).orange()
set.each(function() {
fills.push(this.attr('fill'))
})
expect(fills).toEqual(['#ff6600','#ff6600','#ff6600','#ff6600','#ff6600'])
})
})
})