Files
electron/spec/api-web-utils.spec.ts
Samuel Attard 8c92be4037 test: fix window leaks in api-subframe, api-view, api-web-frame, api-web-utils
- api-subframe: 'should not crash when changing subframe src' never closed w
- api-view: 'can be added as a child of another View' declared a local const w
  shadowing the describe-level one the afterEach closes
- api-web-frame / api-web-utils: defer(() => w.close()) and afterAll win.close()
  don't await 'closed', so windows were still in getAllWindows() when the
  global leak check fired; use closeWindow() which awaits
2026-04-13 01:00:14 -07:00

66 lines
2.0 KiB
TypeScript

import { BrowserWindow } from 'electron/main';
import { expect } from 'chai';
import { describe, it } from 'vitest';
import * as path from 'node:path';
import { defer } from './lib/spec-helpers';
import { closeWindow } from './lib/window-helpers';
// import { once } from 'node:events';
describe('webUtils module', () => {
const fixtures = path.resolve(__dirname, 'fixtures');
describe('getPathForFile', () => {
it('returns nothing for a Blob', async () => {
const w = new BrowserWindow({
show: false,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
sandbox: false
}
});
defer(() => closeWindow(w));
await w.loadFile(path.resolve(fixtures, 'pages', 'file-input.html'));
const pathFromWebUtils = await w.webContents.executeJavaScript(
'require("electron").webUtils.getPathForFile(new Blob([1, 2, 3]))'
);
expect(pathFromWebUtils).to.equal('');
});
it('reports the correct path for a File object', async () => {
const w = new BrowserWindow({
show: false,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
sandbox: false
}
});
defer(() => closeWindow(w));
await w.loadFile(path.resolve(fixtures, 'pages', 'file-input.html'));
const { debugger: debug } = w.webContents;
debug.attach();
try {
const {
root: { nodeId }
} = await debug.sendCommand('DOM.getDocument');
const { nodeId: inputNodeId } = await debug.sendCommand('DOM.querySelector', { nodeId, selector: 'input' });
await debug.sendCommand('DOM.setFileInputFiles', {
files: [__filename],
nodeId: inputNodeId
});
const pathFromWebUtils = await w.webContents.executeJavaScript(
'require("electron").webUtils.getPathForFile(document.querySelector("input").files[0])'
);
expect(pathFromWebUtils).to.equal(__filename);
} finally {
debug.detach();
}
});
});
});