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

textable.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* globals describe, expect, it, beforeEach, spyOn, container */
  2. import { SVG, Box, Tspan } from '../../../../src/main.js'
  3. describe('textable.js', () => {
  4. var canvas, text, tspan
  5. beforeEach(() => {
  6. canvas = SVG().addTo(container)
  7. text = canvas.text('Hello World\nIn two lines')
  8. tspan = text.get(0)
  9. })
  10. describe('x()', () => {
  11. it('returns the value of x without an argument on a text', () => {
  12. expect(text.x(0).x()).toBe(0)
  13. })
  14. it('sets the x value of the bbox with the first argument on a text', () => {
  15. text.x(123)
  16. expect(text.bbox().x).toBe(123)
  17. })
  18. it('sets the value of all lines', () => {
  19. text.x(200)
  20. text.each(function () {
  21. expect(this.x()).toBe(text.x())
  22. })
  23. })
  24. it('returns the value of x without an argument on a tspan', () => {
  25. expect(tspan.x(10).x()).toBe(10)
  26. })
  27. it('sets the x value of the bbox with the first argument on a tspan', () => {
  28. tspan.x(123)
  29. expect(tspan.bbox().x).toBe(123)
  30. })
  31. })
  32. describe('y()', () => {
  33. it('returns the value of y without an argument on a text', () => {
  34. expect(text.y(0).y()).toBe(0)
  35. })
  36. it('sets the y value of the bbox with the first argument on a text', () => {
  37. text.y(123)
  38. expect(text.bbox().y).toBe(123)
  39. })
  40. it('sets the y position of first line', () => {
  41. text.y(200)
  42. expect(text.first().y()).toBe(text.y())
  43. })
  44. it('returns the value of y without an argument on a tspan', () => {
  45. expect(tspan.y(10).y()).toBe(10)
  46. })
  47. it('sets the y value of the bbox with the first argument on a tspan', () => {
  48. tspan.y(123)
  49. expect(tspan.bbox().y).toBe(123)
  50. })
  51. })
  52. describe('move()', () => {
  53. it('calls x() and y() with parameters on text', () => {
  54. const spyX = spyOn(text, 'x').and.callThrough()
  55. const spyY = spyOn(text, 'y').and.callThrough()
  56. const box = new Box()
  57. text.move(1, 2, box)
  58. expect(spyX).toHaveBeenCalledWith(1, box)
  59. expect(spyY).toHaveBeenCalledWith(2, box)
  60. })
  61. it('calls x() and y() with parameters on tspan', () => {
  62. const spyX = spyOn(tspan, 'x').and.callThrough()
  63. const spyY = spyOn(tspan, 'y').and.callThrough()
  64. const box = new Box()
  65. tspan.move(1, 2, box)
  66. expect(spyX).toHaveBeenCalledWith(1, box)
  67. expect(spyY).toHaveBeenCalledWith(2, box)
  68. })
  69. })
  70. describe('ax()', () => {
  71. it('sets the value of x with a percent value with Text', () => {
  72. text.ax('40%')
  73. expect(text.node.getAttribute('x')).toBe('40%')
  74. })
  75. it('returns the value of x when x is a percentual value with Text', () => {
  76. expect(text.ax('40%').ax()).toBe('40%')
  77. })
  78. it('sets the value of x with a percent value with Tspan', () => {
  79. tspan.ax('40%')
  80. expect(tspan.node.getAttribute('x')).toBe('40%')
  81. })
  82. it('returns the value of x when x is a percentual value with Tspan', () => {
  83. tspan.ax('40%')
  84. expect(tspan.ax()).toBe('40%')
  85. })
  86. })
  87. describe('ay()', () => {
  88. it('sets the value of y with a percent value with Text', () => {
  89. text.ay('40%')
  90. expect(text.node.getAttribute('y')).toBe('40%')
  91. })
  92. it('returns the value of y when y is a percentual value with Tspan', () => {
  93. expect(text.ay('45%').ay()).toBe('45%')
  94. })
  95. it('sets the value of y with a percent value with Text', () => {
  96. tspan.ay('40%')
  97. expect(tspan.node.getAttribute('y')).toBe('40%')
  98. })
  99. it('returns the value of y when y is a percentual value with Tspan', () => {
  100. tspan.ay('40%')
  101. expect(tspan.ay()).toBe('40%')
  102. })
  103. })
  104. describe('move()', () => {
  105. it('calls ax() and ay() with parameters on text', () => {
  106. const spyX = spyOn(text, 'ax').and.callThrough()
  107. const spyY = spyOn(text, 'ay').and.callThrough()
  108. text.amove(1, 2)
  109. expect(spyX).toHaveBeenCalledWith(1)
  110. expect(spyY).toHaveBeenCalledWith(2)
  111. })
  112. it('calls ax() and ay() with parameters on tspan', () => {
  113. const spyX = spyOn(tspan, 'ax').and.callThrough()
  114. const spyY = spyOn(tspan, 'ay').and.callThrough()
  115. tspan.amove(1, 2)
  116. expect(spyX).toHaveBeenCalledWith(1)
  117. expect(spyY).toHaveBeenCalledWith(2)
  118. })
  119. })
  120. // this is a hackish. It should not be neccessary to use toBeCloseTo but bbox with text is a thing...
  121. describe('cx()', () => {
  122. it('returns the value of cx without an argument with Text', () => {
  123. var box = text.bbox()
  124. expect(text.cx()).toBeCloseTo(box.x + box.width / 2)
  125. })
  126. it('sets the value of cx with the first argument with Text', () => {
  127. text.cx(123)
  128. var box = text.bbox()
  129. expect(box.cx).toBeCloseTo(box.x + box.width / 2)
  130. })
  131. it('returns the value of cx without an argument with Tspan', () => {
  132. var box = tspan.bbox()
  133. expect(tspan.cx()).toBeCloseTo(box.x + box.width / 2)
  134. })
  135. it('sets the value of cx with the first argument with Tspan', () => {
  136. tspan.cx(123)
  137. var box = tspan.bbox()
  138. expect(box.cx).toBeCloseTo(box.x + box.width / 2)
  139. })
  140. })
  141. describe('cy()', () => {
  142. it('returns the value of cy without an argument with Tspan', () => {
  143. var box = tspan.bbox()
  144. expect(tspan.cy()).toBe(box.cy)
  145. })
  146. it('sets the value of cy with the first argument with Tspan', () => {
  147. tspan.cy(345)
  148. var box = tspan.bbox()
  149. expect(Math.round(box.cy * 10) / 10).toBe(345)
  150. })
  151. it('returns the value of cy without an argument with Tspan', () => {
  152. var box = tspan.bbox()
  153. expect(tspan.cy()).toBe(box.cy)
  154. })
  155. it('sets the value of cy with the first argument with Tspan', () => {
  156. tspan.cy(345)
  157. var box = tspan.bbox()
  158. expect(Math.round(box.cy * 10) / 10).toBe(345)
  159. })
  160. })
  161. describe('center()', () => {
  162. it('calls cx() and cy() with parameters on Text', () => {
  163. const spyX = spyOn(text, 'cx').and.callThrough()
  164. const spyY = spyOn(text, 'cy').and.callThrough()
  165. const box = new Box()
  166. text.center(1, 2, box)
  167. expect(spyX).toHaveBeenCalledWith(1, box)
  168. expect(spyY).toHaveBeenCalledWith(2, box)
  169. })
  170. it('calls cx() and cy() with parameters on Tspan', () => {
  171. const spyX = spyOn(tspan, 'cx').and.callThrough()
  172. const spyY = spyOn(tspan, 'cy').and.callThrough()
  173. const box = new Box()
  174. tspan.center(1, 2, box)
  175. expect(spyX).toHaveBeenCalledWith(1, box)
  176. expect(spyY).toHaveBeenCalledWith(2, box)
  177. })
  178. })
  179. describe('plain()', () => {
  180. it('adds content without a tspan with Text', () => {
  181. text.plain('It is a bear!')
  182. expect(text.node.childNodes[0].nodeType).toBe(3)
  183. expect(text.node.childNodes[0].data).toBe('It is a bear!')
  184. })
  185. it('clears content before adding new content with Text', () => {
  186. text.plain('It is not a bear!')
  187. expect(text.node.childNodes.length).toBe(1)
  188. expect(text.node.childNodes[0].data).toBe('It is not a bear!')
  189. })
  190. it('restores the content from the dom with Text', () => {
  191. text.plain('Just plain text!')
  192. expect(text.text()).toBe('Just plain text!')
  193. })
  194. it('adds content without a tspan with Tspan', () => {
  195. tspan.plain('It is a bear!')
  196. expect(tspan.node.childNodes[0].nodeType).toBe(3)
  197. expect(tspan.node.childNodes[0].data).toBe('It is a bear!')
  198. })
  199. it('clears content before adding new content with Tspan', () => {
  200. tspan.plain('It is not a bear!')
  201. expect(tspan.node.childNodes.length).toBe(1)
  202. expect(tspan.node.childNodes[0].data).toBe('It is not a bear!')
  203. })
  204. it('restores the content from the dom with Tspan', () => {
  205. // We create a new Tspan here because the one used before was part of text creation
  206. // and therefore is marked as newline and that is not what we want to test
  207. const tspan = new Tspan().plain('Just plain text!')
  208. expect(tspan.text()).toBe('Just plain text!')
  209. })
  210. })
  211. describe('length()', () => {
  212. it('gets total length of text', () => {
  213. text.text(function (add) {
  214. add.tspan('The first.')
  215. add.tspan('The second.')
  216. add.tspan('The third.')
  217. })
  218. expect(text.length()).toBeCloseTo(text.get(0).length() + text.get(1).length() + text.get(2).length(), 3)
  219. })
  220. it('gets total length of tspan', () => {
  221. tspan.text(function (add) {
  222. add.tspan('The first.')
  223. add.tspan('The second.')
  224. add.tspan('The third.')
  225. })
  226. expect(tspan.length()).toBeCloseTo(tspan.get(0).length() + tspan.get(1).length() + tspan.get(2).length(), 3)
  227. })
  228. })
  229. describe('build()', () => {
  230. it('enables adding multiple plain text nodes when given true for Text', () => {
  231. text.clear().build(true)
  232. text.plain('A great piece!')
  233. text.plain('Another great piece!')
  234. expect(text.node.childNodes[0].data).toBe('A great piece!')
  235. expect(text.node.childNodes[1].data).toBe('Another great piece!')
  236. })
  237. it('enables adding multiple tspan nodes when given true for Text', () => {
  238. text.clear().build(true)
  239. text.tspan('A great piece!')
  240. text.tspan('Another great piece!')
  241. expect(text.node.childNodes[0].childNodes[0].data).toBe('A great piece!')
  242. expect(text.node.childNodes[1].childNodes[0].data).toBe('Another great piece!')
  243. })
  244. it('disables adding multiple plain text nodes when given false for Text', () => {
  245. text.clear().build(true)
  246. text.plain('A great piece!')
  247. text.build(false).plain('Another great piece!')
  248. expect(text.node.childNodes[0].data).toBe('Another great piece!')
  249. expect(text.node.childNodes[1]).toBe(undefined)
  250. })
  251. it('disables adding multiple tspan nodes when given false for Text', () => {
  252. text.clear().build(true)
  253. text.tspan('A great piece!')
  254. text.build(false).tspan('Another great piece!')
  255. expect(text.node.childNodes[0].childNodes[0].data).toBe('Another great piece!')
  256. expect(text.node.childNodes[1]).toBe(undefined)
  257. })
  258. it('enables adding multiple plain text nodes when given true for Tspan', () => {
  259. tspan.clear().build(true)
  260. tspan.plain('A great piece!')
  261. tspan.plain('Another great piece!')
  262. expect(tspan.node.childNodes[0].data).toBe('A great piece!')
  263. expect(tspan.node.childNodes[1].data).toBe('Another great piece!')
  264. })
  265. it('enables adding multiple text nodes when given true for Tspan', () => {
  266. tspan.clear().build(true)
  267. tspan.tspan('A great piece!')
  268. tspan.tspan('Another great piece!')
  269. expect(tspan.node.childNodes[0].childNodes[0].data).toBe('A great piece!')
  270. expect(tspan.node.childNodes[1].childNodes[0].data).toBe('Another great piece!')
  271. })
  272. it('disables adding multiple plain text nodes when given false for Tspan', () => {
  273. tspan.clear().build(true)
  274. tspan.plain('A great piece!')
  275. tspan.build(false).plain('Another great piece!')
  276. expect(tspan.node.childNodes[0].data).toBe('Another great piece!')
  277. expect(tspan.node.childNodes[1]).toBe(undefined)
  278. })
  279. it('disables adding multiple tspan nodes when given false for Tspan', () => {
  280. tspan.clear().build(true)
  281. tspan.tspan('A great piece!')
  282. tspan.build(false).tspan('Another great piece!')
  283. expect(tspan.node.childNodes[0].childNodes[0].data).toBe('Another great piece!')
  284. expect(tspan.node.childNodes[1]).toBe(undefined)
  285. })
  286. })
  287. })