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/
54 changes: 54 additions & 0 deletions docs/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const fs = require('fs');
const path = require('path');
const { JSDOM } = require('jsdom');

describe('Universal Image Converter', () => {
let dom;
let document;
let window;

beforeEach(async () => {
const htmlPath = path.resolve(__dirname, 'index.html');
const htmlContent = fs.readFileSync(htmlPath, 'utf8');

// We need runScripts: "dangerously" to use window.eval
dom = new JSDOM(htmlContent, {
runScripts: "dangerously"
});
document = dom.window.document;
window = dom.window;
});

test('should display error message when initWasm fails', async () => {
const htmlPath = path.resolve(__dirname, 'index.html');
const htmlContent = fs.readFileSync(htmlPath, 'utf8');

const scriptContent = htmlContent.match(/<script type="module">([\s\S]*?)<\/script>/)[1];

window.fetch = jest.fn(() => Promise.reject(new Error("Network Error")));

const initWasmStr = scriptContent.match(/async function initWasm\(\) \{[\s\S]*?\n \}/)[0];

window.eval(`
${initWasmStr}

window.testInitWasm = initWasm;

window.isEngineReady = false;
window.convertBtn = document.getElementById('convertBtn');
window.statusDiv = document.getElementById('status');
window.initializeImageMagick = async function() {};
`);

const originalError = console.error;
console.error = jest.fn();

await window.testInitWasm();

expect(window.statusDiv.textContent).toBe("Failed to initialize ImageMagick WASM engine.");
expect(window.fetch).toHaveBeenCalledWith("https://unpkg.com/@imagemagick/magick-wasm@0.0.30/dist/magick.wasm");
expect(console.error).toHaveBeenCalledWith(expect.any(Error));

console.error = originalError;
});
});
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
testEnvironment: "jsdom",
setupFiles: ["./setupJest.js"]
};
Loading