forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressImage.test.ts
59 lines (42 loc) · 1.68 KB
/
compressImage.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import compressImage from './compressImage';
const getContextSpy = vi.spyOn(global.HTMLCanvasElement.prototype, 'getContext');
const drawImageSpy = vi.spyOn(CanvasRenderingContext2D.prototype, 'drawImage');
beforeEach(() => {
getContextSpy.mockClear();
drawImageSpy.mockClear();
});
describe('compressImage', () => {
it('should compress image with maxWidth', () => {
const img = document.createElement('img');
img.width = 3000;
img.height = 2000;
const r = compressImage({ img });
expect(r).toMatch(/^data:image\/webp;base64,/);
expect(getContextSpy).toBeCalledTimes(1);
expect(getContextSpy).toBeCalledWith('2d');
expect(drawImageSpy).toBeCalledTimes(1);
expect(drawImageSpy).toBeCalledWith(img, 0, 0, 3000, 2000, 0, 0, 2160, 1440);
});
it('should compress image with maxHeight', () => {
const img = document.createElement('img');
img.width = 2000;
img.height = 3000;
const r = compressImage({ img });
expect(r).toMatch(/^data:image\/webp;base64,/);
expect(getContextSpy).toBeCalledTimes(1);
expect(getContextSpy).toBeCalledWith('2d');
expect(drawImageSpy).toBeCalledTimes(1);
expect(drawImageSpy).toBeCalledWith(img, 0, 0, 2000, 3000, 0, 0, 1440, 2160);
});
it('should not compress image', () => {
const img = document.createElement('img');
img.width = 2000;
img.height = 2000;
const r = compressImage({ img });
expect(r).toMatch(/^data:image\/webp;base64,/);
expect(getContextSpy).toBeCalledTimes(1);
expect(getContextSpy).toBeCalledWith('2d');
expect(drawImageSpy).toBeCalledTimes(1);
expect(drawImageSpy).toBeCalledWith(img, 0, 0, 2000, 2000, 0, 0, 2000, 2000);
});
});