Skip to content

Commit 0a91392

Browse files
committed
style: format code
1 parent b41fbff commit 0a91392

File tree

2 files changed

+141
-143
lines changed

2 files changed

+141
-143
lines changed

test/index.spec.js

Lines changed: 132 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,212 +1,209 @@
1-
import fs from 'fs'
2-
import path from 'path'
3-
import chai, { expect } from 'chai'
4-
import chaiAsPromised from 'chai-as-promised'
5-
import imageCompression from '../lib'
6-
7-
const { drawImageInCanvas, getDataUrlFromFile, getFilefromDataUrl, loadImage, getExifOrientation, drawFileInCanvas } = imageCompression
8-
9-
const IMAGE_DIR = './example'
10-
const JPG_NAME = '178440.jpg'
11-
const JPG_PATH = path.join(IMAGE_DIR, JPG_NAME)
12-
const JPG_FILE = fs.readFileSync(JPG_PATH)
13-
const PNG_NAME = 'sonic.png'
14-
const PNG_PATH = path.join(IMAGE_DIR, PNG_NAME)
15-
const PNG_FILE = fs.readFileSync(PNG_PATH)
16-
const base64String = 'data:image/jpeg;base64,' + Buffer.from(JPG_FILE).toString('base64')
17-
const base64String2 = 'data:image/png;base64,' + Buffer.from(PNG_FILE).toString('base64');
18-
19-
chai.use(chaiAsPromised)
1+
import fs from 'fs';
2+
import path from 'path';
3+
import chai, { expect } from 'chai';
4+
import chaiAsPromised from 'chai-as-promised';
5+
import imageCompression from '../lib';
6+
7+
const {
8+
drawImageInCanvas, getDataUrlFromFile, getFilefromDataUrl, loadImage, getExifOrientation, drawFileInCanvas,
9+
} = imageCompression;
10+
11+
const IMAGE_DIR = './example';
12+
const JPG_NAME = '178440.jpg';
13+
const JPG_PATH = path.join(IMAGE_DIR, JPG_NAME);
14+
const JPG_FILE = fs.readFileSync(JPG_PATH);
15+
const PNG_NAME = 'sonic.png';
16+
const PNG_PATH = path.join(IMAGE_DIR, PNG_NAME);
17+
const PNG_FILE = fs.readFileSync(PNG_PATH);
18+
const base64String = `data:image/jpeg;base64,${Buffer.from(JPG_FILE).toString('base64')}`;
19+
const base64String2 = `data:image/png;base64,${Buffer.from(PNG_FILE).toString('base64')}`;
20+
21+
chai.use(chaiAsPromised);
2022

2123
describe('Tests', function () {
22-
this.timeout(30000)
24+
this.timeout(30000);
2325

2426
beforeEach(() => {
25-
})
27+
});
2628

2729
it('get File from jpg base64', async () => {
28-
const file = await getFilefromDataUrl(base64String, JPG_PATH)
29-
expect(file.type).to.equal('image/jpeg')
30-
expect(file.size).to.equal(2001612)
31-
expect(file).to.be.an.instanceof(Blob)
32-
})
30+
const file = await getFilefromDataUrl(base64String, JPG_PATH);
31+
expect(file.type).to.equal('image/jpeg');
32+
expect(file.size).to.equal(2001612);
33+
expect(file).to.be.an.instanceof(Blob);
34+
});
3335

3436
it('get base64 from jpg file', async () => {
35-
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' })
36-
const base64 = await getDataUrlFromFile(file)
37-
expect(base64).to.equal(base64String)
38-
})
37+
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' });
38+
const base64 = await getDataUrlFromFile(file);
39+
expect(base64).to.equal(base64String);
40+
});
3941

4042
it('get File from png base64', async () => {
41-
const file = await getFilefromDataUrl(base64String2, PNG_PATH)
42-
expect(file.type).to.equal('image/png')
43-
expect(file.size).to.equal(2304210)
44-
expect(file).to.be.an.instanceof(Blob)
45-
})
43+
const file = await getFilefromDataUrl(base64String2, PNG_PATH);
44+
expect(file.type).to.equal('image/png');
45+
expect(file.size).to.equal(2304210);
46+
expect(file).to.be.an.instanceof(Blob);
47+
});
4648

