@@ -3,6 +3,8 @@ import path from 'path';
3
3
import chai , { expect } from 'chai' ;
4
4
import chaiAsPromised from 'chai-as-promised' ;
5
5
import imageCompression from '../lib' ;
6
+ import BROWSER_NAME from '../lib/config/browser-name' ;
7
+ import MAX_CANVAS_SIZE from '../lib/config/max-canvas-size' ;
6
8
7
9
const {
8
10
drawImageInCanvas, getDataUrlFromFile, getFilefromDataUrl, loadImage, getExifOrientation, drawFileInCanvas,
@@ -23,7 +25,11 @@ chai.use(chaiAsPromised);
23
25
describe ( 'Tests' , function ( ) {
24
26
this . timeout ( 30000 ) ;
25
27
28
+ const navigator = global . navigator ;
29
+
26
30
beforeEach ( ( ) => {
31
+ global . navigator = navigator ;
32
+ imageCompression . getBrowserName . cachedResult = undefined ;
27
33
} ) ;
28
34
29
35
it ( 'get File from jpg base64' , async ( ) => {
@@ -204,6 +210,97 @@ describe('Tests', function () {
204
210
expect ( orientation ) . to . equal ( - 2 ) ;
205
211
} ) ;
206
212
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 j u s t w a n t t o s t o p / ) ;
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
+
207
304
afterEach ( ( ) => {
208
305
} ) ;
209
306
} ) ;
0 commit comments