From e6fda9da8f777786a98c9351f33823241ce864fd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ulrich-Matthias=20Sch=C3=A4fer?= Date: Fri, 10 Apr 2020 15:15:29 +1000 Subject: [PATCH] added tests for Mask --- spec/spec/elements/ClipPath.js | 2 +- spec/spec/elements/Mask.js | 95 ++++++++++++++++++++++++++++++++++ src/elements/Mask.js | 8 +-- 3 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 spec/spec/elements/Mask.js diff --git a/spec/spec/elements/ClipPath.js b/spec/spec/elements/ClipPath.js index a476884..d1d799b 100644 --- a/spec/spec/elements/ClipPath.js +++ b/spec/spec/elements/ClipPath.js @@ -42,7 +42,7 @@ describe('ClipPath.js', () => { }) describe('Container', () => { - describe('circle()', () => { + describe('clip()', () => { it('creates a clipPath in the defs', () => { const canvas = SVG() const clip = canvas.clip() diff --git a/spec/spec/elements/Mask.js b/spec/spec/elements/Mask.js new file mode 100644 index 0000000..b9c97f3 --- /dev/null +++ b/spec/spec/elements/Mask.js @@ -0,0 +1,95 @@ +/* globals describe, expect, it, spyOn, jasmine, container */ + +import { Mask, SVG, Container, Rect } from '../../../src/main.js' + +const { any } = jasmine + +describe('Mask.js', () => { + + describe('()', () => { + it('creates a new object of type Mask', () => { + expect(new Mask()).toEqual(any(Mask)) + }) + + it('sets passed attributes on the element', () => { + expect(new Mask({ id: 'foo' }).id()).toBe('foo') + }) + }) + + describe('remove()', () => { + it('unmasks all targets', () => { + const canvas = SVG().addTo(container) + const mask = canvas.mask() + const rect = canvas.rect(100, 100).maskWith(mask) + expect(mask.remove()).toBe(mask) + expect(rect.masker()).toBe(null) + }) + + it('calls remove on parent class', () => { + const mask = new Mask() + const spy = spyOn(Container.prototype, 'remove') + mask.remove() + expect(spy).toHaveBeenCalled() + }) + }) + + describe('targets()', () => { + it('gets all targets of this maskPath', () => { + const canvas = SVG().addTo(container) + const mask = canvas.mask() + const rect = canvas.rect(100, 100).maskWith(mask) + expect(mask.targets()).toEqual([ rect ]) + }) + }) + + describe('Container', () => { + describe('mask()', () => { + it('creates a maskPath in the defs', () => { + const canvas = SVG() + const mask = canvas.mask() + expect(mask).toEqual(any(Mask)) + expect(canvas.defs().children()).toEqual([ mask ]) + }) + }) + }) + + describe('Element', () => { + describe('masker()', () => { + it('returns the instance of Mask the current element is maskped with', () => { + const canvas = SVG().addTo(container) + const mask = canvas.mask() + const rect = canvas.rect(100, 100).maskWith(mask) + expect(rect.masker()).toEqual(mask) + }) + + it('returns null if no maskPath was found', () => { + expect(new Rect().masker()).toBe(null) + }) + }) + + describe('maskWith()', () => { + it('sets the mask attribute on the element to the id of the maskPath', () => { + const mask = new Mask().id('foo') + const rect = new Rect().maskWith(mask) + expect(rect.attr('mask')).toBe('url("#foo")') + }) + + it('creates a maskPath and appends the passed element to it to mask current element', () => { + const canvas = SVG().addTo(container) + const circle = canvas.circle(40) + const rect = canvas.rect(100, 100).maskWith(circle) + expect(circle.parent()).toEqual(any(Mask)) + expect(rect.attr('mask')).toBe(`url("#${circle.parent().id()}")`) + }) + }) + + describe('unmask()', () => { + it('sets the mask-target attribute to null and returns itself', () => { + const mask = new Mask().id('foo') + const rect = new Rect().maskWith(mask) + expect(rect.unmask()).toBe(rect) + expect(rect.attr('mask')).toBe(undefined) + }) + }) + }) +}) diff --git a/src/elements/Mask.js b/src/elements/Mask.js index 64caca5..3d2119e 100644 --- a/src/elements/Mask.js +++ b/src/elements/Mask.js @@ -33,6 +33,10 @@ registerMethods({ }, Element: { // Distribute mask to svg element + masker () { + return this.reference('mask') + }, + maskWith (element) { // use given mask or create a new one var masker = element instanceof Mask @@ -46,10 +50,6 @@ registerMethods({ // Unmask element unmask () { return this.attr('mask', null) - }, - - masker () { - return this.reference('mask') } } }) -- 2.39.5