4749
it('get base64 from png file', async () => {
48-
const file = new File([PNG_FILE], PNG_NAME, { type: 'image/png' })
49-
const base64 = await getDataUrlFromFile(file)
50-
expect(base64).to.equal(base64String2)
51-
})
50+
const file = new File([PNG_FILE], PNG_NAME, { type: 'image/png' });
51+
const base64 = await getDataUrlFromFile(file);
52+
expect(base64).to.equal(base64String2);
53+
});
5254

5355
it('load image', async () => {
54-
const img = await loadImage(base64String)
55-
expect(img).to.be.an.instanceof(Image)
56-
expect(img.src).to.equal(base64String)
57-
})
56+
const img = await loadImage(base64String);
57+
expect(img).to.be.an.instanceof(Image);
58+
expect(img.src).to.equal(base64String);
59+
});
5860

5961
it('draw image in canvas', async () => {
60-
const img = await loadImage(base64String)
61-
const canvas = await drawImageInCanvas(img)
62-
expect(canvas).to.be.an.instanceof(HTMLCanvasElement)
63-
expect(canvas.width).to.be.a('number')
64-
expect(canvas.height).to.be.a('number')
65-
expect(canvas.getContext).to.be.a('function')
66-
})
62+
const img = await loadImage(base64String);
63+
const canvas = await drawImageInCanvas(img);
64+
expect(canvas).to.be.an.instanceof(HTMLCanvasElement);
65+
expect(canvas.width).to.be.a('number');
66+
expect(canvas.height).to.be.a('number');
67+
expect(canvas.getContext).to.be.a('function');
68+
});
6769

6870
it('draw file in canvas', async () => {
69-
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' })
71+
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' });
7072

71-
const [img, canvas] = await drawFileInCanvas(file)
73+
const [img, canvas] = await drawFileInCanvas(file);
7274
// expect(img).to.satisfy((c) => c instanceof HTMLImageElement || c instanceof ImageBitmap)
73-
expect(img).to.be.an.instanceof(HTMLImageElement)
74-
expect(canvas).to.be.an.instanceof(HTMLCanvasElement)
75-
expect(canvas.width).to.be.a('number')
76-
expect(canvas.height).to.be.a('number')
77-
expect(canvas.getContext).to.be.a('function')
78-
})
75+
expect(img).to.be.an.instanceof(HTMLImageElement);
76+
expect(canvas).to.be.an.instanceof(HTMLCanvasElement);
77+
expect(canvas.width).to.be.a('number');
78+
expect(canvas.height).to.be.a('number');
79+
expect(canvas.getContext).to.be.a('function');
80+
});
7981

8082
it('compress jpg image file', async () => {
81-
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' })
83+
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' });
8284

83-
const maxSizeMB = 1
84-
const maxSizeByte = maxSizeMB * 1024 * 1024
85+
const maxSizeMB = 1;
86+
const maxSizeByte = maxSizeMB * 1024 * 1024;
8587

86-
const compressedFile = await imageCompression(file, { maxSizeMB, useWebWorker: false, exifOrientation: -2 })
87-
expect(compressedFile.size).to.be.at.most(maxSizeByte)
88-
})
88+
const compressedFile = await imageCompression(file, { maxSizeMB, useWebWorker: false, exifOrientation: -2 });
89+
expect(compressedFile.size).to.be.at.most(maxSizeByte);
90+
});
8991

9092
it('resize jpg image file', async () => {
91-
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' })
93+
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' });
9294

93-
const maxWidthOrHeight = 720
95+
const maxWidthOrHeight = 720;
9496

95-
const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 })
97+
const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 });
9698

97-
const temp = await drawFileInCanvas(compressedFile)
98-
const img = temp[0]
99-
expect(img.width).to.be.at.most(maxWidthOrHeight)
100-
expect(img.height).to.be.at.most(maxWidthOrHeight)
101-
102-
})
99+
const temp = await drawFileInCanvas(compressedFile);
100+
const img = temp[0];
101+
expect(img.width).to.be.at.most(maxWidthOrHeight);
102+
expect(img.height).to.be.at.most(maxWidthOrHeight);
103+
});
103104

