unexpected-image
Version:
Image metadata plugin for the Unexpected assertion library
131 lines (117 loc) • 5.04 kB
JavaScript
const unexpected = require('unexpected');
const pathModule = require('path');
const fs = require('fs');
describe('unexpected-image', () => {
unexpected.output.preferredWidth = 80;
const expect = unexpected
.clone()
.installPlugin(require('../lib/unexpectedImage'));
const turtleJpegPath = pathModule.resolve(
__dirname,
'..',
'testdata',
'turtle.jpg'
);
const turtleJpeg = fs.readFileSync(turtleJpegPath);
describe('with an image provided as a Buffer instance', () => {
it('should succeed', () =>
expect(turtleJpeg, 'to have metadata satisfying', {
format: 'JPEG',
size: {
width: 481,
},
}));
it('should fail with a diff', () =>
expect(
expect(turtleJpeg, 'to have metadata satisfying', {
format: 'JPEG',
size: {
width: 481,
height: 426,
},
Interlace: 'Yes',
Resolution: '96x96 pixels/inch',
}),
'when rejected',
'to have message',
/height: 424 \/\/ should equal 426/
));
});
describe('with an image provided as a Uint8Array instance', () => {
it('should succeed', () =>
expect(new Uint8Array(turtleJpeg), 'to have metadata satisfying', {
format: 'JPEG',
size: {
width: 481,
},
}));
it('should fail with a diff', () =>
expect(
expect(new Uint8Array(turtleJpeg), 'to have metadata satisfying', {
format: 'JPEG',
size: {
width: 481,
height: 426,
},
Interlace: 'Yes',
Resolution: '96x96 pixels/inch',
}),
'when rejected',
'to have message',
/height: 424 \/\/ should equal 426/
));
});
describe('with an image provided as a file name', () => {
it('should succeed', () =>
expect(turtleJpegPath, 'to have metadata satisfying', {
format: 'JPEG',
size: {
width: 481,
},
}));
it('should fail with a diff', () =>
expect(
expect(turtleJpegPath, 'to have metadata satisfying', {
format: 'JPEG',
size: {
width: 481,
height: 426,
},
Interlace: 'Yes',
Resolution: '96x96 pixels/inch',
}),
'when rejected',
'to have message',
/height: 424 \/\/ should equal 426/
));
});
it('should identify an image despite of a wrong extension', () =>
expect(
pathModule.resolve(__dirname, '..', 'testdata', 'reallyapng.jpg'),
'to have metadata satisfying',
{
format: 'PNG',
}
));
it('should fail with an error message if the file type is unsupported', () =>
expect(
expect(
pathModule.resolve(__dirname, '..', 'testdata', 'foo.data'),
'to have metadata satisfying',
{
format: 'PNG',
}
),
'to be rejected with',
/gm identify: No decode delegate for this image format/
));
it('should support identifying an image given by a data: url', () =>
expect(
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEgAACxIB0t1+/AAAA81JREFUOMttlF1Mm2UUx8/z8faTbqXty9pKKYU4YINF0S7EjZlF6Qg4gplEozNL2HRmJur02kTnx/WM0ahbdmP0xksdFuYNc6CDjc1OcHxtiLSFttC1peV92+fDi27oEs7VOcn/9z85yTmHSilhqxBCCCEQAMIYY7ylhv6/kEIgjAFASokfMFJKKSVCqNwGIbQFLKVEGG8qotHohfPnk8nU6ffeDQQCQohNr03+P5gxlkwmVVVVFGVycrL3cM9yPF7U9VuRyMBg2Gw2x+Nxzli1z/dQ57LrmQ8+/PzsZ2c++fitd94eHR1VDIbnenr8tbXhgYFIJGKvrOw/dkxw0X/8+GsnXy8jtDxeOn1vQ9NOvnkqmUrldd79fB+SknFGCHnl6MuNe1rDAz891X7A7fH8E4ttTk6FEISQmbk7t27eCHWGwj8PLizF6up9TfufHRm6SAg8feQoMSp7g3sLyaV8YaO36xAHYFwohFCNSSShyutteaYnzXmwq8/lclyaXj09rH3a3MqQciScudBpkgbHdXOzgSc2RMCXL5kI4oLTIpeMFSeujV2ueHJGMx+y56bHf/06EYiu5r63O3Quomv57/5ILKfXB+M2h9m+Fp5v3A4H612FIqeSKhO/jXg9nj0Gz/iVxY7Q7unU4mwiU1tpmkysI4AdFcrEUqbdXnBZHCUBT2zT2fz1jL8TcU5zBc3rr5emivGJeWoiIwtrKd3W3UgvR/W8MAkJKhIBhV3R3Qrl2Q2u+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