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