standard.spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict'
  2. /* global describe, it, expect, jsPDF, comparePdf */
  3. /**
  4. * Standard spec tests
  5. *
  6. * These tests return the datauristring so that reference files can be generated.
  7. * We compare the exact output.
  8. */
  9. describe('Standard Text', () => {
  10. it('should load', () => {
  11. // assertions here]
  12. expect(typeof jsPDF).toBe('function')
  13. })
  14. it('should generate blank page', () => {
  15. const doc = jsPDF()
  16. comparePdf(doc.output(), 'blank.pdf', 'text')
  17. })
  18. it('should allow text insertion', () => {
  19. const doc = jsPDF()
  20. doc.text(10, 10, 'This is a test!')
  21. comparePdf(doc.output(), 'standard.pdf', 'text')
  22. })
  23. it('should allow text insertion at an angle', () => {
  24. const doc = jsPDF()
  25. doc.text(20, 20, 'This is a test!', null, 20)
  26. comparePdf(doc.output(), 'angle.pdf', 'text')
  27. })
  28. it('should render different font faces', () => {
  29. const doc = jsPDF()
  30. doc.text(20, 20, 'This is the default font.')
  31. doc.setFont('courier')
  32. doc.setFontType('normal')
  33. doc.text(20, 30, 'This is courier normal.')
  34. doc.setFont('times')
  35. doc.setFontType('italic')
  36. doc.text(20, 40, 'This is times italic.')
  37. doc.setFont('helvetica')
  38. doc.setFontType('bold')
  39. doc.text(20, 50, 'This is helvetica bold.')
  40. doc.setFont('courier')
  41. doc.setFontType('bolditalic')
  42. doc.text(20, 60, 'This is courier bolditalic.')
  43. comparePdf(doc.output(), 'font-faces.pdf', 'text')
  44. })
  45. it('should support multiple pages', () => {
  46. const doc = jsPDF()
  47. doc.text(20, 20, 'Hello world!')
  48. doc.text(20, 30, 'This is client-side JavaScript, pumping out a PDF.')
  49. doc.addPage()
  50. doc.text(20, 20, 'Do you like that?')
  51. comparePdf(doc.output(), 'two-page.pdf', 'text')
  52. })
  53. it('should support different size fonts', () => {
  54. const doc = jsPDF()
  55. doc.setFontSize(22)
  56. doc.text(20, 20, 'This is a title')
  57. doc.setFontSize(16)
  58. doc.text(20, 30, 'This is some normal sized text underneath.')
  59. comparePdf(doc.output(), 'different-sizes.pdf', 'text')
  60. })
  61. it('should support multiline text', () => {
  62. const doc = jsPDF()
  63. doc.text(20, 20, `This is a line
  64. break`)
  65. comparePdf(doc.output(), 'line-break.pdf', 'text')
  66. })
  67. it('should support strokes', () => {
  68. const doc = jsPDF()
  69. doc.text('Stroke on', 20, 20, { stroke: true })
  70. doc.text('Stroke on', 20, 40, { stroke: true })
  71. doc.text('Stroke off', 20, 60, { stroke: false })
  72. doc.text('Stroke on', 20, 80, { stroke: true })
  73. comparePdf(doc.output(), 'stroke.pdf', 'text')
  74. })
  75. // @TODO: Implement passing color as a name
  76. it('should display two red lines of text', () => {
  77. const doc = jsPDF()
  78. doc.setTextColor('#FF0000')
  79. doc.text('Red on', 20, 20)
  80. doc.setTextColor(255, 0, 0)
  81. doc.text('Red on', 20, 40)
  82. comparePdf(doc.output(), 'color.pdf', 'text')
  83. })
  84. it('should display one line of red, one black', () => {
  85. const doc = jsPDF()
  86. doc.setTextColor('#FF0000')
  87. doc.text('Red', 20, 20)
  88. doc.setTextColor('#000000')
  89. doc.text('Black', 20, 40)
  90. comparePdf(doc.output(), 'red-black.pdf', 'text')
  91. })
  92. // @TODO: Document alignment
  93. it('should center align text', () => {
  94. const doc = jsPDF()
  95. doc.setFont('times')
  96. doc.setFontType('normal')
  97. doc.text(105, 80, 'This is centred text.', null, null, 'center')
  98. doc.text(105, 90, 'And a little bit more underneath it.', null, null, 'center')
  99. doc.text(200, 100, 'This is right aligned text', null, null, 'right')
  100. doc.text(200, 110, 'And some more', null, null, 'right')
  101. doc.text(20, 120, 'Back to left')
  102. comparePdf(doc.output(), 'alignment.pdf', 'text')
  103. })
  104. it('should throw an error if not a string', () => {
  105. expect(() => {
  106. const doc = jsPDF()
  107. doc.text(10, 10, 43290943)
  108. }).toThrow(new Error('Type of text must be string or Array. "43290943" is not recognized.'))
  109. })
  110. it('should throw an error when passed incorrect alignment', () => {
  111. expect(() => {
  112. const doc = jsPDF()
  113. doc.text(105, 80, 'This is text with a moose alignment.', null, null, 'moose')
  114. }).toThrow(new Error('Unrecognized alignment option, use "center" or "right".'))
  115. })
  116. it('should render letter spaced text', () => {
  117. const doc = jsPDF()
  118. doc.lstext('hello', 10, 10, 2)
  119. doc.lstext('hello', 10, 20, 5)
  120. doc.lstext('hello', 10, 30, 10)
  121. comparePdf(doc.output(), 'letter-spacing.pdf', 'text')
  122. })
  123. })