mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-06 16:08:05 +00:00
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { setContext } from "@/platform/context";
|
|
import { EDITOR_AREA_SELECTOR, installFocusTracking } from "./focus";
|
|
|
|
describe("focus after popups", () => {
|
|
afterEach(() => {
|
|
document.body.innerHTML = "";
|
|
setContext("popupOpen", false);
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
function setup() {
|
|
document.body.innerHTML = `<div data-editor-area><div class="cm-content" tabindex="0"></div></div><button id="other">x</button>`;
|
|
return document.querySelector<HTMLElement>(`${EDITOR_AREA_SELECTOR} .cm-content`)!;
|
|
}
|
|
|
|
it("returns focus to the editor when the last popup closes and focus was left nowhere", () => {
|
|
vi.useFakeTimers();
|
|
const editor = setup();
|
|
const dispose = installFocusTracking(() => {});
|
|
setContext("popupOpen", true);
|
|
(document.activeElement as HTMLElement | null)?.blur();
|
|
setContext("popupOpen", false);
|
|
vi.runAllTimers();
|
|
expect(document.activeElement).toBe(editor);
|
|
dispose();
|
|
});
|
|
|
|
it("leaves focus alone when the popup handed it to another component", () => {
|
|
vi.useFakeTimers();
|
|
setup();
|
|
const dispose = installFocusTracking(() => {});
|
|
setContext("popupOpen", true);
|
|
const other = document.getElementById("other")!;
|
|
other.focus();
|
|
setContext("popupOpen", false);
|
|
vi.runAllTimers();
|
|
expect(document.activeElement).toBe(other);
|
|
dispose();
|
|
});
|
|
});
|