Files
electron/spec/api-view.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

161 lines
4.5 KiB
TypeScript

import { BaseWindow, View } from 'electron/main';
import { expect } from 'chai';
import { afterEach, describe, it } from 'vitest';
import { withDone } from './lib/spec-helpers';
import { closeWindow } from './lib/window-helpers';
describe('View', () => {
let w: BaseWindow;
afterEach(async () => {
await closeWindow(w as any);
w = null as unknown as BaseWindow;
});
it('can be used as content view', () => {
w = new BaseWindow({ show: false });
const v = new View();
w.setContentView(v);
expect(w.contentView).to.equal(v);
});
it('will throw when added as a child to itself', () => {
w = new BaseWindow({ show: false });
expect(() => {
w.contentView.addChildView(w.contentView);
}).to.throw('A view cannot be added as its own child');
});
it('does not crash when attempting to add a child multiple times', () => {
w = new BaseWindow({ show: false });
const cv = new View();
w.setContentView(cv);
const v = new View();
w.contentView.addChildView(v);
w.contentView.addChildView(v);
w.contentView.addChildView(v);
expect(w.contentView.children).to.have.lengthOf(1);
});
it('can be added as a child of another View', async () => {
w = new BaseWindow();
const v1 = new View();
const v2 = new View();
v1.addChildView(v2);
w.contentView.addChildView(v1);
expect(w.contentView.children).to.deep.equal([v1]);
expect(v1.children).to.deep.equal([v2]);
});
it('correctly reorders children', () => {
w = new BaseWindow({ show: false });
const cv = new View();
w.setContentView(cv);
const v1 = new View();
const v2 = new View();
const v3 = new View();
w.contentView.addChildView(v1);
w.contentView.addChildView(v2);
w.contentView.addChildView(v3);
expect(w.contentView.children).to.deep.equal([v1, v2, v3]);
w.contentView.addChildView(v1);
w.contentView.addChildView(v2);
expect(w.contentView.children).to.deep.equal([v3, v1, v2]);
});
it('allows setting various border radius values', () => {
w = new BaseWindow({ show: false });
const v = new View();
w.setContentView(v);
v.setBorderRadius(10);
v.setBorderRadius(0);
v.setBorderRadius(-10);
v.setBorderRadius(9999999);
v.setBorderRadius(-9999999);
});
describe('view.getVisible|setVisible', () => {
it('is visible by default', () => {
const v = new View();
expect(v.getVisible()).to.be.true();
});
it('can be set to not visible', () => {
const v = new View();
v.setVisible(false);
expect(v.getVisible()).to.be.false();
});
});
describe('view.getBounds|setBounds', () => {
it('defaults to 0,0,0,0', () => {
const v = new View();
expect(v.getBounds()).to.deep.equal({ x: 0, y: 0, width: 0, height: 0 });
});
it('can be set and retrieved', () => {
const v = new View();
v.setBounds({ x: 10, y: 20, width: 300, height: 400 });
expect(v.getBounds()).to.deep.equal({ x: 10, y: 20, width: 300, height: 400 });
});
it('emits bounds-changed when bounds mutate', () => {
const v = new View();
let called = 0;
v.once('bounds-changed', () => {
called++;
});
v.setBounds({ x: 5, y: 6, width: 7, height: 8 });
expect(called).to.equal(1);
});
it('allows zero-size bounds', () => {
const v = new View();
v.setBounds({ x: 1, y: 2, width: 0, height: 0 });
expect(v.getBounds()).to.deep.equal({ x: 1, y: 2, width: 0, height: 0 });
});
it('allows negative coordinates', () => {
const v = new View();
v.setBounds({ x: -10, y: -20, width: 100, height: 50 });
expect(v.getBounds()).to.deep.equal({ x: -10, y: -20, width: 100, height: 50 });
});
it('child bounds remain relative after parent moves', () => {
const parent = new View();
const child = new View();
parent.addChildView(child);
child.setBounds({ x: 10, y: 15, width: 25, height: 30 });
parent.setBounds({ x: 50, y: 60, width: 500, height: 600 });
expect(child.getBounds()).to.deep.equal({ x: 10, y: 15, width: 25, height: 30 });
});
it(
'can set bounds with animation',
withDone((done) => {
const v = new View();
v.setBounds(
{ x: 0, y: 0, width: 100, height: 100 },
{
animate: {
duration: 300
}
}
);
setTimeout(() => {
expect(v.getBounds()).to.deep.equal({ x: 0, y: 0, width: 100, height: 100 });
done();
}, 350);
})
);
});
});