|
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); |
20 | 22 |
|
21 | 23 | describe('Tests', function () {
|
22 |
| - this.timeout(30000) |
| 24 | + this.timeout(30000); |
23 | 25 |
|
24 | 26 | beforeEach(() => {
|
25 |
| - }) |
| 27 | + }); |
26 | 28 |
|
27 | 29 | 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 | + }); |
33 | 35 |
|
34 | 36 | 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 | + }); |
39 | 41 |
|
40 | 42 | 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 | + }); |
46 | 48 |
|
47 | 49 | 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 | + }); |
52 | 54 |
|
53 | 55 | 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 | + }); |
58 | 60 |
|
59 | 61 | 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 | + }); |
67 | 69 |
|
68 | 70 | 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' }); |
70 | 72 |
|
71 |
| - const [img, canvas] = await drawFileInCanvas(file) |
| 73 | + const [img, canvas] = await drawFileInCanvas(file); |
72 | 74 | // 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 | + }); |
79 | 81 |
|
80 | 82 | 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' }); |
82 | 84 |
|
83 |
| - const maxSizeMB = 1 |
84 |
| - const maxSizeByte = maxSizeMB * 1024 * 1024 |
| 85 | + const maxSizeMB = 1; |
| 86 | + const maxSizeByte = maxSizeMB * 1024 * 1024; |
85 | 87 |
|
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 | + }); |
89 | 91 |
|
90 | 92 | 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' }); |
92 | 94 |
|
93 |
| - const maxWidthOrHeight = 720 |
| 95 | + const maxWidthOrHeight = 720; |
94 | 96 |
|
95 |
| - const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 }) |
| 97 | + const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 }); |
96 | 98 |
|
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 | + }); |
103 | 104 |
|
104 | 105 | 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' }); |
106 | 107 |
|
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; |
110 | 111 |
|
111 | 112 | const compressedFile = await imageCompression(file, {
|
112 | 113 | maxSizeMB,
|
113 | 114 | maxWidthOrHeight,
|
114 | 115 | useWebWorker: false,
|
115 |
| - exifOrientation: -2 |
116 |
| - }) |
117 |
| - |
118 |
| - expect(compressedFile.size).to.be.at.most(maxSizeByte) |
| 116 | + exifOrientation: -2, |
| 117 | + }); |
119 | 118 |
|
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); |
124 | 120 |
|
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 | + }); |
126 | 126 |
|
127 | 127 | 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' }); |
129 | 129 |
|
130 |
| - const maxSizeMB = 1 |
131 |
| - const maxSizeByte = maxSizeMB * 1024 * 1024 |
| 130 | + const maxSizeMB = 1; |
| 131 | + const maxSizeByte = maxSizeMB * 1024 * 1024; |
132 | 132 |
|
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 | + }); |
136 | 136 |
|
137 | 137 | 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' }); |
139 | 139 |
|
140 |
| - const maxWidthOrHeight = 720 |
| 140 | + const maxWidthOrHeight = 720; |
141 | 141 |
|
142 |
| - const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 }) |
| 142 | + const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 }); |
143 | 143 |
|
144 |
| - const temp = await drawFileInCanvas(compressedFile) |
| 144 | + const temp = await drawFileInCanvas(compressedFile); |
145 | 145 |
|
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 | + }); |
151 | 150 |
|
152 | 151 | 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' }); |
154 | 153 |
|
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; |
158 | 157 |
|
159 | 158 | const compressedFile = await imageCompression(file, {
|
160 | 159 | maxSizeMB,
|
161 | 160 | maxWidthOrHeight,
|
162 | 161 | useWebWorker: false,
|
163 |
| - exifOrientation: -2 |
164 |
| - }) |
165 |
| - |
166 |
| - expect(compressedFile.size).to.be.at.most(maxSizeByte) |
| 162 | + exifOrientation: -2, |
| 163 | + }); |
167 | 164 |
|
168 |
| - const temp = await drawFileInCanvas(compressedFile) |
| 165 | + expect(compressedFile.size).to.be.at.most(maxSizeByte); |
169 | 166 |
|
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); |
173 | 168 |
|
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 | + }); |
175 | 173 |
|
176 | 174 | it('fails if wrong file provided', async () => {
|
177 |
| - const file = undefined |
| 175 | + const file = undefined; |
178 | 176 |
|
179 |
| - const maxSizeMB = 1 |
| 177 | + const maxSizeMB = 1; |
180 | 178 | return expect(imageCompression(file, {
|
181 | 179 | 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 | + }); |
185 | 183 |
|
186 | 184 | it('fails if wrong file provided 2', async () => {
|
187 |
| - const file = { type: '' } |
| 185 | + const file = { type: '' }; |
188 | 186 |
|
189 |
| - const maxSizeMB = 1 |
| 187 | + const maxSizeMB = 1; |
190 | 188 | return expect(imageCompression(file, {
|
191 | 189 | 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 | + }); |
195 | 193 |
|
196 | 194 | 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' }); |
198 | 196 |
|
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 | + }); |
202 | 200 |
|
203 | 201 | 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 | + }); |
208 | 206 |
|
209 | 207 | afterEach(() => {
|
210 |
| - }) |
211 |
| - |
212 |
| -}) |
| 208 | + }); |
| 209 | +}); |
0 commit comments