codify-node
Version:
Generates 1D, 2D, and composite barcodes in png, svg, or eps formats.
82 lines (67 loc) • 2.09 kB
text/typescript
import { PNG } from 'pngjs'
import png from '../png'
describe('PNG Functions', () => {
afterEach(() => jest.resetAllMocks())
describe('blobToBase64()', () => {
it('should return a base-64 image string', async () => {
const image = new PNG({ width: 1, height: 1 })
const base64 = await png.blobToBase64(image)
expect(base64).toMatch(/^data:image\/png;base64,[a-z0-9\-+/]+=*$/i)
})
})
describe('getRgbaColor()', () => {
it('should parse the alpha channel for an 8-digit hex color', () => {
const color = png.getRgbaColor('ff00ff00')
expect(color).toEqual({
red: 255,
green: 0,
blue: 255,
alpha: 0
})
})
it('should parse a 6-digit hex color, with alpha channel falling back to 255', () => {
const color = png.getRgbaColor('ff00ff')
expect(color).toEqual({
red: 255,
green: 0,
blue: 255,
alpha: 255
})
})
it('should default to solid black if an invalid hex is passed in', () => {
const color = png.getRgbaColor('red')
expect(color).toEqual({
red: 0,
green: 0,
blue: 0,
alpha: 255
})
})
it('should default to solid black if no color is specified', () => {
const color = png.getRgbaColor()
expect(color).toEqual({
red: 0,
green: 0,
blue: 0,
alpha: 255
})
})
})
describe('render()', () => {
const bitmap = [
...Array(3).fill(0), // 1x1 black dot (left)
...Array(3).fill(255) // 1x1 white dot (right)
]
it('should fill in each pixel color from the given bitmap array', () => {
const result = png.render(bitmap, 1, 2, '00000000', 'ffffffff')
expect(result.data[0]).toEqual(0)
expect(result.data[1]).toEqual(0)
expect(result.data[2]).toEqual(0)
expect(result.data[3]).toEqual(0)
expect(result.data[4]).toEqual(255)
expect(result.data[5]).toEqual(255)
expect(result.data[6]).toEqual(255)
expect(result.data[7]).toEqual(255)
})
})
})