|
| 1 | +import {beforeEach, describe, expect, it, vi} from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + cleanUrl, |
| 5 | + DIMENSION_PATTERN, |
| 6 | + extractHash, |
| 7 | + parseDimensionsFromHash, |
| 8 | +} from 'sentry-docs/components/docImage'; |
| 9 | +import {serverContext} from 'sentry-docs/serverContext'; |
| 10 | + |
| 11 | +// Mock the serverContext |
| 12 | +vi.mock('sentry-docs/serverContext', () => ({ |
| 13 | + serverContext: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +// Mock the ImageLightbox component |
| 17 | +vi.mock('../imageLightbox', () => ({ |
| 18 | + ImageLightbox: vi.fn(({src, alt, width, height, imgPath}) => ({ |
| 19 | + type: 'ImageLightbox', |
| 20 | + props: {src, alt, width, height, imgPath}, |
| 21 | + })), |
| 22 | +})); |
| 23 | + |
| 24 | +const mockServerContext = serverContext as any; |
| 25 | + |
| 26 | +describe('DocImage Helper Functions', () => { |
| 27 | + beforeEach(() => { |
| 28 | + mockServerContext.mockReturnValue({ |
| 29 | + path: '/docs/test-page', |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('dimension validation bounds', () => { |
| 34 | + it('should validate dimensions within acceptable range', () => { |
| 35 | + // Test actual boundary conditions used in parseDimensionsFromHash |
| 36 | + expect(parseDimensionsFromHash('/image.jpg#1x1')).toEqual([1, 1]); // minimum valid |
| 37 | + expect(parseDimensionsFromHash('/image.jpg#10000x10000')).toEqual([10000, 10000]); // maximum valid |
| 38 | + expect(parseDimensionsFromHash('/image.jpg#0x600')).toEqual([]); // below minimum |
| 39 | + expect(parseDimensionsFromHash('/image.jpg#10001x5000')).toEqual([]); // above maximum |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('dimension pattern regex', () => { |
| 44 | + it('should match valid dimension patterns', () => { |
| 45 | + expect(DIMENSION_PATTERN.test('800x600')).toBe(true); |
| 46 | + expect(DIMENSION_PATTERN.test('1920x1080')).toBe(true); |
| 47 | + expect(DIMENSION_PATTERN.test('10000x10000')).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should not match invalid dimension patterns', () => { |
| 51 | + expect(DIMENSION_PATTERN.test('800x')).toBe(false); |
| 52 | + expect(DIMENSION_PATTERN.test('x600')).toBe(false); |
| 53 | + expect(DIMENSION_PATTERN.test('800')).toBe(false); |
| 54 | + expect(DIMENSION_PATTERN.test('800x600x400')).toBe(false); |
| 55 | + expect(DIMENSION_PATTERN.test('abc800x600')).toBe(false); |
| 56 | + expect(DIMENSION_PATTERN.test('section-heading')).toBe(false); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('hash extraction', () => { |
| 61 | + it('should extract hash from URLs', () => { |
| 62 | + expect(extractHash('/image.jpg#800x600')).toBe('800x600'); |
| 63 | + expect(extractHash('./img/issue_page.png#1200x800')).toBe('1200x800'); |
| 64 | + expect(extractHash('https://example.com/image.jpg#section')).toBe('section'); |
| 65 | + expect(extractHash('/image.jpg')).toBe(''); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('dimension parsing from hash', () => { |
| 70 | + it('should parse valid dimensions from URL hash', () => { |
| 71 | + expect(parseDimensionsFromHash('/image.jpg#800x600')).toEqual([800, 600]); |
| 72 | + expect(parseDimensionsFromHash('/image.jpg#1920x1080')).toEqual([1920, 1080]); |
| 73 | + expect(parseDimensionsFromHash('/image.jpg#10000x10000')).toEqual([10000, 10000]); |
| 74 | + expect(parseDimensionsFromHash('./img/issue_page.png#1200x800')).toEqual([ |
| 75 | + 1200, 800, |
| 76 | + ]); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should return empty array for invalid dimensions', () => { |
| 80 | + const invalidCases = [ |
| 81 | + '/image.jpg#0x600', |
| 82 | + '/image.jpg#10001x5000', |
| 83 | + '/image.jpg#800x', |
| 84 | + '/image.jpg#section-heading', |
| 85 | + '/image.jpg', |
| 86 | + './img/error_level.png#malformed', |
| 87 | + ]; |
| 88 | + |
| 89 | + invalidCases.forEach(url => { |
| 90 | + expect(parseDimensionsFromHash(url)).toEqual([]); |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('URL cleaning', () => { |
| 96 | + it('should remove dimension hashes from URLs', () => { |
| 97 | + const testCases = [ |
| 98 | + {input: '/image.jpg#800x600', expected: '/image.jpg'}, |
| 99 | + { |
| 100 | + input: 'https://example.com/image.jpg#1920x1080', |
| 101 | + expected: 'https://example.com/image.jpg', |
| 102 | + }, |
| 103 | + {input: './img/issue_page.png#1200x800', expected: './img/issue_page.png'}, |
| 104 | + {input: './img/error_level.png#32x32', expected: './img/error_level.png'}, |
| 105 | + ]; |
| 106 | + |
| 107 | + testCases.forEach(({input, expected}) => { |
| 108 | + expect(cleanUrl(input)).toBe(expected); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should preserve non-dimension hashes', () => { |
| 113 | + const testCases = [ |
| 114 | + './img/issue_page.png#section-heading', |
| 115 | + '/image.jpg#important-section', |
| 116 | + '/image.jpg#anchor', |
| 117 | + ]; |
| 118 | + |
| 119 | + testCases.forEach(url => { |
| 120 | + expect(cleanUrl(url)).toBe(url); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should handle URLs without hashes', () => { |
| 125 | + const testCases = [ |
| 126 | + '/image.jpg', |
| 127 | + 'https://example.com/image.jpg', |
| 128 | + './img/issue_page.png', |
| 129 | + ]; |
| 130 | + |
| 131 | + testCases.forEach(url => { |
| 132 | + expect(cleanUrl(url)).toBe(url); |
| 133 | + }); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('Issues page integration scenarios', () => { |
| 138 | + const issuesPageImages = [ |
| 139 | + './img/issue_page.png#1200x800', |
| 140 | + './img/error_level.png#32x32', |
| 141 | + './img/issue_sort.png#600x400', |
| 142 | + ]; |
| 143 | + |
| 144 | + it('should handle Issues page image paths correctly', () => { |
| 145 | + issuesPageImages.forEach(path => { |
| 146 | + const hash = extractHash(path); |
| 147 | + const cleanedUrl = cleanUrl(path); |
| 148 | + const dimensions = parseDimensionsFromHash(path); |
| 149 | + |
| 150 | + expect(hash).toMatch(DIMENSION_PATTERN); |
| 151 | + expect(cleanedUrl).not.toContain('#'); |
| 152 | + expect(dimensions).toHaveLength(2); |
| 153 | + expect(dimensions[0]).toBeGreaterThan(0); |
| 154 | + expect(dimensions[1]).toBeGreaterThan(0); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should handle malformed relative paths gracefully', () => { |
| 159 | + const malformedPaths = [ |
| 160 | + './img/issue_page.png#800x', |
| 161 | + './img/error_level.png#invalid', |
| 162 | + './img/issue_sort.png#section-anchor', |
| 163 | + ]; |
| 164 | + |
| 165 | + malformedPaths.forEach(path => { |
| 166 | + expect(cleanUrl(path)).toBe(path); // Should not clean non-dimension hashes |
| 167 | + expect(parseDimensionsFromHash(path)).toEqual([]); // Should return empty array |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments