|
| 1 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { type HostFocusElement, keepHostFocusDuringGuestInput } from "./previewHostFocus"; |
| 4 | + |
| 5 | +const TAB = "runtime-tab"; |
| 6 | + |
| 7 | +const element = (tagName: string, previewTab: string | null = null) => { |
| 8 | + const focus = vi.fn<(options?: FocusOptions) => void>(); |
| 9 | + const blur = vi.fn<() => void>(); |
| 10 | + let connected = true; |
| 11 | + const node = { |
| 12 | + tagName, |
| 13 | + get isConnected() { |
| 14 | + return connected; |
| 15 | + }, |
| 16 | + getAttribute: (name: string) => (name === "data-preview-tab" ? previewTab : null), |
| 17 | + focus, |
| 18 | + blur, |
| 19 | + } satisfies HostFocusElement; |
| 20 | + const detach = () => { |
| 21 | + connected = false; |
| 22 | + }; |
| 23 | + return { node, focus, blur, detach }; |
| 24 | +}; |
| 25 | + |
| 26 | +const focusTracker = (initial: HostFocusElement | null) => { |
| 27 | + let active = initial; |
| 28 | + return { |
| 29 | + read: () => active, |
| 30 | + move: (next: HostFocusElement | null) => { |
| 31 | + active = next; |
| 32 | + }, |
| 33 | + }; |
| 34 | +}; |
| 35 | + |
| 36 | +describe("keepHostFocusDuringGuestInput", () => { |
| 37 | + it("returns focus to the composer when the click moved it into the clicked webview", async () => { |
| 38 | + const composer = element("TEXTAREA"); |
| 39 | + const webview = element("WEBVIEW", TAB); |
| 40 | + const focus = focusTracker(composer.node); |
| 41 | + |
| 42 | + const result = await keepHostFocusDuringGuestInput(TAB, focus.read, async () => { |
| 43 | + focus.move(webview.node); |
| 44 | + return "clicked"; |
| 45 | + }); |
| 46 | + |
| 47 | + expect(result).toBe("clicked"); |
| 48 | + expect(composer.focus).toHaveBeenCalledWith({ preventScroll: true }); |
| 49 | + expect(webview.blur).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("blurs the webview when nothing in the app had focus before the click", async () => { |
| 53 | + const body = element("BODY"); |
| 54 | + const webview = element("WEBVIEW", TAB); |
| 55 | + const focus = focusTracker(body.node); |
| 56 | + |
| 57 | + await keepHostFocusDuringGuestInput(TAB, focus.read, async () => { |
| 58 | + focus.move(webview.node); |
| 59 | + }); |
| 60 | + |
| 61 | + expect(webview.blur).toHaveBeenCalledOnce(); |
| 62 | + expect(body.focus).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("blurs the webview when the previously focused element left the page", async () => { |
| 66 | + const composer = element("TEXTAREA"); |
| 67 | + const webview = element("WEBVIEW", TAB); |
| 68 | + const focus = focusTracker(composer.node); |
| 69 | + |
| 70 | + await keepHostFocusDuringGuestInput(TAB, focus.read, async () => { |
| 71 | + composer.detach(); |
| 72 | + focus.move(webview.node); |
| 73 | + }); |
| 74 | + |
| 75 | + expect(composer.focus).not.toHaveBeenCalled(); |
| 76 | + expect(webview.blur).toHaveBeenCalledOnce(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("leaves focus in the page when the user was already typing there", async () => { |
| 80 | + const webview = element("WEBVIEW", TAB); |
| 81 | + const focus = focusTracker(webview.node); |
| 82 | + |
| 83 | + await keepHostFocusDuringGuestInput(TAB, focus.read, async () => undefined); |
| 84 | + |
| 85 | + expect(webview.focus).not.toHaveBeenCalled(); |
| 86 | + expect(webview.blur).not.toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("keeps a selection the user made somewhere else during the click", async () => { |
| 90 | + const composer = element("TEXTAREA"); |
| 91 | + const search = element("INPUT"); |
| 92 | + const focus = focusTracker(composer.node); |
| 93 | + |
| 94 | + await keepHostFocusDuringGuestInput(TAB, focus.read, async () => { |
| 95 | + focus.move(search.node); |
| 96 | + }); |
| 97 | + |
| 98 | + expect(composer.focus).not.toHaveBeenCalled(); |
| 99 | + expect(search.blur).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("ignores focus in a different preview tab", async () => { |
| 103 | + const composer = element("TEXTAREA"); |
| 104 | + const otherWebview = element("WEBVIEW", "other-tab"); |
| 105 | + const focus = focusTracker(composer.node); |
| 106 | + |
| 107 | + await keepHostFocusDuringGuestInput(TAB, focus.read, async () => { |
| 108 | + focus.move(otherWebview.node); |
| 109 | + }); |
| 110 | + |
| 111 | + expect(composer.focus).not.toHaveBeenCalled(); |
| 112 | + expect(otherWebview.blur).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("restores focus when the click fails and rethrows the failure", async () => { |
| 116 | + const composer = element("TEXTAREA"); |
| 117 | + const webview = element("WEBVIEW", TAB); |
| 118 | + const focus = focusTracker(composer.node); |
| 119 | + |
| 120 | + await expect( |
| 121 | + keepHostFocusDuringGuestInput(TAB, focus.read, async () => { |
| 122 | + focus.move(webview.node); |
| 123 | + throw new Error("click failed"); |
| 124 | + }), |
| 125 | + ).rejects.toThrow("click failed"); |
| 126 | + |
| 127 | + expect(composer.focus).toHaveBeenCalledWith({ preventScroll: true }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments