canvas.test.js
3.76 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
144
145
146
147
const test = require('tap').test
const { Canvas, createCanvas } = require('canvas')
const QRCode = require('core/qrcode')
const CanvasRenderer = require('renderer/canvas')
test('CanvasRenderer interface', function (t) {
t.type(CanvasRenderer.render, 'function',
'Should have render function')
t.type(CanvasRenderer.renderToDataURL, 'function',
'Should have renderToDataURL function')
t.end()
})
test('CanvasRenderer render', function (t) {
// Mock document object
global.document = {
createElement: function (el) {
if (el === 'canvas') {
return createCanvas(200, 200)
}
}
}
const sampleQrData = QRCode.create('sample text', { version: 2 })
let canvasEl
t.notThrow(function () { canvasEl = CanvasRenderer.render(sampleQrData) },
'Should not throw if canvas is not provided')
t.ok(canvasEl instanceof Canvas,
'Should return a new canvas object')
t.notThrow(function () {
canvasEl = CanvasRenderer.render(sampleQrData, {
margin: 10,
scale: 1
})
}, 'Should not throw with options param')
// modules: 25, margins: 10 * 2, scale: 1
t.equal(canvasEl.width, 25 + 10 * 2,
'Should have correct size')
t.equal(canvasEl.width, canvasEl.height,
'Should be a square image')
global.document = undefined
t.throw(function () { canvasEl = CanvasRenderer.render(sampleQrData) },
'Should throw if canvas cannot be created')
t.end()
})
test('CanvasRenderer render to provided canvas', function (t) {
const sampleQrData = QRCode.create('sample text', { version: 2 })
const canvasEl = createCanvas(200, 200)
t.notThrow(function () { CanvasRenderer.render(sampleQrData, canvasEl) },
'Should not throw with only qrData and canvas param')
t.notThrow(function () {
CanvasRenderer.render(sampleQrData, canvasEl, {
margin: 10,
scale: 1
})
}, 'Should not throw with options param')
// modules: 25, margins: 10 * 2, scale: 1
t.equal(canvasEl.width, 25 + 10 * 2,
'Should have correct size')
t.equal(canvasEl.width, canvasEl.height,
'Should be a square image')
t.end()
})
test('CanvasRenderer renderToDataURL', function (t) {
// Mock document object
global.document = {
createElement: function (el) {
if (el === 'canvas') {
return createCanvas(200, 200)
}
}
}
const sampleQrData = QRCode.create('sample text', { version: 2 })
let url
t.notThrow(function () { url = CanvasRenderer.renderToDataURL(sampleQrData) },
'Should not throw if canvas is not provided')
t.notThrow(function () {
url = CanvasRenderer.renderToDataURL(sampleQrData, {
margin: 10,
scale: 1,
type: 'image/png'
})
}, 'Should not throw with options param')
t.type(url, 'string',
'Should return a string')
t.equal(url.split(',')[0], 'data:image/png;base64',
'Should have correct header')
const b64png = url.split(',')[1]
t.equal(b64png.length % 4, 0,
'Should have a correct length')
global.document = undefined
t.end()
})
test('CanvasRenderer renderToDataURL to provided canvas', function (t) {
const sampleQrData = QRCode.create('sample text', { version: 2 })
const canvasEl = createCanvas(200, 200)
let url
t.notThrow(function () {
url = CanvasRenderer.renderToDataURL(sampleQrData, canvasEl)
}, 'Should not throw with only qrData and canvas param')
t.notThrow(function () {
url = CanvasRenderer.renderToDataURL(sampleQrData, canvasEl, {
margin: 10,
scale: 1,
type: 'image/png'
})
}, 'Should not throw with options param')
t.type(url, 'string',
'Should return a string')
t.equal(url.split(',')[0], 'data:image/png;base64',
'Should have correct header')
const b64png = url.split(',')[1]
t.equal(b64png.length % 4, 0,
'Should have a correct length')
t.end()
})