| Server IP : 66.29.153.156 / Your IP : 216.73.217.22 Web Server : LiteSpeed System : Linux premium322.web-hosting.com 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64 User : lastyfjz ( 1521) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/lastyfjz/././www/wp-content/plugins/extendify/src/Shared/utils/__tests__/ |
Upload File : |
// resize-image.test.js
import { resizeImage } from '@shared/utils/resize-image';
describe('resizeImage', () => {
beforeEach(() => {
Object.defineProperty(global.Image.prototype, 'src', {
set() {
setTimeout(() => {
this.width = 500;
this.height = 500;
this.onload();
}, 10);
},
});
HTMLCanvasElement.prototype.getContext = () => ({
clearRect: jest.fn(),
drawImage: jest.fn(),
});
HTMLCanvasElement.prototype.toBlob = function (callback, type) {
callback(new Blob(['mock'], { type }));
};
global.URL.createObjectURL = jest.fn(
() => 'blob:http://example.com/fake-url',
);
});
afterEach(() => {
delete global.URL.createObjectURL;
});
it('should resize image and return a blob URL', async () => {
const result = await resizeImage('http://example.com/img.png', {
size: { width: 64, height: 64 },
mimeType: 'image/png',
});
expect(typeof result).toBe('string');
expect(result).toBe('blob:http://example.com/fake-url');
expect(global.URL.createObjectURL).toHaveBeenCalledWith(expect.any(Blob));
});
it('should throw if imageUrl is missing', async () => {
await expect(
resizeImage(null, { width: 64, height: 64 }, 'image/png'),
).rejects.toThrow('Invalid imageUrl or size dimensions');
});
it('should throw if size is missing', async () => {
await expect(resizeImage('http://example.com/img.png', {})).rejects.toThrow(
'Invalid imageUrl or size dimensions',
);
});
it('should throw if size is not an object', async () => {
await expect(
resizeImage('http://example.com/img.png', 64, 'image/png'),
).rejects.toThrow('Invalid imageUrl or size dimensions');
});
it('should throw if width or height are not valid numbers', async () => {
await expect(
resizeImage('http://example.com/img.png', { width: 0, height: 64 }),
).rejects.toThrow('Invalid imageUrl or size dimensions');
await expect(
resizeImage('http://example.com/img.png', { width: 64, height: -1 }),
).rejects.toThrow('Invalid imageUrl or size dimensions');
await expect(
resizeImage('http://example.com/img.png', { width: '64', height: 64 }),
).rejects.toThrow('Invalid imageUrl or size dimensions');
});
});