Skip to content

Commit 449e2c9

Browse files
committed
test: bmp tests
1 parent b164e9b commit 449e2c9

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

example/sample_1280×853.bmp

3.12 MB
Binary file not shown.

test/index.spec.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ const EXIF_FILES = [
2727
.map((fileName, i) => fileName.replace('#', i % 9))
2828
.map((fileName) => path.join(IMAGE_DIR, 'Exif orientation examples', fileName))
2929
.map((filePath) => fs.readFileSync(filePath));
30+
const BMP_NAME = 'sample_1280×853.bmp';
31+
const BMP_PATH = path.join(IMAGE_DIR, BMP_NAME);
32+
const BMP_FILE = fs.readFileSync(BMP_PATH);
3033
const base64String = `data:image/jpeg;base64,${Buffer.from(JPG_FILE).toString('base64')}`;
3134
const base64String2 = `data:image/png;base64,${Buffer.from(PNG_FILE).toString('base64')}`;
35+
const base64String3 = `data:image/bmp;base64,${Buffer.from(BMP_FILE).toString('base64')}`;
3236

3337
chai.use(chaiAsPromised);
3438

@@ -68,6 +72,19 @@ describe('Tests', function () {
6872
expect(base64).to.equal(base64String2);
6973
});
7074

75+
it('get File from bmp base64', async () => {
76+
const file = await getFilefromDataUrl(base64String3, BMP_PATH);
77+
expect(file.type).to.equal('image/bmp');
78+
expect(file.size).to.equal(3275658);
79+
expect(file).to.be.an.instanceof(Blob);
80+
});
81+
82+
it('get base64 from bmp file', async () => {
83+
const file = new File([BMP_FILE], BMP_NAME, { type: 'image/bmp' });
84+
const base64 = await getDataUrlFromFile(file);
85+
expect(base64).to.equal(base64String3);
86+
});
87+
7188
it('load image', async () => {
7289
const img = await loadImage(base64String);
7390
expect(img).to.be.an.instanceof(Image);
@@ -187,6 +204,50 @@ describe('Tests', function () {
187204
expect(img.height).to.be.at.most(maxWidthOrHeight);
188205
});
189206

207+
it('compress bmp image file', async () => {
208+
const file = new File([BMP_FILE], BMP_NAME, { type: 'image/bmp' });
209+
210+
const maxSizeMB = 1;
211+
const maxSizeByte = maxSizeMB * 1024 * 1024;
212+
213+
const compressedFile = await imageCompression(file, { maxSizeMB, useWebWorker: false, exifOrientation: -2, maxIteration: 15 });
214+
expect(compressedFile.size).to.be.at.most(maxSizeByte);
215+
});
216+
217+
it('resize bmp image file', async () => {
218+
const file = new File([BMP_FILE], BMP_NAME, { type: 'image/bmp' });
219+
220+
const maxWidthOrHeight = 720;
221+
222+
const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 });
223+
224+
const temp = await drawFileInCanvas(compressedFile);
225+
const img = temp[0];
226+
expect(img.width).to.be.at.most(maxWidthOrHeight);
227+
expect(img.height).to.be.at.most(maxWidthOrHeight);
228+
});
229+
230+
it('compress and resize bmp image file', async () => {
231+
const file = new File([BMP_FILE], BMP_NAME, { type: 'image/bmp' });
232+
233+
const maxSizeMB = 1;
234+
const maxSizeByte = maxSizeMB * 1024 * 1024;
235+
const maxWidthOrHeight = 720;
236+
237+
const compressedFile = await imageCompression(file, {
238+
maxSizeMB,
239+
maxWidthOrHeight,
240+
useWebWorker: false,
241+
});
242+
243+
expect(compressedFile.size).to.be.at.most(maxSizeByte);
244+
245+
const temp = await drawFileInCanvas(compressedFile);
246+
const img = temp[0];
247+
expect(img.width).to.be.at.most(maxWidthOrHeight);
248+
expect(img.height).to.be.at.most(maxWidthOrHeight);
249+
});
250+
190251
it('fails if wrong file provided', async () => {
191252
const file = undefined;
192253

0 commit comments

Comments
 (0)