Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Svg.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* globals describe, expect, it, jasmine, container */
  2. import { Svg, SVG, Defs } from '../../../src/main.js'
  3. import { svg as ns, xlink } from '../../../src/modules/core/namespaces.js'
  4. import { getWindow } from '../../../src/utils/window.js'
  5. const { any } = jasmine
  6. describe('Svg.js', () => {
  7. describe('()', () => {
  8. it('creates a new object of type Svg', () => {
  9. expect(new Svg()).toEqual(any(Svg))
  10. })
  11. it('sets passed attributes on the element', () => {
  12. expect(new Svg({ id: 'foo' }).id()).toBe('foo')
  13. })
  14. it('creates namespaces on creation', () => {
  15. const svg = new Svg()
  16. expect(svg.attr('xmlns')).toBe(ns)
  17. expect(svg.attr('version')).toBe(1.1)
  18. expect(svg.attr('xmlns:xlink')).toBe(xlink)
  19. })
  20. })
  21. describe('defs()', () => {
  22. it('returns the defs if its the root svg', () => {
  23. const svg = new Svg()
  24. const defs = new Defs().addTo(svg)
  25. expect(svg.defs()).toBe(defs)
  26. })
  27. it('returns the defs if its not the root svg', () => {
  28. const svg = new Svg()
  29. const defs = new Defs().addTo(svg)
  30. const nested = new Svg().addTo(svg)
  31. expect(nested.defs()).toBe(defs)
  32. })
  33. it('creates the defs if not found', () => {
  34. const svg = new SVG()
  35. expect(svg.findOne('defs')).toBe(null)
  36. const defs = svg.defs()
  37. expect(svg.findOne('defs')).toBe(defs)
  38. })
  39. })
  40. describe('namespace()', () => {
  41. it('returns itself', () => {
  42. const svg = SVG('<svg />')
  43. expect(svg.namespace()).toBe(svg)
  44. })
  45. it('creates the namespace attributes on the svg', () => {
  46. const svg = SVG('<svg />')
  47. expect(svg.attr('xmlns')).toBe(undefined)
  48. svg.namespace()
  49. expect(svg.attr('xmlns')).toBe(ns)
  50. expect(svg.attr('version')).toBe(1.1)
  51. expect(svg.attr('xmlns:xlink')).toBe(xlink)
  52. })
  53. })
  54. describe('isRoot()', () => {
  55. it('returns true if svg is the root svg', () => {
  56. const canvas = SVG().addTo(container)
  57. expect(canvas.isRoot()).toBe(true)
  58. })
  59. it('returns true if its detached from the dom', () => {
  60. const svg = new Svg()
  61. expect(svg.isRoot()).toBe(true)
  62. })
  63. it('returns true if its the root child of the document', () => {
  64. // cannot be tested here
  65. })
  66. it('returns false if its the child of a document-fragment', () => {
  67. const fragment = getWindow().document.createDocumentFragment()
  68. const svg = new Svg().addTo(fragment)
  69. expect(svg.isRoot()).toBe(false)
  70. })
  71. it('returns false if its a child of another svg element', () => {
  72. const svg = new Svg()
  73. const nested = new Svg().addTo(svg)
  74. expect(nested.isRoot()).toBe(false)
  75. })
  76. })
  77. describe('removeNamespace()', () => {
  78. it('returns itself', () => {
  79. const svg = new Svg()
  80. expect(svg.removeNamespace()).toBe(svg)
  81. })
  82. it('removes the namespace attributes from the svg element', () => {
  83. const svg = new Svg()
  84. expect(svg.attr('xmlns')).toBe(ns)
  85. svg.removeNamespace()
  86. expect(svg.attr('xmlns')).toBe(undefined)
  87. expect(svg.attr('version')).toBe(undefined)
  88. expect(svg.attr('xmlns:xlink')).toBe(undefined)
  89. expect(svg.attr('xmlns:svgjs')).toBe(undefined)
  90. })
  91. })
  92. describe('root()', () => {
  93. it('returns itself if its the root svg', () => {
  94. const svg = new Svg()
  95. expect(svg.root()).toBe(svg)
  96. })
  97. it('returns the actual root if its not the root svg', () => {
  98. const svg = new Svg()
  99. const nested = new Svg().addTo(svg)
  100. expect(nested.root()).toBe(svg)
  101. })
  102. })
  103. describe('Container', () => {
  104. describe('nested()', () => {
  105. it('creates an svg element in the container', () => {
  106. const svg = new Svg()
  107. const nested = svg.nested()
  108. expect(nested).toEqual(any(Svg))
  109. expect(nested.parent()).toBe(svg)
  110. })
  111. it('has no namespaces set', () => {
  112. const svg = new Svg()
  113. const nested = svg.nested()
  114. expect(nested.attr('xmlns')).toBe(undefined)
  115. expect(nested.attr('version')).toBe(undefined)
  116. expect(nested.attr('xmlns:xlink')).toBe(undefined)
  117. expect(nested.attr('xmlns:svgjs')).toBe(undefined)
  118. })
  119. })
  120. })
  121. })