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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* globals describe, expect, it, beforeEach */
  2. import { PathArray, Box } from '../../../src/main.js'
  3. describe('PathArray.js', () => {
  4. let p1, p2, p3
  5. beforeEach(() => {
  6. p1 = new PathArray('m10 10 h 80 v 80 h -80 l 300 400 z')
  7. p2 = new PathArray('m10 80 c 40 10 65 10 95 80 s 150 150 180 80 t 300 300 q 52 10 95 80 z')
  8. p3 = new PathArray('m80 80 A 45 45, 0, 0, 0, 125 125 L 125 80 z')
  9. })
  10. it('parses flat arrays correctly', () => {
  11. const arr = new PathArray([ 'M', 0, 0, 'L', 100, 100, 'z' ])
  12. expect(arr.toString()).toBe('M0 0L100 100Z ')
  13. })
  14. it('parses nested arrays correctly', () => {
  15. const arr = new PathArray([ [ 'M', 0, 0 ], [ 'L', 100, 100 ], [ 'z' ] ])
  16. expect(arr.toString()).toBe('M0 0L100 100Z ')
  17. })
  18. // this test is designed to cover a certain line but it doesn't work because of #608
  19. it('returns the valueOf when PathArray is given', () => {
  20. const p = new PathArray('m10 10 h 80 v 80 h -80 l 300 400 z')
  21. expect((new PathArray(p))).toEqual(p)
  22. })
  23. describe('move()', () => {
  24. it('moves all points in a straight path', () => {
  25. expect(p1.move(100, 200).toString()).toBe('M100 200H180V280H100L400 680Z ')
  26. })
  27. it('moves all points in a curved path', () => {
  28. expect(p2.move(100, 200).toString()).toBe('M100 200C140 210 165 210 195 280S345 430 375 360T675 660Q727 670 770 740Z ')
  29. })
  30. it('moves all points in a arc path', () => {
  31. expect(p3.move(100, 200).toString()).toBe('M100 200A45 45 0 0 0 145 245L145 200Z ')
  32. })
  33. it('does nothing if passed number is not a number', () => {
  34. expect(p3.move()).toEqual(p3)
  35. })
  36. })
  37. describe('size()', () => {
  38. it('resizes all points in a straight path', () => {
  39. expect(p1.size(600, 200).toString()).toBe('M10 10H170V43.333333333333336H10L610 210Z ')
  40. })
  41. it('resizes all points in a curved path', () => {
  42. expect(p2.size(600, 200).toString()).toBe('M10 80C45.82089552238806 83.70370370370371 68.2089552238806 83.70370370370371 95.07462686567165 109.62962962962963S229.40298507462686 165.1851851851852 256.2686567164179 139.25925925925927T524.9253731343283 250.37037037037038Q571.4925373134329 254.07407407407408 610 280Z ')
  43. })
  44. it('resizes all points in a arc path', () => {
  45. const expected = [
  46. [ 'M', 80, 80 ],
  47. [ 'A', 600, 200, 0, 0, 0, 680, 280 ],
  48. [ 'L', 680, 80 ],
  49. [ 'Z' ]
  50. ]
  51. const toBeTested = p3.size(600, 200)
  52. for (let i = toBeTested.length; i--;) {
  53. expect(toBeTested[i].shift().toUpperCase()).toBe(expected[i].shift().toUpperCase())
  54. for (let j = toBeTested[i].length; j--;) {
  55. expect(toBeTested[i][j]).toBeCloseTo(expected[i][j])
  56. }
  57. }
  58. })
  59. })
  60. describe('bbox()', () => {
  61. it('calculates the bounding box of the PathArray', () => {
  62. const box = new PathArray('M0 0 L 10 10').bbox()
  63. expect(box).toEqual(new Box(0, 0, 10, 10))
  64. })
  65. })
  66. })