png.test.js
3.71 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
const test = require('tap').test
const sinon = require('sinon')
const fs = require('fs')
const QRCode = require('core/qrcode')
const PngRenderer = require('renderer/png')
const PNG = require('pngjs').PNG
const StreamMock = require('../../mocks/writable-stream')
test('PNG renderer interface', function (t) {
t.type(PngRenderer.render, 'function',
'Should have render function')
t.type(PngRenderer.renderToDataURL, 'function',
'Should have renderToDataURL function')
t.type(PngRenderer.renderToFile, 'function',
'Should have renderToFile function')
t.type(PngRenderer.renderToFileStream, 'function',
'Should have renderToFileStream function')
t.end()
})
test('PNG render', function (t) {
const sampleQrData = QRCode.create('sample text', { version: 2 })
let png
t.notThrow(function () { png = PngRenderer.render(sampleQrData) },
'Should not throw with only qrData param')
t.ok(png instanceof PNG,
'Should return an instance of PNG')
t.equal(png.width, png.height,
'Should be a square image')
// modules: 25, margins: 4 * 2, scale: 4
t.equal(png.width, (25 + 4 * 2) * 4,
'Should have correct size')
t.notThrow(function () {
png = PngRenderer.render(sampleQrData, {
margin: 10,
scale: 1
})
}, 'Should not throw with options param')
t.equal(png.width, png.height,
'Should be a square image')
// modules: 25, margins: 10 * 2, scale: 1
t.equal(png.width, 25 + 10 * 2,
'Should have correct size')
t.end()
})
test('PNG renderToDataURL', function (t) {
const sampleQrData = QRCode.create('sample text', { version: 2 })
t.plan(6)
PngRenderer.renderToDataURL(sampleQrData, function (err, url) {
t.ok(!err,
'Should not generate errors with only qrData param')
t.type(url, 'string',
'Should return a string')
})
PngRenderer.renderToDataURL(sampleQrData, { margin: 10, scale: 1 },
function (err, url) {
t.ok(!err, 'Should not generate errors 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')
}
)
})
test('PNG renderToFile', function (t) {
const sampleQrData = QRCode.create('sample text', { version: 2 })
const fileName = 'qrimage.png'
let fsStub = sinon.stub(fs, 'createWriteStream')
fsStub.returns(new StreamMock())
t.plan(5)
PngRenderer.renderToFile(fileName, sampleQrData, function (err) {
t.ok(!err,
'Should not generate errors with only qrData param')
t.equal(fsStub.getCall(0).args[0], fileName,
'Should save file with correct file name')
})
PngRenderer.renderToFile(fileName, sampleQrData, {
margin: 10,
scale: 1
}, function (err) {
t.ok(!err,
'Should not generate errors with options param')
t.equal(fsStub.getCall(0).args[0], fileName,
'Should save file with correct file name')
})
fsStub.restore()
fsStub = sinon.stub(fs, 'createWriteStream')
fsStub.returns(new StreamMock().forceErrorOnWrite())
PngRenderer.renderToFile(fileName, sampleQrData, function (err) {
t.ok(err,
'Should fail if error occurs during save')
})
fsStub.restore()
})
test('PNG renderToFileStream', function (t) {
const sampleQrData = QRCode.create('sample text', { version: 2 })
t.notThrow(function () {
PngRenderer.renderToFileStream(new StreamMock(), sampleQrData)
}, 'Should not throw with only qrData param')
t.notThrow(function () {
PngRenderer.renderToFileStream(new StreamMock(), sampleQrData, {
margin: 10,
scale: 1
})
}, 'Should not throw with options param')
t.end()
})