standard.spec.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. 'use strict'
  2. /* global describe, xit, it, jsPDF, comparePdf, jasmine, expect */
  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('jsPDF init options', () => {
  10. /**
  11. * @TODO: this document doesn't work, needs fixing, see #881
  12. */
  13. it('should make a compressed document', () => {
  14. const doc = jsPDF({
  15. compress: true
  16. })
  17. doc.text(10, 10, 'This is a test')
  18. doc.output()
  19. // comparePdf(doc.output(), 'compress.pdf', 'init')
  20. })
  21. // @TODO: Make sure this is what we want
  22. it('should silently fail compressing when adler32cs is not present', () => {
  23. delete window.adler32cs
  24. const doc = jsPDF({
  25. compress: true
  26. })
  27. doc.text(10, 10, 'This is a test')
  28. doc.output()
  29. })
  30. it('should make a landscape document', () => {
  31. const doc = jsPDF({
  32. orientation: 'landscape'
  33. })
  34. doc.text(10, 10, 'This is a test!')
  35. comparePdf(doc.output(), 'landscape.pdf', 'init')
  36. })
  37. it('should set document properties', () => {
  38. const doc = jsPDF()
  39. doc.setProperties({
  40. title: 'Title',
  41. subject: 'This is the subject',
  42. author: 'James Hall',
  43. keywords: 'generated, javascript, parallax',
  44. creator: 'jsPDF'
  45. })
  46. comparePdf(doc.output(), 'properties.pdf', 'init')
  47. })
  48. /**
  49. * @TODO: Fix 'undefined' see #882
  50. */
  51. it('should return font list', () => {
  52. const doc = jsPDF()
  53. const fontList = doc.getFontList()
  54. expect(fontList).toEqual({
  55. helvetica: ['normal', 'bold', 'italic', 'bolditalic'],
  56. Helvetica: ['', 'Bold', 'Oblique', 'BoldOblique'],
  57. courier: ['normal', 'bold', 'italic', 'bolditalic'],
  58. Courier: ['', 'Bold', 'Oblique', 'BoldOblique'],
  59. times: ['normal', 'bold', 'italic', 'bolditalic'],
  60. Times: ['Roman', 'Bold', 'Italic', 'BoldItalic'],
  61. zapfdingbats: ['undefined'],
  62. ZapfDingbats: ['']
  63. })
  64. })
  65. it('should return an ArrayBuffer', () => {
  66. const doc = jsPDF()
  67. expect(doc.output('arraybuffer')).toEqual(jasmine.any(ArrayBuffer))
  68. })
  69. it('should return a Blob', () => {
  70. const doc = jsPDF()
  71. expect(doc.output('blob')).toEqual(jasmine.any(window.Blob))
  72. })
  73. it('should return a bloburl', () => {
  74. const doc = jsPDF()
  75. expect(doc.output('bloburl')).toContain('blob:')
  76. expect(doc.output('bloburi')).toContain('blob:')
  77. })
  78. it('should return a datauri', () => {
  79. const doc = jsPDF()
  80. expect(doc.output('datauristring')).toContain('data:')
  81. expect(doc.output('dataurlstring')).toContain('data:')
  82. })
  83. // @TODO Figure out a way to test this
  84. xit('should return a datauri', () => {
  85. const doc = jsPDF()
  86. doc.output('datauri')
  87. window.stop()
  88. doc.output('dataurl')
  89. window.stop()
  90. })
  91. it('should open a new window', () => {
  92. if (navigator.userAgent.indexOf('Trident') !== -1) {
  93. console.warn('Skipping IE for new window test')
  94. return
  95. }
  96. const doc = jsPDF()
  97. doc.text(10, 10, 'This is a test')
  98. doc.output('dataurlnewwindow')
  99. // expect(doc.output('dataurlnewwindow').Window).toEqual(jasmine.any(Function))
  100. })
  101. const renderBoxes = (doc) => {
  102. for (let i = 0; i < 100; i++) {
  103. doc.rect(0, 0, i, i)
  104. }
  105. }
  106. it('should render text 100pt away from the top left', () => {
  107. const doc = jsPDF('portrait', 'pt')
  108. renderBoxes(doc)
  109. comparePdf(doc.output(), 'pt.pdf', 'init')
  110. })
  111. it('should render text 100pt away from the top left', () => {
  112. const doc = jsPDF('portrait', 'mm')
  113. renderBoxes(doc)
  114. comparePdf(doc.output(), 'mm.pdf', 'init')
  115. })
  116. it('should render text 100pt away from the top left', () => {
  117. const doc = jsPDF('portrait', 'cm')
  118. renderBoxes(doc)
  119. comparePdf(doc.output(), 'cm.pdf', 'init')
  120. })
  121. it('should render text 2in away from the top left', () => {
  122. const doc = jsPDF('portrait', 'in')
  123. renderBoxes(doc)
  124. comparePdf(doc.output(), 'in.pdf', 'init')
  125. })
  126. it('should render text 2px away from the top left', () => {
  127. const doc = jsPDF('portrait', 'px')
  128. renderBoxes(doc)
  129. comparePdf(doc.output(), 'px.pdf', 'init')
  130. })
  131. it('should render text 2px away from the top left', () => {
  132. const doc = jsPDF('portrait', 'pc')
  133. renderBoxes(doc)
  134. comparePdf(doc.output(), 'pc.pdf', 'init')
  135. })
  136. it('should render text 2px away from the top left with alternative syntax', () => {
  137. const doc = jsPDF({ unit: 'pc' })
  138. renderBoxes(doc)
  139. comparePdf(doc.output(), 'pc.pdf', 'init')
  140. })
  141. it('should render text 2em away from the top left with alternative syntax', () => {
  142. const doc = jsPDF({ unit: 'em' })
  143. renderBoxes(doc)
  144. comparePdf(doc.output(), 'em.pdf', 'init')
  145. })
  146. it('should render text 2ex away from the top left with alternative syntax', () => {
  147. const doc = jsPDF({ unit: 'ex' })
  148. renderBoxes(doc)
  149. comparePdf(doc.output(), 'ex.pdf', 'init')
  150. })
  151. it('should warn me about an invalid unit', () => {
  152. expect(() => {
  153. jsPDF({ unit: 'invalid' })
  154. }).toThrow('Invalid unit: invalid')
  155. })
  156. it('should warn me about an invalid unit when passed as second argument', () => {
  157. expect(() => {
  158. jsPDF('portrait', 'invalid')
  159. }).toThrow('Invalid unit: invalid')
  160. })
  161. })