Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__/
*.pyc
node_modules/
65 changes: 65 additions & 0 deletions docs/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const fs = require('fs');
const path = require('path');

describe('loadFormats Error Path', () => {
test('should populate fallback formats when fetch fails', async () => {
const html = fs.readFileSync(path.resolve(__dirname, 'index.html'), 'utf-8');

// Mock fetch to reject ONLY for format_database.json
global.fetch = jest.fn().mockImplementation((url) => {
if (url.includes('format_database.json')) {
return Promise.reject(new Error('Failed to fetch format database'));
}
// For the WASM fetch, just resolve it slowly or return a dummy buffer
return new Promise((resolve) => setTimeout(() => resolve({
arrayBuffer: () => Promise.resolve(new ArrayBuffer(0))
}), 2000));
});

// Mock URL methods
global.URL.createObjectURL = jest.fn();
global.URL.revokeObjectURL = jest.fn();

// Ensure DOM is empty first
document.body.innerHTML = '';

// Now set the innerHTML to our full HTML
document.documentElement.innerHTML = html;

// Get the script tag content
const scripts = document.querySelectorAll('script');
let mainScript = '';
scripts.forEach(script => {
if (!script.src && script.textContent.includes('loadFormats')) {
mainScript = script.textContent;
}
});

// Remove import statements since eval doesn't support them in this context
mainScript = mainScript.replace(/import\s+.*?from\s+['"].*?['"];?/g, '');

global.initializeImageMagick = jest.fn().mockResolvedValue();
global.MagickFormat = {};

// Execute the script
eval(mainScript);

// Let the promises resolve (specifically the rejected fetch for format_database)
await new Promise(resolve => setTimeout(resolve, 50));

const targetFormatSelect = document.getElementById('targetFormat');
const fileInput = document.getElementById('fileInput');
const statusDiv = document.getElementById('status');

expect(statusDiv.textContent).toBe("Failed to load format database. Defaulting to basic formats.");

const options = targetFormatSelect.querySelectorAll('option');
expect(options.length).toBe(4);
expect(options[0].value).toBe('png');
expect(options[1].value).toBe('jpg');
expect(options[2].value).toBe('webp');
expect(options[3].value).toBe('pdf');

expect(fileInput.accept).toBe('.png,.jpg,.jpeg,.webp,.pdf');
});
});
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: 'jest-environment-jsdom'
};
Loading