Skip to content

Commit ba7773e

Browse files
committed
test: test for alwaysKeepResolution, abort compress, getBrowserName,
1 parent 10b2faf commit ba7773e

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

test/index.spec.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import path from 'path';
33
import chai, { expect } from 'chai';
44
import chaiAsPromised from 'chai-as-promised';
55
import imageCompression from '../lib';
6+
import BROWSER_NAME from '../lib/config/browser-name';
7+
import MAX_CANVAS_SIZE from '../lib/config/max-canvas-size';
68

79
const {
810
drawImageInCanvas, getDataUrlFromFile, getFilefromDataUrl, loadImage, getExifOrientation, drawFileInCanvas,
@@ -23,7 +25,11 @@ chai.use(chaiAsPromised);
2325
describe('Tests', function () {
2426
this.timeout(30000);
2527

28+
const navigator = global.navigator;
29+
2630
beforeEach(() => {
31+
global.navigator = navigator;
32+
imageCompression.getBrowserName.cachedResult = undefined;
2733
});
2834

2935
it('get File from jpg base64', async () => {
@@ -204,6 +210,97 @@ describe('Tests', function () {
204210
expect(orientation).to.equal(-2);
205211
});
206212

213+
it('alwaysKeepResolution', async () => {
214+
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' });
215+
216+
const maxSizeMB = 1;
217+
218+
const compressedFile = await imageCompression(file, {
219+
maxSizeMB,
220+
useWebWorker: false,
221+
exifOrientation: -2,
222+
alwaysKeepResolution: true,
223+
});
224+
225+
const temp1 = await drawFileInCanvas(file);
226+
const img1 = temp1[0];
227+
const temp2 = await drawFileInCanvas(compressedFile);
228+
const img2 = temp2[0];
229+
expect(img2.width).to.equal(img1.width);
230+
expect(img2.height).to.equal(img1.height);
231+
});
232+
233+
it('abort compression', async () => {
234+
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' });
235+
236+
const maxSizeMB = 1;
237+
238+
const controller = new AbortController();
239+
240+
setTimeout(() => {
241+
controller.abort(new Error('I just want to stop'));
242+
}, 0);
243+
244+
expect(imageCompression(file, {
245+
maxSizeMB,
246+
useWebWorker: false,
247+
exifOrientation: -2,
248+
signal: controller.signal,
249+
})).to.eventually.rejectedWith(/I just want to stop/);
250+
});
251+
252+
it('getBrowserName Chrome', async () => {
253+
global.navigator = { userAgent: 'Chrome' };
254+
const browserName = imageCompression.getBrowserName();
255+
expect(browserName).to.equal(BROWSER_NAME.CHROME);
256+
});
257+
258+
it('getBrowserName iPhone', async () => {
259+
global.navigator = { userAgent: 'WebKit iPhone' };
260+
const browserName = imageCompression.getBrowserName();
261+
expect(browserName).to.equal(BROWSER_NAME.MOBILE_SAFARI);
262+
});
263+
264+
it('getBrowserName Safari', async () => {
265+
global.navigator = { userAgent: 'Safari' };
266+
const browserName = imageCompression.getBrowserName();
267+
expect(browserName).to.equal(BROWSER_NAME.DESKTOP_SAFARI);
268+
});
269+
270+
it('getBrowserName Firefox', async () => {
271+
global.navigator = { userAgent: 'Firefox' };
272+
const browserName = imageCompression.getBrowserName();
273+
expect(browserName).to.equal(BROWSER_NAME.FIREFOX);
274+
});
275+
276+
it('getBrowserName MSIE', async () => {
277+
global.navigator = { userAgent: 'MSIE' };
278+
const browserName = imageCompression.getBrowserName();
279+
expect(browserName).to.equal(BROWSER_NAME.IE);
280+
});
281+
282+
it('getBrowserName Other', async () => {
283+
global.navigator = { userAgent: 'Other' };
284+
const browserName = imageCompression.getBrowserName();
285+
expect(browserName).to.equal(BROWSER_NAME.ETC);
286+
});
287+
288+
it('approximateBelowMaximumCanvasSizeOfBrowser', async () => {
289+
global.navigator = { userAgent: 'Chrome' };
290+
const browserName = imageCompression.getBrowserName();
291+
const maximumCanvasSize = MAX_CANVAS_SIZE[browserName];
292+
const { width, height } = imageCompression.approximateBelowMaximumCanvasSizeOfBrowser(maximumCanvasSize * 1.2, maximumCanvasSize * 1.3);
293+
expect(width * height).to.be.lessThanOrEqual(maximumCanvasSize * maximumCanvasSize);
294+
});
295+
296+
it('approximateBelowMaximumCanvasSizeOfBrowser 2', async () => {
297+
global.navigator = { userAgent: 'Chrome' };
298+
const browserName = imageCompression.getBrowserName();
299+
const maximumCanvasSize = MAX_CANVAS_SIZE[browserName];
300+
const { width, height } = imageCompression.approximateBelowMaximumCanvasSizeOfBrowser(maximumCanvasSize * 1.3, maximumCanvasSize * 1.2);
301+
expect(width * height).to.be.lessThanOrEqual(maximumCanvasSize * maximumCanvasSize);
302+
});
303+
207304
afterEach(() => {
208305
});
209306
});

test/setup_global.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const pkg = require('../package.json');
22

33
global.process.env.NODE_ENV = 'test';
4-
global.process.env.BUILD = 'test';
4+
global.process.env.BUILD = 'development';
5+
global.console.log = () => {};
6+
global.console.error = () => {};
57
global.__buildDate__ = () => JSON.stringify(new Date());
68
global.__buildVersion__ = JSON.stringify(pkg.version);

0 commit comments

Comments
 (0)