選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ClipPath.js 2.9KB

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