fix: Lazy unloading should not leave dangling connections (#2004)

- Closes #1992
This commit is contained in:
Dermot Duffy
2025-04-12 10:48:37 +01:00
committed by GitHub
parent a24c9628c5
commit a5ffd51728
12 changed files with 397 additions and 468 deletions
+25 -3
View File
@@ -383,15 +383,22 @@ export const requestAnimationFrameMock = (callback: FrameRequestCallback) => {
return 1;
};
export const getMockIntersectionObserver = (n = 0): IntersectionObserver | null => {
const mockResult = vi.mocked(IntersectionObserver).mock.results[n];
if (mockResult.type !== 'return') {
return null;
}
return mockResult.value;
};
export const callIntersectionHandler = async (
intersecting = true,
n = 0,
): Promise<void> => {
const mockResult = vi.mocked(IntersectionObserver).mock.results[n];
if (mockResult.type !== 'return') {
const observer = getMockIntersectionObserver(n);
if (!observer) {
return;
}
const observer = mockResult.value;
await (
vi.mocked(IntersectionObserver).mock.calls[n][0] as
| IntersectionObserverCallback
@@ -422,6 +429,20 @@ export const callMutationHandler = async (n = 0): Promise<void> => {
);
};
export const callVisibilityHandler = async (visible: boolean): Promise<void> => {
Object.defineProperty(document, 'visibilityState', {
value: visible ? 'visible' : 'hidden',
writable: true,
});
const mock = vi.mocked(global.document.addEventListener).mock;
for (const [evt, cb] of mock.calls) {
if (evt === 'visibilitychange' && typeof cb === 'function') {
await (cb as EventListener | ((_: unknown) => Promise<void>))(new Event('foo'));
}
}
};
export const createSlotHost = (options?: {
slot?: HTMLSlotElement;
children?: HTMLElement[];
@@ -453,6 +474,7 @@ export const createParent = (options?: { children?: HTMLElement[] }): HTMLElemen
export const createLitElement = (): LitElement => {
const element = document.createElement('div') as unknown as LitElement;
element.addController = vi.fn();
element.removeController = vi.fn();
element.requestUpdate = vi.fn();
const promise: Promise<boolean> = new Promise((resolve) => {