You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Mask.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* globals describe, expect, it, spyOn, jasmine, container */
  2. import { Mask, SVG, Container, Rect } from '../../../src/main.js'
  3. const { any } = jasmine
  4. describe('Mask.js', () => {
  5. describe('()', () => {
  6. it('creates a new object of type Mask', () => {
  7. expect(new Mask()).toEqual(any(Mask))
  8. })
  9. it('sets passed attributes on the element', () => {
  10. expect(new Mask({ id: 'foo' }).id()).toBe('foo')
  11. })
  12. })
  13. describe('remove()', () => {
  14. it('unmasks all targets', () => {
  15. const canvas = SVG().addTo(container)
  16. const mask = canvas.mask()
  17. const rect = canvas.rect(100, 100).maskWith(mask)
  18. expect(mask.remove()).toBe(mask)
  19. expect(rect.masker()).toBe(null)
  20. })
  21. it('calls remove on parent class', () => {
  22. const mask = new Mask()
  23. const spy = spyOn(Container.prototype, 'remove')
  24. mask.remove()
  25. expect(spy).toHaveBeenCalled()
  26. })
  27. })
  28. describe('targets()', () => {
  29. it('gets all targets of this maskPath', () => {
  30. const canvas = SVG().addTo(container)
  31. const mask = canvas.mask()
  32. const rect = canvas.rect(100, 100).maskWith(mask)
  33. expect(mask.targets()).toEqual([ rect ])
  34. })
  35. })
  36. describe('Container', () => {
  37. describe('mask()', () => {
  38. it('creates a maskPath in the defs', () => {
  39. const canvas = SVG()
  40. const mask = canvas.mask()
  41. expect(mask).toEqual(any(Mask))
  42. expect(canvas.defs().children()).toEqual([ mask ])
  43. })
  44. })
  45. })
  46. describe('Element', () => {
  47. describe('masker()', () => {
  48. it('returns the instance of Mask the current element is masked with', () => {
  49. const canvas = SVG().addTo(container)
  50. const mask = canvas.mask()
  51. const rect = canvas.rect(100, 100).maskWith(mask)
  52. expect(rect.masker()).toEqual(mask)
  53. })
  54. it('returns null if no maskPath was found', () => {
  55. expect(new Rect().masker()).toBe(null)
  56. })
  57. })
  58. describe('maskWith()', () => {
  59. it('sets the mask attribute on the element to the id of the maskPath', () => {
  60. const mask = new Mask().id('foo')
  61. const rect = new Rect().maskWith(mask)
  62. expect(rect.attr('mask')).toBe('url(#foo)')
  63. })
  64. it('creates a maskPath and appends the passed element to it to mask current element', () => {
  65. const canvas = SVG().addTo(container)
  66. const circle = canvas.circle(40)
  67. const rect = canvas.rect(100, 100).maskWith(circle)
  68. expect(circle.parent()).toEqual(any(Mask))
  69. expect(rect.attr('mask')).toBe(`url(#${circle.parent().id()})`)
  70. })
  71. })
  72. describe('unmask()', () => {
  73. it('sets the mask-target attribute to null and returns itself', () => {
  74. const mask = new Mask().id('foo')
  75. const rect = new Rect().maskWith(mask)
  76. expect(rect.unmask()).toBe(rect)
  77. expect(rect.attr('mask')).toBe(undefined)
  78. })
  79. })
  80. })
  81. })