forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageCompression.spec.ts
More file actions
173 lines (143 loc) · 5.71 KB
/
Copy pathimageCompression.spec.ts
File metadata and controls
173 lines (143 loc) · 5.71 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { describe, it, expect } from 'vitest';
import type { CompressionOptions } from './imageCompression';
import {
compressImage,
blobToFile,
formatFileSize,
isValidImageType,
} from './imageCompression';
describe('imageCompression Utilities', () => {
describe('compressImage', () => {
it('should compress an image file', async () => {
// Create a test image file
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
const blob = await new Promise<Blob>((resolve) => {
canvas.toBlob((blob) => resolve(blob!), 'image/jpeg');
});
const file = new File([blob], 'test.jpg', { type: 'image/jpeg' });
const compressed = await compressImage(file);
expect(compressed).toBeInstanceOf(Blob);
expect(compressed.type).toBe('image/jpeg');
});
it('should respect max width constraint', async () => {
const canvas = document.createElement('canvas');
canvas.width = 3000;
canvas.height = 2000;
const blob = await new Promise<Blob>((resolve) => {
canvas.toBlob((blob) => resolve(blob!), 'image/jpeg');
});
const file = new File([blob], 'test.jpg', { type: 'image/jpeg' });
const options: CompressionOptions = {
maxWidth: 1920,
maxHeight: 1440,
};
const compressed = await compressImage(file, options);
expect(compressed).toBeInstanceOf(Blob);
});
it('should respect quality parameter', async () => {
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
const blob = await new Promise<Blob>((resolve) => {
canvas.toBlob((blob) => resolve(blob!), 'image/jpeg');
});
const file = new File([blob], 'test.jpg', { type: 'image/jpeg' });
const highQuality = await compressImage(file, { quality: 0.9 });
const lowQuality = await compressImage(file, { quality: 0.5 });
// Higher quality should result in larger file size
expect(highQuality.size).toBeGreaterThanOrEqual(lowQuality.size);
});
it('should maintain aspect ratio', async () => {
const canvas = document.createElement('canvas');
canvas.width = 2000;
canvas.height = 1000; // 2:1 aspect ratio
const blob = await new Promise<Blob>((resolve) => {
canvas.toBlob((blob) => resolve(blob!), 'image/jpeg');
});
const file = new File([blob], 'test.jpg', { type: 'image/jpeg' });
const options: CompressionOptions = {
maxWidth: 1000,
maxHeight: 500,
};
const compressed = await compressImage(file, options);
expect(compressed).toBeInstanceOf(Blob);
});
it('should handle invalid files gracefully', async () => {
const invalidFile = new File(['not an image'], 'test.txt', {
type: 'text/plain',
});
await expect(compressImage(invalidFile)).rejects.toThrow();
});
it('should use default quality if not specified', async () => {
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
const blob = await new Promise<Blob>((resolve) => {
canvas.toBlob((blob) => resolve(blob!), 'image/jpeg');
});
const file = new File([blob], 'test.jpg', { type: 'image/jpeg' });
const compressed = await compressImage(file, {});
expect(compressed).toBeInstanceOf(Blob);
});
});
describe('blobToFile', () => {
it('should convert blob to file', () => {
const blob = new Blob(['test content'], { type: 'image/jpeg' });
const file = blobToFile(blob, 'test.jpg');
expect(file).toBeInstanceOf(File);
expect(file.name).toBe('test.jpg');
expect(file.type).toBe('image/jpeg');
});
it('should preserve blob content', () => {
const content = 'test content';
const blob = new Blob([content], { type: 'image/jpeg' });
const file = blobToFile(blob, 'test.jpg');
expect(file.size).toBe(blob.size);
});
});
describe('formatFileSize', () => {
it('should format bytes correctly', () => {
expect(formatFileSize(0)).toBe('0 Bytes');
expect(formatFileSize(1024)).toBe('1 KB');
expect(formatFileSize(1024 * 1024)).toBe('1 MB');
expect(formatFileSize(1024 * 1024 * 1024)).toBe('1 GB');
});
it('should handle decimal sizes', () => {
expect(formatFileSize(512)).toBe('0.5 KB');
expect(formatFileSize(1536 * 1024)).toBe('1.5 MB');
});
it('should handle large numbers', () => {
const largeNumber = 1024 * 1024 * 1024 * 5; // 5GB
const result = formatFileSize(largeNumber);
expect(result).toContain('GB');
});
});
describe('isValidImageType', () => {
it('should accept JPEG files', () => {
const file = new File(['test'], 'test.jpg', { type: 'image/jpeg' });
expect(isValidImageType(file)).toBe(true);
});
it('should accept PNG files', () => {
const file = new File(['test'], 'test.png', { type: 'image/png' });
expect(isValidImageType(file)).toBe(true);
});
it('should accept WebP files', () => {
const file = new File(['test'], 'test.webp', { type: 'image/webp' });
expect(isValidImageType(file)).toBe(true);
});
it('should reject non-image files', () => {
const file = new File(['test'], 'test.txt', { type: 'text/plain' });
expect(isValidImageType(file)).toBe(false);
});
it('should reject PDF files', () => {
const file = new File(['test'], 'test.pdf', { type: 'application/pdf' });
expect(isValidImageType(file)).toBe(false);
});
it('should reject video files', () => {
const file = new File(['test'], 'test.mp4', { type: 'video/mp4' });
expect(isValidImageType(file)).toBe(false);
});
});
});