104105
it('compress and resize jpg image file', async () => {
105-
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' })
106+
const file = new File([JPG_FILE], JPG_NAME, { type: 'image/jpeg' });
106107

107-
const maxSizeMB = 1
108-
const maxSizeByte = maxSizeMB * 1024 * 1024
109-
const maxWidthOrHeight = 720
108+
const maxSizeMB = 1;
109+
const maxSizeByte = maxSizeMB * 1024 * 1024;
110+
const maxWidthOrHeight = 720;
110111

111112
const compressedFile = await imageCompression(file, {
112113
maxSizeMB,
113114
maxWidthOrHeight,
114115
useWebWorker: false,
115-
exifOrientation: -2
116-
})
117-
118-
expect(compressedFile.size).to.be.at.most(maxSizeByte)
116+
exifOrientation: -2,
117+
});
119118

120-
const temp = await drawFileInCanvas(compressedFile)
121-
const img = temp[0]
122-
expect(img.width).to.be.at.most(maxWidthOrHeight)
123-
expect(img.height).to.be.at.most(maxWidthOrHeight)
119+
expect(compressedFile.size).to.be.at.most(maxSizeByte);
124120

125-
})
121+
const temp = await drawFileInCanvas(compressedFile);
122+
const img = temp[0];
123+
expect(img.width).to.be.at.most(maxWidthOrHeight);
124+
expect(img.height).to.be.at.most(maxWidthOrHeight);
125+
});
126126

127127
it('compress png image file', async () => {
128-
const file = new File([PNG_FILE], PNG_NAME, { type: 'image/png' })
128+
const file = new File([PNG_FILE], PNG_NAME, { type: 'image/png' });
129129

130-
const maxSizeMB = 1
131-
const maxSizeByte = maxSizeMB * 1024 * 1024
130+
const maxSizeMB = 1;
131+
const maxSizeByte = maxSizeMB * 1024 * 1024;
132132

133-
const compressedFile = await imageCompression(file, { maxSizeMB, useWebWorker: false, exifOrientation: -2 })
134-
expect(compressedFile.size).to.be.at.most(maxSizeByte)
135-
})
133+
const compressedFile = await imageCompression(file, { maxSizeMB, useWebWorker: false, exifOrientation: -2 });
134+
expect(compressedFile.size).to.be.at.most(maxSizeByte);
135+
});
136136

137137
it('resize png image file', async () => {
138-
const file = new File([PNG_FILE], PNG_NAME, { type: 'image/png' })
138+
const file = new File([PNG_FILE], PNG_NAME, { type: 'image/png' });
139139

140-
const maxWidthOrHeight = 720
140+
const maxWidthOrHeight = 720;
141141

142-
const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 })
142+
const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 });
143143

144-
const temp = await drawFileInCanvas(compressedFile)
144+
const temp = await drawFileInCanvas(compressedFile);
145145

146-
const img = temp[0]
147-
expect(img.width).to.be.at.most(maxWidthOrHeight)
148-
expect(img.height).to.be.at.most(maxWidthOrHeight)
149-
150-
})
146+
const img = temp[0];
147+
expect(img.width).to.be.at.most(maxWidthOrHeight);
148+
expect(img.height).to.be.at.most(maxWidthOrHeight);
149+
});
151150

152151
it('compress and resize png image file', async () => {
153-
const file = new File([PNG_FILE], PNG_NAME, { type: 'image/png' })
152+
const file = new File([PNG_FILE], PNG_NAME, { type: 'image/png' });
154153

155-
const maxSizeMB = 1
156-
const maxSizeByte = maxSizeMB * 1024 * 1024
157-
const maxWidthOrHeight = 720
154+
const maxSizeMB = 1;
155+
const maxSizeByte = maxSizeMB * 1024 * 1024;
156+
const maxWidthOrHeight = 720;
158157

159158
const compressedFile = await imageCompression(file, {
160159
maxSizeMB,
161160
maxWidthOrHeight,
162161
useWebWorker: false,
163-
exifOrientation: -2
164-
})
165-
166-
expect(compressedFile.size).to.be.at.most(maxSizeByte)
162+
exifOrientation: -2,
163+
});
167164

