isahaq-barcode 1.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/.eslintrc.js +29 -0
- package/README.md +549 -0
- package/bin/generate.js +274 -0
- package/eslint.config.js +68 -0
- package/examples/basic-usage.js +58 -0
- package/examples/batch-example.json +56 -0
- package/examples/express-server.js +159 -0
- package/index.js +134 -0
- package/jest.config.js +13 -0
- package/package.json +66 -0
- package/src/builders/QrCodeBuilder.js +401 -0
- package/src/renderers/HTMLRenderer.js +212 -0
- package/src/renderers/PDFRenderer.js +184 -0
- package/src/renderers/PNGRenderer.js +123 -0
- package/src/renderers/RenderFormats.js +87 -0
- package/src/renderers/SVGRenderer.js +221 -0
- package/src/services/BarcodeService.js +175 -0
- package/src/types/BarcodeTypes.js +195 -0
- package/src/validators/Validator.js +352 -0
- package/tests/BarcodeGenerator.test.js +187 -0
- package/tests/BarcodeService.test.js +76 -0
- package/tests/QrCodeBuilder.test.js +130 -0
- package/tests/setup.js +59 -0
|
@@ -0,0 +1,130 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
});
|