11/** @vitest -environment jsdom */
2- import { renderHook } from "@testing-library/react" ;
2+ import { cleanup , renderHook } from "@testing-library/react" ;
33import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
44
55const isTauriMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
66const getCurrentWindowMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
7- const isWindowsPlatformMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
87
98vi . mock ( "@tauri-apps/api/core" , ( ) => ( {
109 isTauri : isTauriMock ,
@@ -14,10 +13,6 @@ vi.mock("@tauri-apps/api/window", () => ({
1413 getCurrentWindow : getCurrentWindowMock ,
1514} ) ) ;
1615
17- vi . mock ( "@utils/platformPaths" , ( ) => ( {
18- isWindowsPlatform : isWindowsPlatformMock ,
19- } ) ) ;
20-
2116import { useWindowDrag } from "./useWindowDrag" ;
2217
2318function setRect ( el : Element , rect : Pick < DOMRect , "left" | "right" | "top" | "bottom" > ) {
@@ -43,12 +38,11 @@ describe("useWindowDrag", () => {
4338 } ) ;
4439
4540 afterEach ( ( ) => {
41+ cleanup ( ) ;
4642 document . body . innerHTML = "" ;
4743 } ) ;
4844
4945 it ( "starts dragging on Windows when click is inside a drag zone" , ( ) => {
50- isWindowsPlatformMock . mockReturnValue ( true ) ;
51-
5246 const titlebar = document . createElement ( "div" ) ;
5347 titlebar . id = "titlebar" ;
5448 document . body . appendChild ( titlebar ) ;
@@ -70,9 +64,82 @@ describe("useWindowDrag", () => {
7064 expect ( startDragging ) . toHaveBeenCalledTimes ( 1 ) ;
7165 } ) ;
7266
73- it ( "does not start dragging when clicking an interactive role target" , ( ) => {
74- isWindowsPlatformMock . mockReturnValue ( true ) ;
67+ it ( "starts dragging on Windows when click is inside the main topbar" , ( ) => {
68+ const topbar = document . createElement ( "div" ) ;
69+ topbar . className = "main-topbar" ;
70+ document . body . appendChild ( topbar ) ;
71+ setRect ( topbar , { left : 0 , top : 0 , right : 800 , bottom : 44 } ) ;
72+
73+ renderHook ( ( ) => useWindowDrag ( "titlebar" ) ) ;
74+
75+ const target = document . createElement ( "span" ) ;
76+ topbar . appendChild ( target ) ;
77+ target . dispatchEvent (
78+ new MouseEvent ( "mousedown" , {
79+ bubbles : true ,
80+ button : 0 ,
81+ clientX : 120 ,
82+ clientY : 20 ,
83+ } ) ,
84+ ) ;
85+
86+ expect ( startDragging ) . toHaveBeenCalledTimes ( 1 ) ;
87+ } ) ;
7588
89+ it ( "starts dragging on Windows when mousedown target is a text node in topbar" , ( ) => {
90+ const topbar = document . createElement ( "div" ) ;
91+ topbar . className = "main-topbar" ;
92+ document . body . appendChild ( topbar ) ;
93+ setRect ( topbar , { left : 0 , top : 0 , right : 800 , bottom : 44 } ) ;
94+
95+ const label = document . createElement ( "span" ) ;
96+ label . textContent = "Project Name" ;
97+ topbar . appendChild ( label ) ;
98+
99+ renderHook ( ( ) => useWindowDrag ( "titlebar" ) ) ;
100+
101+ const textNode = label . firstChild ;
102+ expect ( textNode ) . toBeTruthy ( ) ;
103+ textNode ?. dispatchEvent (
104+ new MouseEvent ( "mousedown" , {
105+ bubbles : true ,
106+ button : 0 ,
107+ clientX : 140 ,
108+ clientY : 20 ,
109+ } ) ,
110+ ) ;
111+
112+ expect ( startDragging ) . toHaveBeenCalledTimes ( 1 ) ;
113+ } ) ;
114+
115+ it ( "does not start dragging when text node is inside an interactive target" , ( ) => {
116+ const topbar = document . createElement ( "div" ) ;
117+ topbar . className = "main-topbar" ;
118+ document . body . appendChild ( topbar ) ;
119+ setRect ( topbar , { left : 0 , top : 0 , right : 800 , bottom : 44 } ) ;
120+
121+ const button = document . createElement ( "button" ) ;
122+ button . type = "button" ;
123+ button . textContent = "Terminal" ;
124+ topbar . appendChild ( button ) ;
125+
126+ renderHook ( ( ) => useWindowDrag ( "titlebar" ) ) ;
127+
128+ const textNode = button . firstChild ;
129+ expect ( textNode ) . toBeTruthy ( ) ;
130+ textNode ?. dispatchEvent (
131+ new MouseEvent ( "mousedown" , {
132+ bubbles : true ,
133+ button : 0 ,
134+ clientX : 200 ,
135+ clientY : 20 ,
136+ } ) ,
137+ ) ;
138+
139+ expect ( startDragging ) . not . toHaveBeenCalled ( ) ;
140+ } ) ;
141+
142+ it ( "does not start dragging when clicking an interactive role target" , ( ) => {
76143 const sidebarDragStrip = document . createElement ( "div" ) ;
77144 sidebarDragStrip . className = "sidebar-drag-strip" ;
78145 document . body . appendChild ( sidebarDragStrip ) ;
@@ -96,8 +163,6 @@ describe("useWindowDrag", () => {
96163 } ) ;
97164
98165 it ( "does not start dragging when click is outside all drag zones" , ( ) => {
99- isWindowsPlatformMock . mockReturnValue ( true ) ;
100-
101166 const titlebar = document . createElement ( "div" ) ;
102167 titlebar . id = "titlebar" ;
103168 document . body . appendChild ( titlebar ) ;
@@ -119,19 +184,20 @@ describe("useWindowDrag", () => {
119184 expect ( startDragging ) . not . toHaveBeenCalled ( ) ;
120185 } ) ;
121186
122- it ( "starts dragging on non-Windows via titlebar listener" , ( ) => {
123- isWindowsPlatformMock . mockReturnValue ( false ) ;
124-
187+ it ( "starts dragging via titlebar drag zone" , ( ) => {
125188 const titlebar = document . createElement ( "div" ) ;
126189 titlebar . id = "titlebar" ;
127190 document . body . appendChild ( titlebar ) ;
191+ setRect ( titlebar , { left : 0 , top : 0 , right : 300 , bottom : 44 } ) ;
128192
129193 renderHook ( ( ) => useWindowDrag ( "titlebar" ) ) ;
130194
131195 titlebar . dispatchEvent (
132196 new MouseEvent ( "mousedown" , {
133197 bubbles : true ,
134198 button : 0 ,
199+ clientX : 12 ,
200+ clientY : 12 ,
135201 } ) ,
136202 ) ;
137203
0 commit comments