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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
describe('Element', function() {
beforeEach(function() {
draw.clear()
draw.attr('viewBox', null)
})
it('should create a circular reference on the node', function() {
var rect = draw.rect(100,100)
expect(rect.node.instance).toBe(rect)
})
describe('native()', function() {
it('returns the node reference', function() {
var rect = draw.rect(100,100)
expect(rect.native()).toBe(rect.node)
})
})
describe('attr()', function() {
var rect
beforeEach(function() {
rect = draw.rect(100,100)
})
afterEach(function() {
rect.remove()
})
it('sets one attribute when two arguments are given', function() {
rect.attr('fill', '#ff0066')
expect(rect.node.getAttribute('fill')).toBe('#ff0066')
})
it('sets various attributes when an object is given', function() {
rect.attr({ fill: '#00ff66', stroke: '#ff2233', 'stroke-width': 10 })
expect(rect.node.getAttribute('fill')).toBe('#00ff66')
expect(rect.node.getAttribute('stroke')).toBe('#ff2233')
expect(rect.node.getAttribute('stroke-width')).toBe('10')
})
it('gets the value of the string value given as first argument', function() {
rect.attr('fill', '#ff0066')
expect(rect.attr('fill')).toEqual('#ff0066')
})
it('gets an object with all attributes without any arguments', function() {
rect.attr({ fill: '#00ff66', stroke: '#ff2233' })
var attr = rect.attr()
expect(attr.fill).toBe('#00ff66')
expect(attr.stroke).toBe('#ff2233')
})
it('removes an attribute if the second argument is explicitly set to null', function() {
rect.attr('stroke-width', 10)
expect(rect.node.getAttribute('stroke-width')).toBe('10')
rect.attr('stroke-width', null)
expect(rect.node.getAttribute('stroke-width')).toBe(null)
})
it('correctly parses numeric values as a getter', function() {
rect.attr('stroke-width', 11)
expect(rect.node.getAttribute('stroke-width')).toBe('11')
expect(rect.attr('stroke-width')).toBe(11)
})
it('correctly parses negative numeric values as a getter', function() {
rect.attr('x', -120)
expect(rect.node.getAttribute('x')).toBe('-120')
expect(rect.attr('x')).toBe(-120)
})
it('falls back on default values if attribute is not present', function() {
expect(rect.attr('stroke-linejoin')).toBe('miter')
})
it('gets the "style" attribute as a string', function() {
rect.style('cursor', 'pointer')
expect(rect.node.style.cursor).toBe('pointer')
})
it('redirects to the style() method when setting a style string', function() {
rect.attr('style', 'cursor:move;')
expect(rect.node.style.cursor).toBe('move')
})
it('removes style attribute on node if the style is empty', function() {
rect.style('cursor', 'move')
rect.style('cursor', '')
expect(rect.style.cursor).toBe(undefined)
})
it('acts as a global getter when no arguments are given', function() {
rect.fill('#ff0066')
expect(rect.attr().fill).toBe('#ff0066')
})
it('correctly parses numeric values as a global getter', function() {
rect.stroke({ width: 20 })
expect(rect.attr()['stroke-width']).toBe(20)
})
it('correctly parses negative numeric values as a global getter', function() {
rect.x(-30)
expect(rect.attr().x).toBe(-30)
})
it('leaves unit values alone as a global getter', function() {
rect.attr('x', '69%')
expect(rect.attr().x).toBe('69%')
})
})
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'))
})
it('sets the value of the id', function() {
rect.id('new_id')
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')
expect(stripped(rect.node.style.cssText)).toBe('cursor:crosshair;')
})
it('sets multiple styles with an object as the first argument', function() {
var rect = draw.rect(100,100).style({ cursor: 'help', display: 'block' })
expect(stripped(rect.node.style.cssText)).toMatch(/cursor:help;/)
expect(stripped(rect.node.style.cssText)).toMatch(/display:block;/)
expect(stripped(rect.node.style.cssText).length).toBe(('display:block;cursor:help;').length)
})
it('gets a style with a string key as the fists argument', function() {
var rect = draw.rect(100,100).style({ cursor: 'progress', display: 'block' })
expect(rect.style('cursor')).toBe('progress')
})
it('gets a style with a string key as the fists argument', function() {
var rect = draw.rect(100,100).style({ cursor: 's-resize', display: 'none' })
expect(stripped(rect.style())).toMatch(/display:none;/)
expect(stripped(rect.style())).toMatch(/cursor:s-resize;/)
expect(stripped(rect.style()).length).toBe(('cursor:s-resize;display:none;').length)
})
it('removes a style if the value is an empty string', function() {
var rect = draw.rect(100,100).style({ cursor: 'n-resize', display: '' })
expect(stripped(rect.style())).toBe('cursor:n-resize;')
})
it('removes a style if the value explicitly set to null', function() {
var rect = draw.rect(100,100).style('cursor', 'w-resize')
expect(stripped(rect.style())).toBe('cursor:w-resize;')
rect.style('cursor', null)
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())
})
it('sets the translation of and element', function() {
rect.transform({ x: 10, y: 11 })
expect(rect.node.getAttribute('transform')).toBe('matrix(1,0,0,1,10,11)')
})
it('performs an absolute translation', function() {
rect.transform({ x: 10, y: 11 }).transform({ x: 20, y: 21 })
expect(rect.node.getAttribute('transform')).toBe('matrix(1,0,0,1,20,21)')
})
it('performs a relative translation when relative is set to true', function() {
rect.transform({ x: 10, y: 11 }).transform({ x: 20, y: 21, relative: true })
expect(rect.node.getAttribute('transform')).toBe('matrix(1,0,0,1,30,32)')
})
it('performs a relative translation with relative flag', function() {
rect.transform({ x: 10, y: 11 }).transform({ x: 20, y: 21 }, true)
expect(rect.node.getAttribute('transform')).toBe('matrix(1,0,0,1,30,32)')
})
it('sets the scaleX and scaleY of an element', function() {
rect.transform({ scaleX: 0.5, scaleY: 2 })
expect(rect.node.getAttribute('transform')).toBe('matrix(0.5,0,0,2,25,-50)')
})
it('performs a uniform scale with scale given', function() {
rect.transform({ scale: 3 })
expect(rect.node.getAttribute('transform')).toBe('matrix(3,0,0,3,-100,-100)')
})
it('performs an absolute scale by default', function() {
rect.transform({ scale: 3 }).transform({ scale: 0.5 })
expect(rect.node.getAttribute('transform')).toBe('matrix(0.5,0,0,0.5,25,25)')
})
it('performs a relative scale with a relative flag', function() {
rect.transform({ scaleX: 0.5, scaleY: 2 }).transform({ scaleX: 3, scaleY: 4 }, true)
expect(rect.node.getAttribute('transform')).toBe('matrix(1.5,0,0,8,-25,-350)')
})
it('sets the skewX of an element with center on the element', function() {
ctm = rect.transform({ skewX: 10 }).ctm()
expect(ctm.a).toBe(1)
expect(ctm.b).toBe(0)
expect(ctm.c).toBeCloseTo(0.17632698070846498)
expect(ctm.d).toBe(1)
expect(ctm.e).toBeCloseTo(-8.81634903542325)
expect(ctm.f).toBe(0)
})
it('sets the skewX of an element with given center', function() {
ctm = rect.transform({ skewX: 10, cx: 0, cy: 0 }).ctm()
expect(ctm.a).toBe(1)
expect(ctm.b).toBe(0)
expect(ctm.c).toBeCloseTo(0.17632698070846498)
expect(ctm.d).toBe(1)
expect(ctm.e).toBe(0)
expect(ctm.f).toBe(0)
})
it('sets the skewY of an element', function() {
ctm = rect.transform({ skewY: -10, cx: 0, cy: 0 }).ctm()
expect(ctm.a).toBe(1)
expect(ctm.b).toBeCloseTo(-0.17632698070846498)
expect(ctm.c).toBe(0)
expect(ctm.d).toBe(1)
expect(ctm.e).toBe(0)
expect(ctm.f).toBe(0)
})
it('sets the skewX and skewY of an element', function() {
ctm = rect.transform({ skewX: 10, skewY: -10, cx: 0, cy: 0 }).ctm()
expect(ctm.a).toBe(1)
expect(ctm.b).toBeCloseTo(-0.17632698070846498)
expect(ctm.c).toBeCloseTo(0.17632698070846498)
expect(ctm.d).toBe(1)
expect(ctm.e).toBe(0)
expect(ctm.f).toBe(0)
})
it('performs a uniform skew with skew given', function() {
ctm = rect.transform({ skew: 5, cx: 0, cy: 0 }).ctm()
expect(ctm.a).toBe(1)
expect(ctm.b).toBeCloseTo(0.08748866352592401)
expect(ctm.c).toBeCloseTo(0.08748866352592401)
expect(ctm.d).toBe(1)
expect(ctm.e).toBe(0)
expect(ctm.f).toBe(0)
})
it('rotates the element around its centre if no rotation point is given', function() {
ctm = rect.center(100, 100).transform({ rotation: 45 }).ctm()
expect(ctm.a).toBeCloseTo(0.7071068286895752)
expect(ctm.b).toBeCloseTo(0.7071068286895752)
expect(ctm.c).toBeCloseTo(-0.7071068286895752)
expect(ctm.d).toBeCloseTo(0.7071068286895752)
expect(ctm.e).toBeCloseTo(100)
expect(ctm.f).toBeCloseTo(-41.421356201171875)
expect(rect.transform('rotation')).toBe(45)
})
it('rotates the element around the given rotation point', function() {
ctm = rect.transform({ rotation: 55, cx: 80, cy:2 }).ctm()
expect(ctm.a).toBeCloseTo(0.5735765099525452)
expect(ctm.b).toBeCloseTo(0.8191521167755127)
expect(ctm.c).toBeCloseTo(-0.8191521167755127)
expect(ctm.d).toBeCloseTo(0.5735765099525452)
expect(ctm.e).toBeCloseTo(35.75218963623047)
expect(ctm.f).toBeCloseTo(-64.67931365966797)
})
it('transforms element using a matrix', function() {
rect.transform({ a: 0.5, c: 0.5 })
expect(rect.node.getAttribute('transform')).toBe('matrix(0.5,0,0.5,1,0,0)')
})
})
describe('untransform()', function() {
var circle
beforeEach(function() {
circle = draw.circle(100).translate(50, 100)
})
it('removes the transform attribute', function() {
expect(circle.node.getAttribute('transform')).toBe('matrix(1,0,0,1,50,100)')
circle.untransform()
expect(circle.node.getAttribute('transform')).toBeNull()
})
it('resets the current transform matix', function() {
expect(circle.ctm()).toEqual(new SVG.Matrix(1,0,0,1,50,100))
circle.untransform()
expect(circle.ctm()).toEqual(new SVG.Matrix)
})
})
describe('matrixify', function() {
var rect
beforeEach(function() {
rect = draw.rect(100, 100)
})
it('allow individual transform definitions to be separated by whitespace', function(){
// One space
rect.attr('transform', 'translate(20) translate(20)')
expect(rect.matrixify().toString()).toBe('matrix(1,0,0,1,40,0)')
// More than one space
rect.attr('transform', 'translate(20) translate(-60)')
expect(rect.matrixify().toString()).toBe('matrix(1,0,0,1,-40,0)')
})
it('allow individual transform definitions to be separated by a comma', function(){
rect.attr('transform', 'translate(20,16),translate(20)')
expect(rect.matrixify().toString()).toBe('matrix(1,0,0,1,40,16)')
})
it('allow individual transform definitions to be separated by whitespace and a comma', function(){
// Spaces before the comma
rect.attr('transform', 'translate(20,16) ,translate(20)')
expect(rect.matrixify().toString()).toBe('matrix(1,0,0,1,40,16)')
// Spaces after the comma
rect.attr('transform', 'translate(12), translate(10,14)')
expect(rect.matrixify().toString()).toBe('matrix(1,0,0,1,22,14)')
// Spaces before and after the comma
rect.attr('transform', 'translate(24,14) , translate(36,6)')
expect(rect.matrixify().toString()).toBe('matrix(1,0,0,1,60,20)')
})
})
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)')
})
it('returns an instance of SVG.Matrix', 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')
expect(rect.node.getAttribute('data-test')).toBe('value')
})
it('sets a data attribute and not convert value to json if flagged raw', function() {
var rect = draw.rect(100,100).data('test', 'value', true)
expect(rect.node.getAttribute('data-test')).toBe('value')
})
it('sets multiple data attributes and convert values to json when an object is passed', function() {
var rect = draw.rect(100,100).data({
forbidden: 'fruit'
, multiple: {
values: 'in'
, an: 'object'
}
})
expect(rect.node.getAttribute('data-forbidden')).toBe('fruit')
expect(rect.node.getAttribute('data-multiple')).toEqual('{"values":"in","an":"object"}')
})
it('gets data value if only one argument is passed', function() {
var rect = draw.rect(100,100).data('test', 101)
expect(rect.data('test')).toBe(101)
})
it('maintains data type for a number', function() {
var rect = draw.rect(100,100).data('test', 101)
expect(typeof rect.data('test')).toBe('number')
})
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)
})
})
describe('remove()', function() {
it('removes an element and return it', function() {
var rect = draw.rect(100,100)
expect(rect.remove()).toBe(rect)
})
it('removes an element from its parent', function() {
var rect = draw.rect(100,100)
rect.remove()
expect(draw.has(rect)).toBe(false)
})
})
describe('addTo()', function() {
it('adds an element to a given parent and returns itself', function() {
var rect = draw.rect(100,100)
, group = draw.group()
expect(rect.addTo(group)).toBe(rect)
expect(rect.parent()).toBe(group)
})
})
describe('putIn()', function() {
it('adds an element to a given parent and returns parent', function() {
var rect = draw.rect(100,100)
, group = draw.group()
expect(rect.putIn(group)).toBe(group)
expect(rect.parent()).toBe(group)
})
})
describe('rbox()', function() {
it('returns an instance of SVG.RBox', function() {
var rect = draw.rect(100,100)
expect(rect.rbox() instanceof SVG.RBox).toBe(true)
})
it('returns the correct rectangular box', function() {
var rect = draw.size(200, 150).viewbox(0, 0, 200, 150).rect(105, 210).move(2, 12)
var box = rect.rbox()
expect(box.x).toBeCloseTo(2, 0)
expect(box.y).toBeCloseTo(12, 0)
expect(box.cx).toBeCloseTo(54.5)
expect(box.cy).toBeCloseTo(117, 0)
expect(box.width).toBe(105)
expect(box.height).toBe(210)
})
it('returns the correct rectangular box within a viewbox', function() {
var rect = draw.size(200,150).viewbox(0,0,100,75).rect(105,210).move(2,12)
var box = rect.rbox()
expect(box.x).toBeCloseTo(4)
expect(box.y).toBeCloseTo(24, 0)
expect(box.cx).toBeCloseTo(56.5)
expect(box.cy).toBeCloseTo(129, 0)
expect(box.width).toBe(105)
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)
expect(rect.parent()).toBe(draw)
})
it('contains the parent group when in a group', function() {
var group = draw.group()
, rect = group.rect(100,100)
expect(rect.parent()).toBe(group)
})
it('contains the parent which matches type', function() {
var group = draw.group()
, rect = group.rect(100,100)
expect(rect.parent(SVG.Doc)).toBe(draw)
})
it('contains the parent which matches selector', function() {
var group1 = draw.group().addClass('test')
, group2 = group1.group()
, rect = group2.rect(100,100)
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')
, group2 = group1.group()
, rect = group2.rect(100,100)
expect(rect.parents('.test')[0]).toBe(group1)
expect(rect.parents(SVG.G)[0]).toBe(group2)
expect(rect.parents(SVG.G)[1]).toBe(group1)
expect(rect.parents().length).toBe(3)
})
})
describe('clone()', function() {
var rect, group, circle
beforeEach(function() {
rect = draw.rect(100,100).center(321,567).fill('#f06')
group = draw.group().add(rect)
circle = group.circle(100)
})
it('makes an exact copy of the element', function() {
clone = rect.clone()
expect(clone.attr('id', null).attr()).toEqual(rect.attr('id', null).attr())
})
it('assigns a new id to the cloned element', function() {
clone = rect.clone()
expect(clone.attr('id')).not.toBe(rect.attr('id'))
})
it('copies all child nodes as well', function() {
clone = group.clone()
expect(clone.children().length).toBe(group.children().length)
})
it('assigns a new id to cloned child elements', function() {
clone = group.clone()
expect(clone.attr('id')).not.toEqual(group.attr('id'))
expect(clone.get(0).attr('id')).not.toBe(group.get(0).attr('id'))
expect(clone.get(1).attr('id')).not.toBe(group.get(1).attr('id'))
})
it('inserts the clone after the cloned element', function() {
clone = rect.clone()
expect(rect.next()).toBe(clone)
})
it('inserts the clone in the specided parent', function() {
var g = draw.group()
clone = rect.clone(g)
expect(g.get(0)).toBe(clone)
})
})
describe('toString()', function() {
it('returns the element id', function() {
var rect = draw.rect(100,100).center(321,567).fill('#f06')
expect(rect + '').toBe(rect.attr('id'))
})
})
describe('replace()', function() {
it('replaces the original element by another given element', function() {
var rect = draw.rect(100,100).center(321,567).fill('#f06')
var circle = draw.circle(200)
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)
})
})
describe('classes()', function() {
it('returns an array of classes on the node', function() {
var element = draw.rect(100,100)
element.node.setAttribute('class', 'one two')
expect(element.classes()).toEqual(['one', 'two'])
})
})
describe('hasClass()', function() {
it('returns true if the node has the class', function() {
var element = draw.rect(100,100)
element.node.setAttribute('class', 'one')
expect(element.hasClass('one')).toBeTruthy()
})
it('returns false if the node does not have the class', function() {
var element = draw.rect(100,100)
element.node.setAttribute('class', 'one')
expect(element.hasClass('two')).toBeFalsy()
})
})
describe('addClass()', function() {
it('adds the class to the node', function() {
var element = draw.rect(100,100)
element.addClass('one')
expect(element.hasClass('one')).toBeTruthy()
})
it('does not add duplicate classes', function() {
var element = draw.rect(100,100)
element.addClass('one')
element.addClass('one')
expect(element.node.getAttribute('class')).toEqual('one')
})
it('returns the svg instance', function() {
var element = draw.rect(100,100)
expect(element.addClass('one')).toEqual(element)
})
})
describe('removeClass()', function() {
it('removes the class from the node when the class exists', function() {
var element = draw.rect(100,100)
element.addClass('one')
element.removeClass('one')
expect(element.hasClass('one')).toBeFalsy()
})
it('does nothing when the class does not exist', function() {
var element = draw.rect(100,100)
element.removeClass('one')
expect(element.hasClass('one')).toBeFalsy()
})
it('returns the element', function() {
var element = draw.rect(100,100)
expect(element.removeClass('one')).toEqual(element)
})
})
describe('toggleClass()', function() {
it('adds the class when it does not already exist', function(){
var element = draw.rect(100,100)
element.toggleClass('one')
expect(element.hasClass('one')).toBeTruthy()
})
it('removes the class when it already exists', function(){
var element = draw.rect(100,100)
element.addClass('one')
element.toggleClass('one')
expect(element.hasClass('one')).toBeFalsy()
})
it('returns the svg instance', function() {
var element = draw.rect(100,100)
expect(element.toggleClass('one')).toEqual(element)
})
})
describe('reference()', function() {
it('gets a referenced element from a given attribute', function() {
var rect = draw.defs().rect(100, 100)
, use = draw.use(rect)
, mark = draw.marker(10, 10)
, path = draw.path(svgPath).marker('end', mark)
expect(use.reference('href')).toBe(rect)
expect(path.reference('marker-end')).toBe(mark)
})
})
describe('svg()', function() {
describe('without an argument', function() {
it('returns full raw svg when called on the main svg doc', function() {
draw.size(100,100).rect(100,100).id(null)
draw.circle(100).fill('#f06').id(null)
var toBeTested = draw.svg()
// Test for different browsers namely Firefox and Chrome
expect(
toBeTested === '<svg xmlns:svgjs="http://svgjs.com/svgjs" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" xmlns="http://www.w3.org/2000/svg" height="100" width="100" id="' + draw.id() + '"><rect height="100" width="100"></rect><circle fill="#ff0066" cy="50" cx="50" r="50"></circle></svg>'
|| toBeTested === '<svg id="' + draw.id() + '" width="100" height="100" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs"><rect width="100" height="100"></rect><circle r="50" cx="50" cy="50" fill="#ff0066"></circle></svg>').toBeTruthy()
})
it('returns partial raw svg when called on a sub group', function() {
var group = draw.group().id(null)
group.rect(100,100).id(null)
group.circle(100).fill('#f06').id(null)
var toBeTested = group.svg()
expect(
toBeTested === '<g><rect width="100" height="100"></rect><circle r="50" cx="50" cy="50" fill="#ff0066"></circle></g>'
|| toBeTested === '<g><rect height="100" width="100"></rect><circle fill="#ff0066" cy="50" cx="50" r="50"></circle></g>').toBeTruthy()
})
it('returns a single element when called on an element', function() {
var group = draw.group().id(null)
group.rect(100,100).id(null)
var circle = group.circle(100).fill('#f06').id(null)
var toBeTested = circle.svg()
expect(
toBeTested === '<circle r="50" cx="50" cy="50" fill="#ff0066"></circle>'
|| toBeTested === '<circle fill="#ff0066" cy="50" cx="50" r="50"></circle>').toBeTruthy()
})
})
describe('with raw svg given', function() {
it('imports a full svg document', function() {
draw.svg('<svg id="SvgjsSvg1000" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100" viewBox="0 0 50 50"><rect id="SvgjsRect1183" width="100" height="100"></rect><circle id="SvgjsCircle1184" r="50" cx="25" cy="25" fill="#ff0066"></circle></svg>')
expect(draw.get(0).type).toBe('svg')
expect(draw.get(0).children().length).toBe(2)
expect(draw.get(0).get(0).type).toBe('rect')
expect(draw.get(0).get(1).type).toBe('circle')
expect(draw.get(0).get(1).attr('fill')).toBe('#ff0066')
})
it('imports partial svg content', function() {
draw.svg('<g id="SvgjsG1185"><rect id="SvgjsRect1186" width="100" height="100"></rect><circle id="SvgjsCircle1187" r="50" cx="25" cy="25" fill="#ff0066"></circle></g>')
expect(draw.get(0).type).toBe('g')
expect(draw.get(0).get(0).type).toBe('rect')
expect(draw.get(0).get(1).type).toBe('circle')
expect(draw.get(0).get(1).attr('fill')).toBe('#ff0066')
})
it('does not import on single elements, even with an argument it acts as a getter', function() {
var rect = draw.rect(100,100).id(null)
, result = rect.svg('<circle r="300"></rect>')
expect(
result === '<rect width="100" height="100"></rect>'
|| result === '<rect height="100" width="100"></rect>').toBeTruthy()
})
})
})
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)
var m = draw.node.getScreenCTM()
// alert([m.a, m.a, m.c, m.d, m.e, m.f].join(', '))
var translation = {x: m.e, y: m.f}
var pos = {x: 2, y:5}
expect(rect.point(pos.x, pos.y).x).toBeCloseTo(pos.x - translation.x)
expect(rect.point(pos.x, pos.y).y).toBeCloseTo(pos.y - translation.y)
})
})
})
|