|
| 1 | +/** |
| 2 | + * Tab-strip middle-click close tests. The press target is recorded on middle |
| 3 | + * mousedown (preventDefaulted to disarm Chrome's middle-click autoscroll) |
| 4 | + * and the close settles on the first middle mouseup OVER that same tab — |
| 5 | + * release-position semantics matching VS Code (microsoft/vscode#101028) and |
| 6 | + * user expectations for Chrome tabs (crbug/40679924): pressing on a tab and |
| 7 | + * releasing elsewhere cancels. The browser dispatches auxclick to the common |
| 8 | + * ancestor of the press/release targets when they differ (or suppresses it |
| 9 | + * entirely), so settling on the recorded press target keeps release |
| 10 | + * semantics without depending on auxclick delivery. Left-button paths |
| 11 | + * (activate / drag) and wheel scrolling are untouched. |
| 12 | + */ |
| 13 | +// @vitest-environment jsdom |
| 14 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 15 | +import { createElement } from 'react' |
| 16 | +import { createRoot, type Root } from 'react-dom/client' |
| 17 | +import { act } from 'react-dom/test-utils' |
| 18 | + |
| 19 | +// The act() environment flag (React 18.2 reads it before flushing effects). |
| 20 | +;(globalThis as Record<string, unknown>).IS_REACT_ACT_ENVIRONMENT = true |
| 21 | + |
| 22 | +import { TabBar } from '../src/client/TabBar.tsx' |
| 23 | +import type { SidebarTab } from '../src/client/state.ts' |
| 24 | + |
| 25 | +function mountBar(): { |
| 26 | + tab0: HTMLElement |
| 27 | + tab1: HTMLElement |
| 28 | + onClose: ReturnType<typeof vi.fn> |
| 29 | + onActivate: ReturnType<typeof vi.fn> |
| 30 | + unmount: () => void |
| 31 | +} { |
| 32 | + const container = document.createElement('div') |
| 33 | + document.body.append(container) |
| 34 | + const onClose = vi.fn() |
| 35 | + const onActivate = vi.fn() |
| 36 | + const tabs: SidebarTab[] = [ |
| 37 | + { id: 't1', type: 'explorer', title: 'Explorer' }, |
| 38 | + { id: 't2', type: 'git', title: 'Git' }, |
| 39 | + ] |
| 40 | + const root: Root = createRoot(container) |
| 41 | + act(() => { |
| 42 | + root.render(createElement(TabBar, { |
| 43 | + paneId: 'pane:1', |
| 44 | + tabs, |
| 45 | + active: 't1', |
| 46 | + onActivate, |
| 47 | + onClose, |
| 48 | + onNewTab: () => {}, |
| 49 | + newTabOptions: [], |
| 50 | + onDropTab: () => {}, |
| 51 | + })) |
| 52 | + }) |
| 53 | + const tabEls = [...container.querySelectorAll('[class*="tabList"] > [class*="tab"]')] as HTMLElement[] |
| 54 | + expect(tabEls.length).toBe(2) |
| 55 | + // noUncheckedIndexedAccess: the length assertion above is the guard, but |
| 56 | + // the compiler still sees `HTMLElement | undefined` through indexing. |
| 57 | + const tab0 = tabEls[0]! |
| 58 | + const tab1 = tabEls[1]! |
| 59 | + return { |
| 60 | + tab0, |
| 61 | + tab1, |
| 62 | + onClose, |
| 63 | + onActivate, |
| 64 | + unmount: () => { |
| 65 | + act(() => { root.unmount() }) |
| 66 | + container.remove() |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** Dispatch a native mouse event; returns the event (to read defaultPrevented). */ |
| 72 | +function mouse(target: EventTarget, type: string, button: number): MouseEvent { |
| 73 | + const event = new MouseEvent(type, { bubbles: true, cancelable: true, button }) |
| 74 | + target.dispatchEvent(event) |
| 75 | + return event |
| 76 | +} |
| 77 | + |
| 78 | +afterEach(() => { |
| 79 | + document.body.innerHTML = '' |
| 80 | + vi.restoreAllMocks() |
| 81 | +}) |
| 82 | + |
| 83 | +describe('TabBar middle-click close', () => { |
| 84 | + it('closes when the middle mouseup lands on the same tab', () => { |
| 85 | + const { tab0, onClose, unmount } = mountBar() |
| 86 | + try { |
| 87 | + mouse(tab0, 'mousedown', 1) |
| 88 | + mouse(tab0, 'mouseup', 1) |
| 89 | + expect(onClose).toHaveBeenCalledWith('t1') |
| 90 | + } finally { |
| 91 | + unmount() |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + it('preventDefaults the middle mousedown (disarms browser autoscroll)', () => { |
| 96 | + const { tab0, unmount } = mountBar() |
| 97 | + try { |
| 98 | + const event = mouse(tab0, 'mousedown', 1) |
| 99 | + expect(event.defaultPrevented).toBe(true) |
| 100 | + } finally { |
| 101 | + unmount() |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + it('a release elsewhere (drag-away) cancels the close — one-shot', () => { |
| 106 | + const { tab0, onClose, unmount } = mountBar() |
| 107 | + try { |
| 108 | + // Press on tab 0, release on unrelated element: no close. |
| 109 | + mouse(tab0, 'mousedown', 1) |
| 110 | + mouse(document.body, 'mouseup', 1) |
| 111 | + expect(onClose).not.toHaveBeenCalled() |
| 112 | + // A later middle mouseup over the tab (no new press) must NOT close. |
| 113 | + mouse(tab0, 'mouseup', 1) |
| 114 | + expect(onClose).not.toHaveBeenCalled() |
| 115 | + } finally { |
| 116 | + unmount() |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + it('a release over a DIFFERENT tab does not close', () => { |
| 121 | + const { tab0, tab1, onClose, unmount } = mountBar() |
| 122 | + try { |
| 123 | + mouse(tab0, 'mousedown', 1) |
| 124 | + mouse(tab1, 'mouseup', 1) |
| 125 | + expect(onClose).not.toHaveBeenCalled() |
| 126 | + } finally { |
| 127 | + unmount() |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + it('middle mousedown on the close button closes (release over the tab)', () => { |
| 132 | + const { tab0, onClose, unmount } = mountBar() |
| 133 | + try { |
| 134 | + const button = tab0.querySelector('button') as HTMLButtonElement |
| 135 | + expect(button).not.toBeNull() |
| 136 | + mouse(button, 'mousedown', 1) |
| 137 | + mouse(button, 'mouseup', 1) |
| 138 | + expect(onClose).toHaveBeenCalledWith('t1') |
| 139 | + } finally { |
| 140 | + unmount() |
| 141 | + } |
| 142 | + }) |
| 143 | + |
| 144 | + it('left mousedown does not close; left click activates', () => { |
| 145 | + const { tab0, onClose, onActivate, unmount } = mountBar() |
| 146 | + try { |
| 147 | + mouse(tab0, 'mousedown', 0) |
| 148 | + mouse(tab0, 'mouseup', 0) |
| 149 | + expect(onClose).not.toHaveBeenCalled() |
| 150 | + mouse(tab0, 'click', 0) |
| 151 | + expect(onActivate).toHaveBeenCalledWith('t1') |
| 152 | + } finally { |
| 153 | + unmount() |
| 154 | + } |
| 155 | + }) |
| 156 | + |
| 157 | + it('a middle auxclick alone (no mousedown) does not close', () => { |
| 158 | + // Pins the contract: the close settles on the mouseup of the recorded |
| 159 | + // press, not on auxclick delivery — the browser redirects auxclick to |
| 160 | + // the common ancestor (or suppresses it) when press/release targets |
| 161 | + // differ, which was the failure mode. |
| 162 | + const { tab0, onClose, unmount } = mountBar() |
| 163 | + try { |
| 164 | + mouse(tab0, 'auxclick', 1) |
| 165 | + expect(onClose).not.toHaveBeenCalled() |
| 166 | + } finally { |
| 167 | + unmount() |
| 168 | + } |
| 169 | + }) |
| 170 | + |
| 171 | + it('a left mouseup after a middle press does not consume the press', () => { |
| 172 | + const { tab0, onClose, unmount } = mountBar() |
| 173 | + try { |
| 174 | + mouse(tab0, 'mousedown', 1) |
| 175 | + mouse(window, 'mouseup', 0) |
| 176 | + expect(onClose).not.toHaveBeenCalled() |
| 177 | + mouse(tab0, 'mouseup', 1) |
| 178 | + expect(onClose).toHaveBeenCalledWith('t1') |
| 179 | + } finally { |
| 180 | + unmount() |
| 181 | + } |
| 182 | + }) |
| 183 | + |
| 184 | + it('stops closing after unmount', () => { |
| 185 | + const { tab0, onClose, unmount } = mountBar() |
| 186 | + mouse(tab0, 'mousedown', 1) |
| 187 | + unmount() |
| 188 | + mouse(tab0, 'mouseup', 1) |
| 189 | + expect(onClose).not.toHaveBeenCalled() |
| 190 | + }) |
| 191 | +}) |
0 commit comments