UNPKG

codify-node

Version:

Generates 1D, 2D, and composite barcodes in png, svg, or eps formats.

198 lines 9.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const EncodingMode_1 = __importDefault(require("../../types/enums/EncodingMode")); const OutputType_1 = __importDefault(require("../../types/enums/OutputType")); const OutputOption_1 = __importDefault(require("../../types/enums/OutputOption")); const binding_1 = __importDefault(require("../../binding")); const binary_1 = __importDefault(require("../binary")); describe('Binary methods', () => { afterEach(() => jest.resetAllMocks()); describe('createBuffer()', () => { beforeEach(() => { jest .spyOn(binding_1.default, 'createStream') .mockReturnValue({ message: 'Barcode created', code: 2, width: 32, height: 32, bitmap: [], encodedData: '' }); }); it('should call createStream() with the arguments provided by the config', () => { const symbol = { symbology: 10, height: 30, whitespaceWidth: 3, borderWidth: 5, outputOptions: OutputOption_1.default.SMALL_TEXT, foregroundColor: '00ff00ff', backgroundColor: 'ff0000ff', fileName: 'myfile.svg', scale: 1.3, option1: 1, option2: 2, option3: 3, showHumanReadableText: false, encoding: EncodingMode_1.default.DATA_MODE, eci: 0, primary: '', text: 'example text', rotation: 0, dotSize: 0.8 }; const barcodeData = 'primary data'; binary_1.default.createBuffer(symbol, barcodeData); expect(binding_1.default.createStream).toHaveBeenCalledWith(barcodeData, symbol.symbology, symbol.height, symbol.whitespaceWidth, symbol.borderWidth, symbol.outputOptions, symbol.backgroundColor, symbol.foregroundColor, symbol.fileName, symbol.scale, symbol.option1, symbol.option2, symbol.option3, 0, 'example text', symbol.encoding, symbol.eci, symbol.primary, symbol.rotation, symbol.dotSize); }); it('should fallback to default values for values missing in config', () => { const symbol = { symbology: 10, height: 30, whitespaceWidth: 3, borderWidth: 5, outputOptions: 2, foregroundColor: '00ff00ff', backgroundColor: 'ff0000ff', fileName: 'myfile.svg', scale: 1.3, option1: 1, option2: 2, option3: 3, showHumanReadableText: true, encoding: EncodingMode_1.default.DATA_MODE, eci: 0, primary: '', text: '', rotation: 0, dotSize: 0.8 }; const barcodeData = 'primary data'; binary_1.default.createBuffer(symbol, barcodeData); expect(binding_1.default.createStream).toHaveBeenCalledWith(barcodeData, symbol.symbology, symbol.height, symbol.whitespaceWidth, symbol.borderWidth, symbol.outputOptions, symbol.backgroundColor, symbol.foregroundColor, symbol.fileName, symbol.scale, symbol.option1, symbol.option2, symbol.option3, 1, barcodeData, symbol.encoding, symbol.eci, symbol.primary, symbol.rotation, symbol.dotSize); }); }); describe('invoke()', () => { describe('when symbol creation succeeds without warnings', () => { const result = { code: 0, message: 'Success', bitmap: [], encodedData: '<encoded data>', width: 10, height: 15 }; beforeEach(() => { jest .spyOn(binary_1.default, 'createBuffer') .mockReturnValue(result); }); it('should resolve with the resulting binary data when symbology is successfully rendered', async () => { expect.assertions(1); await expect(binary_1.default.invoke({ symbology: 10 }, '12345', OutputType_1.default.SVG)).resolves.toEqual({ ...result, message: 'Symbology successfully created.' }); }); it('should apply bitmap output option (8) to existing output options for non-PNG images', async () => { const config = { symbology: 10, outputOptions: OutputOption_1.default.BARCODE_BIND }; const barcodeData = '12345'; await binary_1.default.invoke(config, barcodeData, OutputType_1.default.SVG); expect(binary_1.default.createBuffer).toHaveBeenCalledTimes(1); expect(binary_1.default.createBuffer).toHaveBeenCalledWith({ ...config, fileName: 'out.svg', outputOptions: config.outputOptions + 8 }, barcodeData); }); it('should set the output options to bitmap output (8) to non-PNG images wth no previous output specified', async () => { const config = { symbology: 10, outputOptions: 0 }; const barcodeData = '12345'; await binary_1.default.invoke(config, barcodeData, OutputType_1.default.SVG); expect(binary_1.default.createBuffer).toHaveBeenCalledTimes(1); expect(binary_1.default.createBuffer).toHaveBeenCalledWith({ ...config, fileName: 'out.svg', outputOptions: 8 }, barcodeData); }); it('should not mutate the output options for PNG images', async () => { const config = { symbology: 10, outputOptions: OutputOption_1.default.BARCODE_BIND }; const barcodeData = '12345'; await binary_1.default.invoke(config, barcodeData, OutputType_1.default.PNG); expect(binary_1.default.createBuffer).toHaveBeenCalledTimes(1); expect(binary_1.default.createBuffer).toHaveBeenCalledWith({ ...config }, barcodeData); }); }); describe('when symbol creation succeeds with a warning', () => { it('should resolve if the status code is greater than 2', async () => { const result = { code: 1, message: 'Created with warnings', bitmap: [], encodedData: '<encoded data>', width: 10, height: 15 }; jest .spyOn(binary_1.default, 'createBuffer') .mockReturnValue(result); expect.assertions(1); await expect(binary_1.default.invoke({ symbology: 10 }, '12345', OutputType_1.default.SVG)).resolves.toEqual(result); }); }); describe('when the symbol creation fails', () => { it('should reject if the status code is greater than 2', async () => { const result = { code: 3, message: 'Failure', bitmap: [], encodedData: '<encoded data>', width: 10, height: 15 }; jest .spyOn(binary_1.default, 'createBuffer') .mockReturnValue(result); expect.assertions(1); await expect(binary_1.default.invoke({ symbology: 10 }, '12345', OutputType_1.default.SVG)).rejects.toEqual(result.message); }); }); }); describe('getOutputType()', () => { it('should return PNG for a file with a .png extension', () => { expect(binary_1.default.getOutputType('out.PNG')).toEqual(OutputType_1.default.PNG); }); it('should return SVG for a file with a .svg extension', () => { expect(binary_1.default.getOutputType('out.SVG')).toEqual(OutputType_1.default.SVG); }); it('should return EPS for a file with a .eps extension', () => { expect(binary_1.default.getOutputType('out.EPS')).toEqual(OutputType_1.default.EPS); }); it('should fall back to PNG for an unrecognized file extension', () => { expect(binary_1.default.getOutputType('out.tiff')).toEqual(OutputType_1.default.PNG); }); }); }); //# sourceMappingURL=binary.test.js.map