]> source.dussan.org Git - svg.js.git/commitdiff
added tests for Pattern
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>
Fri, 10 Apr 2020 05:55:22 +0000 (15:55 +1000)
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>
Fri, 10 Apr 2020 05:55:22 +0000 (15:55 +1000)
spec/spec/elements/Pattern.js [new file with mode: 0644]
src/elements/Pattern.js

diff --git a/spec/spec/elements/Pattern.js b/spec/spec/elements/Pattern.js
new file mode 100644 (file)
index 0000000..fa6c0b8
--- /dev/null
@@ -0,0 +1,76 @@
+/* globals describe, expect, it, spyOn, jasmine, container */
+
+import { Pattern, SVG, Container } from '../../../src/main.js'
+
+const { any, objectContaining, createSpy } = jasmine
+
+describe('Pattern.js', () => {
+  describe('()', () => {
+    it('creates a new object of type Pattern', () => {
+      const pattern = new Pattern()
+      expect(pattern).toEqual(any(Pattern))
+    })
+
+    it('sets passed attributes on the element', () => {
+      expect(new Pattern({ id: 'foo' }).id()).toBe('foo')
+    })
+  })
+
+  describe('attr()', () => {
+    it('relays to parents attr method for any call except transformation', () => {
+      const pattern = new Pattern()
+      const spy = spyOn(Container.prototype, 'attr')
+      pattern.attr(1, 2, 3)
+      pattern.attr('transform', 2, 3)
+
+      expect(spy).toHaveBeenCalledWith(1, 2, 3)
+      expect(spy).toHaveBeenCalledWith('patternTransform', 2, 3)
+    })
+  })
+
+  describe('bbox()', () => {
+    it('returns an empty box', () => {
+      expect(new Pattern().bbox().isNulled()).toBe(true)
+    })
+  })
+
+  describe('targets()', () => {
+    it('gets all targets of this pattern', () => {
+      const canvas = SVG().addTo(container)
+      const pattern = canvas.pattern('linear')
+      const rect = canvas.rect(100, 100).fill(pattern)
+      expect(pattern.targets()).toEqual([ rect ])
+    })
+  })
+
+  describe('toString()', () => {
+    it('calls url() and returns the result', () => {
+      const pattern = new Pattern()
+      expect(pattern.toString()).toBe(pattern.url())
+    })
+  })
+
+  describe('update()', () => {
+    it('clears the element', () => {
+      const pattern = new Pattern()
+      pattern.rect(100, 100)
+      expect(pattern.update().children()).toEqual([])
+    })
+
+    it('executes a function in the context of the pattern', () => {
+      const pattern = new Pattern()
+      const spy = createSpy('pattern')
+      pattern.update(spy)
+      expect(spy.calls.all()).toEqual([
+        objectContaining({ object: pattern, args: [ pattern ] })
+      ])
+    })
+  })
+
+  describe('url()', () => {
+    it('returns url(#id)', () => {
+      const pattern = new Pattern().id('foo')
+      expect(pattern.url()).toBe('url("#foo")')
+    })
+  })
+})
index 72b7c59110eb6d5374ff3d22b85e34bd11663094..be034232a0f3435cb9e345d2648d6b46d1a36e2e 100644 (file)
@@ -10,9 +10,23 @@ export default class Pattern extends Container {
     super(nodeOrNew('pattern', node), attrs)
   }
 
-  // Return the fill id
-  url () {
-    return 'url(#' + this.id() + ')'
+  // custom attr to handle transform
+  attr (a, b, c) {
+    if (a === 'transform') a = 'patternTransform'
+    return super.attr(a, b, c)
+  }
+
+  bbox () {
+    return new Box()
+  }
+
+  targets () {
+    return baseFind('svg [fill*="' + this.id() + '"]')
+  }
+
+  // Alias string convertion to fill
+  toString () {
+    return this.url()
   }
 
   // Update pattern by rebuilding
@@ -28,24 +42,11 @@ export default class Pattern extends Container {
     return this
   }
 
-  // Alias string convertion to fill
-  toString () {
-    return this.url()
-  }
-
-  // custom attr to handle transform
-  attr (a, b, c) {
-    if (a === 'transform') a = 'patternTransform'
-    return super.attr(a, b, c)
-  }
-
-  targets () {
-    return baseFind('svg [fill*="' + this.id() + '"]')
+  // Return the fill id
+  url () {
+    return 'url("#' + this.id() + '")'
   }
 
-  bbox () {
-    return new Box()
-  }
 }
 
 registerMethods({