1- import { describe , expect , it } from "vite-plus/test" ;
1+ // @vitest -environment jsdom
22
3+ import { act } from "react" ;
4+ import { createRoot , type Root } from "react-dom/client" ;
5+ import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
6+
7+ import { Sidebar , SidebarProvider , SidebarTrigger } from "./sidebar" ;
38import { resolveSidebarState } from "./sidebarState" ;
49
10+ let root : Root ;
11+ let container : HTMLDivElement ;
12+
13+ beforeEach ( ( ) => {
14+ vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
15+ vi . stubGlobal ( "matchMedia" , ( query : string ) => ( {
16+ matches : false ,
17+ media : query ,
18+ addEventListener : vi . fn ( ) ,
19+ removeEventListener : vi . fn ( ) ,
20+ } ) ) ;
21+ container = document . createElement ( "div" ) ;
22+ document . body . append ( container ) ;
23+ root = createRoot ( container ) ;
24+ } ) ;
25+
26+ afterEach ( async ( ) => {
27+ await act ( async ( ) => root . unmount ( ) ) ;
28+ container . remove ( ) ;
29+ vi . unstubAllGlobals ( ) ;
30+ } ) ;
31+
32+ async function renderSidebar ( open : boolean ) {
33+ await act ( async ( ) => {
34+ root . render (
35+ < SidebarProvider open = { open } >
36+ < Sidebar >
37+ < SidebarTrigger data-testid = "internal-trigger" />
38+ < button data-testid = "sidebar-control" > Thread</ button >
39+ </ Sidebar >
40+ < SidebarTrigger data-testid = "external-trigger" />
41+ < button data-testid = "content-control" > Content</ button >
42+ </ SidebarProvider > ,
43+ ) ;
44+ } ) ;
45+ }
46+
47+ function element ( selector : string ) {
48+ const node = container . querySelector < HTMLElement > ( selector ) ;
49+ if ( ! node ) throw new Error ( `Missing element: ${ selector } ` ) ;
50+ return node ;
51+ }
52+
553describe ( "responsive sidebar state" , ( ) => {
654 it ( "uses mobile sheet visibility for the shared responsive state" , ( ) => {
755 expect ( resolveSidebarState ( { isMobile : true , open : true , openMobile : false } ) ) . toBe (
@@ -13,3 +61,35 @@ describe("responsive sidebar state", () => {
1361 ) ;
1462 } ) ;
1563} ) ;
64+
65+ describe ( "desktop sidebar focus" , ( ) => {
66+ it ( "moves focus to the external toggle on collapse and restores sidebar access on expansion" , async ( ) => {
67+ await renderSidebar ( true ) ;
68+ const control = element ( '[data-testid="sidebar-control"]' ) ;
69+ const sidebar = element ( '[data-slot="sidebar"]' ) ;
70+ control . focus ( ) ;
71+ expect ( document . activeElement ) . toBe ( control ) ;
72+
73+ await renderSidebar ( false ) ;
74+
75+ expect ( document . activeElement ) . toBe ( element ( '[data-testid="external-trigger"]' ) ) ;
76+ expect ( sidebar . hasAttribute ( "inert" ) ) . toBe ( true ) ;
77+
78+ await renderSidebar ( true ) ;
79+
80+ expect ( sidebar . hasAttribute ( "inert" ) ) . toBe ( false ) ;
81+ control . focus ( ) ;
82+ expect ( document . activeElement ) . toBe ( control ) ;
83+ } ) ;
84+
85+ it ( "keeps focus in the content when the sidebar collapses" , async ( ) => {
86+ await renderSidebar ( true ) ;
87+ const control = element ( '[data-testid="content-control"]' ) ;
88+ control . focus ( ) ;
89+ expect ( document . activeElement ) . toBe ( control ) ;
90+
91+ await renderSidebar ( false ) ;
92+
93+ expect ( document . activeElement ) . toBe ( control ) ;
94+ } ) ;
95+ } ) ;
0 commit comments