File size: 2,763 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Tests for client-side image resizing utility
 */

import { shouldResizeImage, supportsClientResize } from '../imageResize';

// Mock browser APIs for testing
Object.defineProperty(global, 'HTMLCanvasElement', {
  value: function () {
    return {
      getContext: () => ({
        drawImage: jest.fn(),
      }),
      toBlob: jest.fn(),
    };
  },
  writable: true,
});

Object.defineProperty(global, 'FileReader', {
  value: function () {
    return {
      readAsDataURL: jest.fn(),
    };
  },
  writable: true,
});

Object.defineProperty(global, 'Image', {
  value: function () {
    return {};
  },
  writable: true,
});

describe('imageResize utility', () => {
  describe('supportsClientResize', () => {
    it('should return true when all required APIs are available', () => {
      const result = supportsClientResize();
      expect(result).toBe(true);
    });

    it('should return false when HTMLCanvasElement is not available', () => {
      const originalCanvas = global.HTMLCanvasElement;
      // @ts-ignore
      delete global.HTMLCanvasElement;

      const result = supportsClientResize();
      expect(result).toBe(false);

      global.HTMLCanvasElement = originalCanvas;
    });
  });

  describe('shouldResizeImage', () => {
    it('should return true for large image files', () => {
      const largeImageFile = new File([''], 'test.jpg', {
        type: 'image/jpeg',
        lastModified: Date.now(),
      });

      // Mock large file size
      Object.defineProperty(largeImageFile, 'size', {
        value: 100 * 1024 * 1024, // 100MB
        writable: false,
      });

      const result = shouldResizeImage(largeImageFile, 50 * 1024 * 1024); // 50MB limit
      expect(result).toBe(true);
    });

    it('should return false for small image files', () => {
      const smallImageFile = new File([''], 'test.jpg', {
        type: 'image/jpeg',
        lastModified: Date.now(),
      });

      // Mock small file size
      Object.defineProperty(smallImageFile, 'size', {
        value: 1024, // 1KB
        writable: false,
      });

      const result = shouldResizeImage(smallImageFile, 50 * 1024 * 1024); // 50MB limit
      expect(result).toBe(false);
    });

    it('should return false for non-image files', () => {
      const textFile = new File([''], 'test.txt', {
        type: 'text/plain',
        lastModified: Date.now(),
      });

      const result = shouldResizeImage(textFile);
      expect(result).toBe(false);
    });

    it('should return false for GIF files', () => {
      const gifFile = new File([''], 'test.gif', {
        type: 'image/gif',
        lastModified: Date.now(),
      });

      const result = shouldResizeImage(gifFile);
      expect(result).toBe(false);
    });
  });
});