isahaq-barcode 1.8.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +107 -0
- package/LICENSE +21 -0
- package/README.md +319 -519
- package/bin/generate.js +171 -134
- package/browser.js +163 -170
- package/index.d.ts +497 -0
- package/index.js +118 -38
- package/package.json +68 -34
- package/src/builders/QrCodeBuilder.js +328 -165
- package/src/encoders/BarcodeEncoder.js +260 -0
- package/src/renderers/HTMLRenderer.js +36 -169
- package/src/renderers/PDFRenderer.js +108 -130
- package/src/renderers/PNGRenderer.js +7 -83
- package/src/renderers/RenderFormats.js +89 -38
- package/src/renderers/SVGRenderer.js +8 -182
- package/src/services/BarcodeService.js +96 -36
- package/src/types/BarcodeTypes.js +535 -144
- package/src/validators/Validator.js +140 -122
- package/.eslintrc.js +0 -29
- package/.prettierignore +0 -11
- package/.prettierrc +0 -11
- package/eslint.config.js +0 -67
- package/examples/basic-usage.js +0 -58
- package/examples/batch-example.json +0 -56
- package/examples/express-server.js +0 -163
- package/jest.config.js +0 -13
- package/tests/BarcodeGenerator.test.js +0 -187
- package/tests/BarcodeService.test.js +0 -76
- package/tests/QrCodeBuilder.test.js +0 -130
- package/tests/setup.js +0 -59
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Express.js server example for Isahaq Barcode Generator
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const express = require('express');
|
|
6
|
-
const BarcodeGenerator = require('../index');
|
|
7
|
-
|
|
8
|
-
const app = express();
|
|
9
|
-
const PORT = process.env.PORT || 3000;
|
|
10
|
-
|
|
11
|
-
// Middleware
|
|
12
|
-
app.use(express.json());
|
|
13
|
-
app.use(express.static('public'));
|
|
14
|
-
|
|
15
|
-
// Barcode generation endpoint
|
|
16
|
-
app.get('/barcode/:data', async (req, res) => {
|
|
17
|
-
const { data } = req.params;
|
|
18
|
-
const { type = 'code128', format = 'png', width, height } = req.query;
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
const options = {};
|
|
22
|
-
if (width) options.width = parseInt(width);
|
|
23
|
-
if (height) options.height = parseInt(height);
|
|
24
|
-
|
|
25
|
-
let result;
|
|
26
|
-
let contentType;
|
|
27
|
-
|
|
28
|
-
switch (format) {
|
|
29
|
-
case 'png':
|
|
30
|
-
result = BarcodeGenerator.png(data, type, options);
|
|
31
|
-
contentType = 'image/png';
|
|
32
|
-
break;
|
|
33
|
-
case 'svg':
|
|
34
|
-
result = BarcodeGenerator.svg(data, type, options);
|
|
35
|
-
contentType = 'image/svg+xml';
|
|
36
|
-
break;
|
|
37
|
-
case 'html':
|
|
38
|
-
result = BarcodeGenerator.html(data, type, options);
|
|
39
|
-
contentType = 'text/html';
|
|
40
|
-
break;
|
|
41
|
-
case 'pdf':
|
|
42
|
-
result = await BarcodeGenerator.pdf(data, type, options);
|
|
43
|
-
contentType = 'application/pdf';
|
|
44
|
-
break;
|
|
45
|
-
default:
|
|
46
|
-
throw new Error(`Unsupported format: ${format}`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
res.set('Content-Type', contentType);
|
|
50
|
-
res.send(result);
|
|
51
|
-
} catch (error) {
|
|
52
|
-
res.status(400).json({ error: error.message });
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
// QR code generation endpoint
|
|
57
|
-
app.get('/qr/:data', async (req, res) => {
|
|
58
|
-
const { data } = req.params;
|
|
59
|
-
const {
|
|
60
|
-
size = 300,
|
|
61
|
-
logo,
|
|
62
|
-
label,
|
|
63
|
-
watermark,
|
|
64
|
-
watermarkPosition = 'center',
|
|
65
|
-
errorCorrection = 'M',
|
|
66
|
-
} = req.query;
|
|
67
|
-
|
|
68
|
-
try {
|
|
69
|
-
const qrOptions = {
|
|
70
|
-
data: decodeURIComponent(data),
|
|
71
|
-
size: parseInt(size),
|
|
72
|
-
errorCorrectionLevel: errorCorrection,
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
if (logo) qrOptions.logoPath = logo;
|
|
76
|
-
if (label) qrOptions.label = label;
|
|
77
|
-
if (watermark) {
|
|
78
|
-
qrOptions.watermark = watermark;
|
|
79
|
-
qrOptions.watermarkPosition = watermarkPosition;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const qrCode = BarcodeGenerator.modernQr(qrOptions);
|
|
83
|
-
const result = await qrCode.generate();
|
|
84
|
-
|
|
85
|
-
res.set('Content-Type', 'image/png');
|
|
86
|
-
res.send(result);
|
|
87
|
-
} catch (error) {
|
|
88
|
-
res.status(400).json({ error: error.message });
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// Batch generation endpoint
|
|
93
|
-
app.post('/batch', async (req, res) => {
|
|
94
|
-
try {
|
|
95
|
-
const { items } = req.body;
|
|
96
|
-
|
|
97
|
-
if (!Array.isArray(items)) {
|
|
98
|
-
throw new Error('Items must be an array');
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const results = BarcodeGenerator.batch(items);
|
|
102
|
-
res.json({ results });
|
|
103
|
-
} catch (error) {
|
|
104
|
-
res.status(400).json({ error: error.message });
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
// Validation endpoint
|
|
109
|
-
app.get('/validate/:data/:type', (req, res) => {
|
|
110
|
-
const { data, type } = req.params;
|
|
111
|
-
|
|
112
|
-
try {
|
|
113
|
-
const validation = BarcodeGenerator.validate(data, type);
|
|
114
|
-
res.json(validation);
|
|
115
|
-
} catch (error) {
|
|
116
|
-
res.status(400).json({ error: error.message });
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// Get supported types
|
|
121
|
-
app.get('/types', (req, res) => {
|
|
122
|
-
const types = BarcodeGenerator.getBarcodeTypes();
|
|
123
|
-
res.json({ types });
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
// Get supported formats
|
|
127
|
-
app.get('/formats', (req, res) => {
|
|
128
|
-
const formats = BarcodeGenerator.getRenderFormats();
|
|
129
|
-
res.json({ formats });
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
// Get watermark positions
|
|
133
|
-
app.get('/watermark-positions', (req, res) => {
|
|
134
|
-
const positions = BarcodeGenerator.getWatermarkPositions();
|
|
135
|
-
res.json({ positions });
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
// Health check
|
|
139
|
-
app.get('/health', (req, res) => {
|
|
140
|
-
res.json({ status: 'OK', timestamp: new Date().toISOString() });
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
// Error handling middleware
|
|
144
|
-
app.use((error, req, res) => {
|
|
145
|
-
console.error('Error:', error);
|
|
146
|
-
res.status(500).json({ error: 'Internal server error' });
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
// Start server
|
|
150
|
-
app.listen(PORT, () => {
|
|
151
|
-
console.log(`🚀 Server running on http://localhost:${PORT}`);
|
|
152
|
-
console.log(`📊 Health check: http://localhost:${PORT}/health`);
|
|
153
|
-
console.log(
|
|
154
|
-
`🔗 Barcode endpoint: http://localhost:${PORT}/barcode/{data}?type=code128&format=png`
|
|
155
|
-
);
|
|
156
|
-
console.log(
|
|
157
|
-
`🔗 QR code endpoint: http://localhost:${PORT}/qr/{data}?size=300`
|
|
158
|
-
);
|
|
159
|
-
console.log(`🔗 Types endpoint: http://localhost:${PORT}/types`);
|
|
160
|
-
console.log(`🔗 Formats endpoint: http://localhost:${PORT}/formats`);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
module.exports = app;
|
package/jest.config.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
testEnvironment: 'node',
|
|
3
|
-
testMatch: ['**/__tests__/**/*.js', '**/?(*.)+(spec|test).js'],
|
|
4
|
-
collectCoverageFrom: [
|
|
5
|
-
'src/**/*.js',
|
|
6
|
-
'!src/**/*.test.js',
|
|
7
|
-
'!src/**/__tests__/**',
|
|
8
|
-
],
|
|
9
|
-
coverageDirectory: 'coverage',
|
|
10
|
-
coverageReporters: ['text', 'lcov', 'html'],
|
|
11
|
-
setupFilesAfterEnv: ['<rootDir>/tests/setup.js'],
|
|
12
|
-
testTimeout: 10000,
|
|
13
|
-
};
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for BarcodeGenerator
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const BarcodeGenerator = require('../index');
|
|
6
|
-
|
|
7
|
-
describe('BarcodeGenerator', () => {
|
|
8
|
-
describe('PNG Generation', () => {
|
|
9
|
-
test('should generate PNG barcode', () => {
|
|
10
|
-
const result = BarcodeGenerator.png('1234567890', 'code128');
|
|
11
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
test('should generate PNG barcode with options', () => {
|
|
15
|
-
const options = {
|
|
16
|
-
width: 3,
|
|
17
|
-
height: 150,
|
|
18
|
-
displayValue: false,
|
|
19
|
-
};
|
|
20
|
-
const result = BarcodeGenerator.png('1234567890', 'code128', options);
|
|
21
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test('should throw error for invalid data', () => {
|
|
25
|
-
expect(() => {
|
|
26
|
-
BarcodeGenerator.png('', 'code128');
|
|
27
|
-
}).toThrow('Data must be a non-empty string');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('should throw error for invalid barcode type', () => {
|
|
31
|
-
expect(() => {
|
|
32
|
-
BarcodeGenerator.png('1234567890', 'invalid-type');
|
|
33
|
-
}).toThrow('Invalid barcode type');
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
describe('SVG Generation', () => {
|
|
38
|
-
test('should generate SVG barcode', () => {
|
|
39
|
-
const result = BarcodeGenerator.svg('1234567890', 'code128');
|
|
40
|
-
expect(typeof result).toBe('string');
|
|
41
|
-
expect(result).toContain('<svg');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test('should generate SVG barcode with options', () => {
|
|
45
|
-
const options = {
|
|
46
|
-
width: 3,
|
|
47
|
-
height: 150,
|
|
48
|
-
displayValue: false,
|
|
49
|
-
};
|
|
50
|
-
const result = BarcodeGenerator.svg('1234567890', 'code128', options);
|
|
51
|
-
expect(typeof result).toBe('string');
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('HTML Generation', () => {
|
|
56
|
-
test('should generate HTML barcode', () => {
|
|
57
|
-
const result = BarcodeGenerator.html('1234567890', 'code128');
|
|
58
|
-
expect(typeof result).toBe('string');
|
|
59
|
-
expect(result).toContain('<div');
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test('should generate HTML barcode with options', () => {
|
|
63
|
-
const options = {
|
|
64
|
-
width: 3,
|
|
65
|
-
height: 150,
|
|
66
|
-
displayValue: false,
|
|
67
|
-
};
|
|
68
|
-
const result = BarcodeGenerator.html('1234567890', 'code128', options);
|
|
69
|
-
expect(typeof result).toBe('string');
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
describe('PDF Generation', () => {
|
|
74
|
-
test('should generate PDF barcode', async () => {
|
|
75
|
-
const result = await BarcodeGenerator.pdf('1234567890', 'code128');
|
|
76
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test('should generate PDF barcode with options', async () => {
|
|
80
|
-
const options = {
|
|
81
|
-
width: 3,
|
|
82
|
-
height: 150,
|
|
83
|
-
displayValue: false,
|
|
84
|
-
};
|
|
85
|
-
const result = await BarcodeGenerator.pdf(
|
|
86
|
-
'1234567890',
|
|
87
|
-
'code128',
|
|
88
|
-
options
|
|
89
|
-
);
|
|
90
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
describe('QR Code Generation', () => {
|
|
95
|
-
test('should generate QR code', () => {
|
|
96
|
-
const result = BarcodeGenerator.modernQr({
|
|
97
|
-
data: 'https://example.com',
|
|
98
|
-
size: 300,
|
|
99
|
-
});
|
|
100
|
-
expect(result).toBeDefined();
|
|
101
|
-
expect(typeof result.generate).toBe('function');
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
test('should generate QR code with logo', () => {
|
|
105
|
-
const result = BarcodeGenerator.modernQr({
|
|
106
|
-
data: 'https://example.com',
|
|
107
|
-
size: 300,
|
|
108
|
-
logoPath: 'path/to/logo.png',
|
|
109
|
-
logoSize: 60,
|
|
110
|
-
});
|
|
111
|
-
expect(result).toBeDefined();
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test('should generate QR code with watermark', () => {
|
|
115
|
-
const result = BarcodeGenerator.modernQr({
|
|
116
|
-
data: 'https://example.com',
|
|
117
|
-
size: 300,
|
|
118
|
-
watermark: 'Watermark Text',
|
|
119
|
-
watermarkPosition: 'center',
|
|
120
|
-
});
|
|
121
|
-
expect(result).toBeDefined();
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
describe('Validation', () => {
|
|
126
|
-
test('should validate data for Code 128', () => {
|
|
127
|
-
const validation = BarcodeGenerator.validate('1234567890', 'code128');
|
|
128
|
-
expect(validation.valid).toBe(true);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
test('should validate data for EAN-13', () => {
|
|
132
|
-
const validation = BarcodeGenerator.validate('1234567890123', 'ean13');
|
|
133
|
-
expect(validation.valid).toBe(true);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
test('should reject invalid data for EAN-13', () => {
|
|
137
|
-
const validation = BarcodeGenerator.validate('123', 'ean13');
|
|
138
|
-
expect(validation.valid).toBe(false);
|
|
139
|
-
expect(validation.error).toContain('too short');
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
describe('Batch Generation', () => {
|
|
144
|
-
test('should generate multiple barcodes', () => {
|
|
145
|
-
const items = [
|
|
146
|
-
{ data: '1234567890', type: 'code128', format: 'png' },
|
|
147
|
-
{ data: '9876543210', type: 'code39', format: 'svg' },
|
|
148
|
-
];
|
|
149
|
-
const results = BarcodeGenerator.batch(items);
|
|
150
|
-
expect(Array.isArray(results)).toBe(true);
|
|
151
|
-
expect(results.length).toBe(2);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
test('should handle batch generation errors', () => {
|
|
155
|
-
const items = [
|
|
156
|
-
{ data: '1234567890', type: 'code128', format: 'png' },
|
|
157
|
-
{ data: '', type: 'code128', format: 'png' }, // Invalid data
|
|
158
|
-
];
|
|
159
|
-
const results = BarcodeGenerator.batch(items);
|
|
160
|
-
expect(results[0].success).toBe(true);
|
|
161
|
-
expect(results[1].success).toBe(false);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
describe('Utility Methods', () => {
|
|
166
|
-
test('should get barcode types', () => {
|
|
167
|
-
const types = BarcodeGenerator.getBarcodeTypes();
|
|
168
|
-
expect(Array.isArray(types)).toBe(true);
|
|
169
|
-
expect(types).toContain('code128');
|
|
170
|
-
expect(types).toContain('ean13');
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
test('should get render formats', () => {
|
|
174
|
-
const formats = BarcodeGenerator.getRenderFormats();
|
|
175
|
-
expect(Array.isArray(formats)).toBe(true);
|
|
176
|
-
expect(formats).toContain('png');
|
|
177
|
-
expect(formats).toContain('svg');
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
test('should get watermark positions', () => {
|
|
181
|
-
const positions = BarcodeGenerator.getWatermarkPositions();
|
|
182
|
-
expect(Array.isArray(positions)).toBe(true);
|
|
183
|
-
expect(positions).toContain('center');
|
|
184
|
-
expect(positions).toContain('top-left');
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
});
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for BarcodeService
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const BarcodeService = require('../src/services/BarcodeService');
|
|
6
|
-
|
|
7
|
-
describe('BarcodeService', () => {
|
|
8
|
-
let service;
|
|
9
|
-
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
service = new BarcodeService();
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
describe('PNG Generation', () => {
|
|
15
|
-
test('should generate PNG barcode', () => {
|
|
16
|
-
const result = service.png('1234567890', 'code128');
|
|
17
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('should generate PNG barcode with options', () => {
|
|
21
|
-
const options = {
|
|
22
|
-
width: 3,
|
|
23
|
-
height: 150,
|
|
24
|
-
displayValue: false,
|
|
25
|
-
};
|
|
26
|
-
const result = service.png('1234567890', 'code128', options);
|
|
27
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe('SVG Generation', () => {
|
|
32
|
-
test('should generate SVG barcode', () => {
|
|
33
|
-
const result = service.svg('1234567890', 'code128');
|
|
34
|
-
expect(typeof result).toBe('string');
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
describe('HTML Generation', () => {
|
|
39
|
-
test('should generate HTML barcode', () => {
|
|
40
|
-
const result = service.html('1234567890', 'code128');
|
|
41
|
-
expect(typeof result).toBe('string');
|
|
42
|
-
expect(result).toContain('<div');
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe('PDF Generation', () => {
|
|
47
|
-
test('should generate PDF barcode', async () => {
|
|
48
|
-
const result = await service.pdf('1234567890', 'code128');
|
|
49
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
describe('Validation', () => {
|
|
54
|
-
test('should validate data', () => {
|
|
55
|
-
const validation = service.validate('1234567890', 'code128');
|
|
56
|
-
expect(validation.valid).toBe(true);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
test('should reject invalid data', () => {
|
|
60
|
-
const validation = service.validate('', 'code128');
|
|
61
|
-
expect(validation.valid).toBe(false);
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
describe('Batch Generation', () => {
|
|
66
|
-
test('should generate multiple barcodes', () => {
|
|
67
|
-
const items = [
|
|
68
|
-
{ data: '1234567890', type: 'code128', format: 'png' },
|
|
69
|
-
{ data: '9876543210', type: 'code39', format: 'svg' },
|
|
70
|
-
];
|
|
71
|
-
const results = service.batch(items);
|
|
72
|
-
expect(Array.isArray(results)).toBe(true);
|
|
73
|
-
expect(results.length).toBe(2);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
});
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for QrCodeBuilder
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const QrCodeBuilder = require('../src/builders/QrCodeBuilder');
|
|
6
|
-
|
|
7
|
-
describe('QrCodeBuilder', () => {
|
|
8
|
-
describe('Builder Pattern', () => {
|
|
9
|
-
test('should create builder instance', () => {
|
|
10
|
-
const builder = QrCodeBuilder.create();
|
|
11
|
-
expect(builder).toBeDefined();
|
|
12
|
-
expect(typeof builder.data).toBe('function');
|
|
13
|
-
expect(typeof builder.size).toBe('function');
|
|
14
|
-
expect(typeof builder.build).toBe('function');
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test('should chain methods', () => {
|
|
18
|
-
const builder = QrCodeBuilder.create()
|
|
19
|
-
.data('https://example.com')
|
|
20
|
-
.size(300)
|
|
21
|
-
.margin(10)
|
|
22
|
-
.foregroundColor([0, 0, 0])
|
|
23
|
-
.backgroundColor([255, 255, 255])
|
|
24
|
-
.build();
|
|
25
|
-
|
|
26
|
-
expect(builder).toBeDefined();
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe('QR Code Generation', () => {
|
|
31
|
-
test('should generate QR code', async () => {
|
|
32
|
-
const qrCode = QrCodeBuilder.create()
|
|
33
|
-
.data('https://example.com')
|
|
34
|
-
.size(300)
|
|
35
|
-
.build();
|
|
36
|
-
|
|
37
|
-
const result = await qrCode.generate();
|
|
38
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('should generate QR code with logo', async () => {
|
|
42
|
-
const qrCode = QrCodeBuilder.create()
|
|
43
|
-
.data('https://example.com')
|
|
44
|
-
.size(300)
|
|
45
|
-
.logoPath('path/to/logo.png')
|
|
46
|
-
.logoSize(60)
|
|
47
|
-
.build();
|
|
48
|
-
|
|
49
|
-
const result = await qrCode.generate();
|
|
50
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test('should generate QR code with watermark', async () => {
|
|
54
|
-
const qrCode = QrCodeBuilder.create()
|
|
55
|
-
.data('https://example.com')
|
|
56
|
-
.size(300)
|
|
57
|
-
.watermark('Watermark Text', 'center')
|
|
58
|
-
.build();
|
|
59
|
-
|
|
60
|
-
const result = await qrCode.generate();
|
|
61
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test('should generate QR code with label', async () => {
|
|
65
|
-
const qrCode = QrCodeBuilder.create()
|
|
66
|
-
.data('https://example.com')
|
|
67
|
-
.size(300)
|
|
68
|
-
.label('Scan me!')
|
|
69
|
-
.build();
|
|
70
|
-
|
|
71
|
-
const result = await qrCode.generate();
|
|
72
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
describe('Output Methods', () => {
|
|
77
|
-
test('should save to file', async () => {
|
|
78
|
-
const qrCode = QrCodeBuilder.create()
|
|
79
|
-
.data('https://example.com')
|
|
80
|
-
.size(300)
|
|
81
|
-
.build();
|
|
82
|
-
|
|
83
|
-
await expect(qrCode.saveToFile('test-qr.png')).resolves.not.toThrow();
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test('should get data URI', async () => {
|
|
87
|
-
const qrCode = QrCodeBuilder.create()
|
|
88
|
-
.data('https://example.com')
|
|
89
|
-
.size(300)
|
|
90
|
-
.build();
|
|
91
|
-
|
|
92
|
-
const dataUri = await qrCode.getDataUri();
|
|
93
|
-
expect(typeof dataUri).toBe('string');
|
|
94
|
-
expect(dataUri).toContain('data:image/');
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('should get string', async () => {
|
|
98
|
-
const qrCode = QrCodeBuilder.create()
|
|
99
|
-
.data('https://example.com')
|
|
100
|
-
.size(300)
|
|
101
|
-
.build();
|
|
102
|
-
|
|
103
|
-
const result = await qrCode.getString();
|
|
104
|
-
expect(Buffer.isBuffer(result)).toBe(true);
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
describe('Options', () => {
|
|
109
|
-
test('should set all options', () => {
|
|
110
|
-
const builder = QrCodeBuilder.create({
|
|
111
|
-
data: 'https://example.com',
|
|
112
|
-
size: 300,
|
|
113
|
-
margin: 10,
|
|
114
|
-
errorCorrectionLevel: 'H',
|
|
115
|
-
foregroundColor: [0, 0, 0],
|
|
116
|
-
backgroundColor: [255, 255, 255],
|
|
117
|
-
logoPath: 'path/to/logo.png',
|
|
118
|
-
logoSize: 60,
|
|
119
|
-
label: 'Scan me!',
|
|
120
|
-
watermark: 'Watermark',
|
|
121
|
-
watermarkPosition: 'center',
|
|
122
|
-
format: 'png',
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
expect(builder.options.data).toBe('https://example.com');
|
|
126
|
-
expect(builder.options.size).toBe(300);
|
|
127
|
-
expect(builder.options.margin).toBe(10);
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
});
|
package/tests/setup.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Jest setup file
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// Mock canvas for testing
|
|
6
|
-
jest.mock('canvas', () => ({
|
|
7
|
-
createCanvas: jest.fn(() => ({
|
|
8
|
-
getContext: jest.fn(() => ({
|
|
9
|
-
fillStyle: '',
|
|
10
|
-
fillRect: jest.fn(),
|
|
11
|
-
drawImage: jest.fn(),
|
|
12
|
-
font: '',
|
|
13
|
-
fillColor: '',
|
|
14
|
-
text: jest.fn(),
|
|
15
|
-
rect: jest.fn(),
|
|
16
|
-
fill: jest.fn(),
|
|
17
|
-
})),
|
|
18
|
-
toBuffer: jest.fn(() => Buffer.from('mock-png-data')),
|
|
19
|
-
toSVG: jest.fn(() => '<svg>mock</svg>'),
|
|
20
|
-
})),
|
|
21
|
-
loadImage: jest.fn(() =>
|
|
22
|
-
Promise.resolve({
|
|
23
|
-
width: 100,
|
|
24
|
-
height: 100,
|
|
25
|
-
})
|
|
26
|
-
),
|
|
27
|
-
}));
|
|
28
|
-
|
|
29
|
-
// Mock fs.promises
|
|
30
|
-
jest.mock('fs', () => ({
|
|
31
|
-
promises: {
|
|
32
|
-
writeFile: jest.fn(() => Promise.resolve()),
|
|
33
|
-
readFile: jest.fn(() => Promise.resolve('{"test": "data"}')),
|
|
34
|
-
mkdir: jest.fn(() => Promise.resolve()),
|
|
35
|
-
},
|
|
36
|
-
}));
|
|
37
|
-
|
|
38
|
-
// Mock JsBarcode
|
|
39
|
-
jest.mock('jsbarcode', () => jest.fn());
|
|
40
|
-
|
|
41
|
-
// Mock QRCode
|
|
42
|
-
jest.mock('qrcode', () => ({
|
|
43
|
-
toDataURL: jest.fn(() =>
|
|
44
|
-
Promise.resolve('data:image/png;base64,mock-qr-data')
|
|
45
|
-
),
|
|
46
|
-
}));
|
|
47
|
-
|
|
48
|
-
// Mock PDFDocument
|
|
49
|
-
jest.mock('pdfkit', () => {
|
|
50
|
-
return jest.fn().mockImplementation(() => ({
|
|
51
|
-
rect: jest.fn().mockReturnThis(),
|
|
52
|
-
fillColor: jest.fn().mockReturnThis(),
|
|
53
|
-
fill: jest.fn().mockReturnThis(),
|
|
54
|
-
fontSize: jest.fn().mockReturnThis(),
|
|
55
|
-
text: jest.fn().mockReturnThis(),
|
|
56
|
-
on: jest.fn().mockReturnThis(),
|
|
57
|
-
end: jest.fn(),
|
|
58
|
-
}));
|
|
59
|
-
});
|