|
| 1 | +// resolveUtils.unit.test.jsx |
| 2 | + |
| 3 | +import resolvePathsForElementsWithAttribute from '../../../../../server/utils/resolveUtils'; |
| 4 | +import { resolvePathToFile } from '../../../../../server/utils/filePath'; |
| 5 | + |
| 6 | +// Mock the dependencies |
| 7 | +jest.mock('../../../../../server/utils/filePath', () => ({ |
| 8 | + resolvePathToFile: jest.fn() |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('../../../../../server/utils/fileUtils', () => ({ |
| 12 | + MEDIA_FILE_REGEX: /\.(png|jpg|jpeg|gif|svg)$/i |
| 13 | +})); |
| 14 | + |
| 15 | +describe('resolvePathsForElementsWithAttribute', () => { |
| 16 | + let mockSketchDoc; |
| 17 | + let mockFiles; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + jest.clearAllMocks(); |
| 21 | + |
| 22 | + // Create a mock DOM environment |
| 23 | + mockSketchDoc = document.implementation.createHTMLDocument(); |
| 24 | + mockFiles = { |
| 25 | + 'image.png': { url: 'https://example.com/image.png' }, |
| 26 | + 'missing.jpg': { url: null } |
| 27 | + }; |
| 28 | + |
| 29 | + resolvePathToFile.mockImplementation((fileName, files) => files[fileName]); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should update the attribute when the file is resolved successfully', () => { |
| 33 | + const element = mockSketchDoc.createElement('img'); |
| 34 | + element.setAttribute('src', 'image.png'); |
| 35 | + mockSketchDoc.body.appendChild(element); |
| 36 | + |
| 37 | + resolvePathsForElementsWithAttribute('src', mockSketchDoc, mockFiles); |
| 38 | + |
| 39 | + expect(element.getAttribute('src')).toBe('https://example.com/image.png'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should not update the attribute when the file resolution fails', () => { |
| 43 | + const element = mockSketchDoc.createElement('img'); |
| 44 | + element.setAttribute('src', 'missing.jpg'); |
| 45 | + mockSketchDoc.body.appendChild(element); |
| 46 | + |
| 47 | + resolvePathsForElementsWithAttribute('src', mockSketchDoc, mockFiles); |
| 48 | + |
| 49 | + expect(element.getAttribute('src')).toBe('missing.jpg'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should not update the attribute when the value does not match MEDIA_FILE_REGEX', () => { |
| 53 | + const element = mockSketchDoc.createElement('img'); |
| 54 | + element.setAttribute('src', 'document.pdf'); |
| 55 | + mockSketchDoc.body.appendChild(element); |
| 56 | + |
| 57 | + resolvePathsForElementsWithAttribute('src', mockSketchDoc, mockFiles); |
| 58 | + |
| 59 | + expect(element.getAttribute('src')).toBe('document.pdf'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should do nothing when no elements with the specified attribute are found', () => { |
| 63 | + resolvePathsForElementsWithAttribute('src', mockSketchDoc, mockFiles); |
| 64 | + |
| 65 | + expect(mockSketchDoc.querySelectorAll('[src]').length).toBe(0); |
| 66 | + }); |
| 67 | +}); |
0 commit comments