168-
const temp = await drawFileInCanvas(compressedFile)
165+
expect(compressedFile.size).to.be.at.most(maxSizeByte);
169166

170-
const img = temp[0]
171-
expect(img.width).to.be.at.most(maxWidthOrHeight)
172-
expect(img.height).to.be.at.most(maxWidthOrHeight)
167+
const temp = await drawFileInCanvas(compressedFile);
173168

174-
})
169+
const img = temp[0];
170+
expect(img.width).to.be.at.most(maxWidthOrHeight);
171+
expect(img.height).to.be.at.most(maxWidthOrHeight);
172+
});
175173

176174
it('fails if wrong file provided', async () => {
177-
const file = undefined
175+
const file = undefined;
178176

179-
const maxSizeMB = 1
177+
const maxSizeMB = 1;
180178
return expect(imageCompression(file, {
181179
maxSizeMB,
182-
useWebWorker: false
183-
})).to.eventually.rejectedWith(/not an instance of/)
184-
})
180+
useWebWorker: false,
181+
})).to.eventually.rejectedWith(/not an instance of/);
182+
});
185183

186184
it('fails if wrong file provided 2', async () => {
187-
const file = { type: '' }
185+
const file = { type: '' };
188186

189-
const maxSizeMB = 1
187+
const maxSizeMB = 1;
190188
return expect(imageCompression(file, {
191189
maxSizeMB,
192-
useWebWorker: false
193-
})).to.eventually.rejectedWith(/not an instance of/)
194-
})
190+
useWebWorker: false,
191+
})).to.eventually.rejectedWith(/not an instance of/);
192+
});
195193

196194
it('fails if wrong file type provided', async () => {
197-
const file = new File(['What is the meaning of life the universe and everything?'], 'text.txt', { type: 'text/plain' })
195+
const file = new File(['What is the meaning of life the universe and everything?'], 'text.txt', { type: 'text/plain' });
198196

199-
const maxSizeMB = 1
200-
await expect(imageCompression(file, { maxSizeMB, useWebWorker: false })).to.eventually.rejectedWith(/not an image/)
201-
})
197+
const maxSizeMB = 1;
198+
await expect(imageCompression(file, { maxSizeMB, useWebWorker: false })).to.eventually.rejectedWith(/not an image/);
199+
});
202200

203201
it('get the image orientation from Exif', async () => {
204-
const file = new File(JPG_FILE, JPG_NAME)
205-
const orientation = await getExifOrientation(file)
206-
expect(orientation).to.equal(-2)
207-
})
202+
const file = new File(JPG_FILE, JPG_NAME);
203+
const orientation = await getExifOrientation(file);
204+
expect(orientation).to.equal(-2);
205+
});
208206

209207
afterEach(() => {
210-
})
211-
212-
})
208+
});
209+
});

test/setup_jsdom.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
const jsdom = require('jsdom')
2-
const { JSDOM } = jsdom
1+
const jsdom = require('jsdom');
32

4-
const { window } = new JSDOM('', { resources: 'usable' })
3+
const { JSDOM } = jsdom;
4+
5+
const { window } = new JSDOM('', { resources: 'usable' });
56

67
if (typeof window.Worker === 'undefined') {
7-
window.Worker = function () {}
8+
window.Worker = function () {};
89
}
910

10-
global.window = window
11-
const KEYS = ['document', 'navigator', 'Blob', 'File', 'URL', 'Worker', 'FileReader', 'atob', 'Uint8Array', 'Image', 'HTMLCanvasElement', 'HTMLImageElement']
12-
KEYS.forEach(key => global[key] = window[key])
11+
global.window = window;
12+
const KEYS = ['document', 'navigator', 'Blob', 'File', 'URL', 'Worker', 'FileReader', 'atob', 'Uint8Array', 'Image', 'HTMLCanvasElement', 'HTMLImageElement'];
13+
KEYS.forEach((key) => global[key] = window[key]);
1314

1415
if (typeof window.URL.createObjectURL === 'undefined') {
15-
Object.defineProperty(window.URL, 'createObjectURL', { value: () => {} })
16+
Object.defineProperty(window.URL, 'createObjectURL', { value: () => {} });
1617
}

0 commit comments

Comments
 (0)