@@ -446,3 +446,119 @@ it("uses chrome tabs with manual keyboard selection, page linkage, and isolated
446446 error . mockRestore ( ) ;
447447 }
448448} ) ;
449+
450+ it . each ( [ false , true ] ) (
451+ "keeps Messages selected for a child without losing exact navigation (compact: %s)" ,
452+ async ( compact ) => {
453+ vi . stubGlobal ( "matchMedia" , ( media : string ) => ( {
454+ media,
455+ matches : compact ,
456+ addEventListener ( ) { } ,
457+ removeEventListener ( ) { } ,
458+ } ) ) ;
459+ const current = createServices ( ) ;
460+ services = current ;
461+ const onSelect = vi . fn ( ) ;
462+ const entries : RegisteredPage [ ] = [
463+ { key : "buzz.me/me" , title : "Me" , placement : "topbar" } ,
464+ { key : "buzz.channels/channels" , title : "Channels" , placement : "topbar" } ,
465+ { key : "buzz.inbox/inbox" , title : "Inbox" , placement : "sidebar" } ,
466+ { key : "example/top" , title : "Top" , placement : "topbar" } ,
467+ { key : "example/tool" , title : "Tool" , placement : "toolbar" } ,
468+ ] . map ( ( entry ) => ( {
469+ ...entry ,
470+ placement : entry . placement as "topbar" | "sidebar" | "toolbar" ,
471+ id : entry . key . slice ( entry . key . indexOf ( "/" ) + 1 ) ,
472+ pluginId : entry . key . slice ( 0 , entry . key . indexOf ( "/" ) ) ,
473+ revision : "one" ,
474+ primary : true ,
475+ component : ( ) => null ,
476+ } ) ) ;
477+ const shell = ( selected : string , pages = entries ) => (
478+ < ToastProvider >
479+ < AppShell
480+ pages = { pages }
481+ selected = { selected }
482+ navigationAttempt = ""
483+ onSelect = { onSelect }
484+ tone = "default"
485+ communities = { current . communities }
486+ accountActions = { current . accountActions }
487+ >
488+ < p > Child content</ p >
489+ </ AppShell >
490+ </ ToastProvider >
491+ ) ;
492+ const { rerender } = render ( shell ( "buzz.inbox/inbox" ) ) ;
493+ if ( compact )
494+ await userEvent . click (
495+ screen . getByRole ( "button" , { name : "Show navigation" } ) ,
496+ ) ;
497+ const inbox = within (
498+ screen . getByRole ( "navigation" , { name : "Pages" } ) ,
499+ ) . getByRole ( "button" , { name : "Inbox" } ) ;
500+ expect ( inbox ) . toHaveAttribute ( "aria-current" , "page" ) ;
501+ if ( compact )
502+ await userEvent . click ( screen . getByRole ( "button" , { name : "More pages" } ) ) ;
503+ const nav = screen . getByRole ( "navigation" , {
504+ name : compact ? "Header pages" : "Topbar pages" ,
505+ } ) ;
506+ const role = compact ? "button" : "tab" ;
507+ const messages = within ( nav ) . getByRole ( role , { name : "Messages" } ) ;
508+ if ( compact ) {
509+ expect ( messages ) . toHaveAttribute ( "aria-current" , "true" ) ;
510+ expect ( screen . queryByRole ( "tabpanel" ) ) . not . toBeInTheDocument ( ) ;
511+ await userEvent . click ( messages ) ;
512+ expect ( onSelect ) . toHaveBeenCalledExactlyOnceWith (
513+ "buzz.channels/channels" ,
514+ ) ;
515+ expect ( document . getElementById ( "main-content" ) ) . toHaveFocus ( ) ;
516+ return ;
517+ }
518+ expect ( messages ) . toHaveAttribute ( "aria-selected" , "true" ) ;
519+ const panel = screen . getByRole ( "tabpanel" , { name : "Messages" } ) ;
520+ expect ( panel ) . toHaveAttribute ( "id" , messages . getAttribute ( "aria-controls" ) ) ;
521+ expect ( panel ) . toHaveAttribute ( "aria-labelledby" , messages . id ) ;
522+ expect ( panel ) . toHaveTextContent ( "Child content" ) ;
523+ await userEvent . click ( messages ) ;
524+ expect ( onSelect ) . toHaveBeenCalledExactlyOnceWith ( "buzz.channels/channels" , {
525+ focus : "preserve" ,
526+ } ) ;
527+ expect ( messages ) . toHaveFocus ( ) ;
528+ onSelect . mockClear ( ) ;
529+ rerender ( shell ( "buzz.channels/channels" ) ) ;
530+ await userEvent . click ( messages ) ;
531+ expect ( onSelect ) . not . toHaveBeenCalled ( ) ;
532+ rerender ( shell ( "example/top" ) ) ;
533+ expect ( within ( nav ) . getByRole ( "tab" , { name : "Top" } ) ) . toHaveAttribute (
534+ "aria-selected" ,
535+ "true" ,
536+ ) ;
537+ rerender ( shell ( "example/tool" ) ) ;
538+ expect (
539+ within ( nav ) . queryByRole ( "tab" , { selected : true } ) ,
540+ ) . not . toBeInTheDocument ( ) ;
541+ expect ( screen . getByRole ( "button" , { name : "Tool" } ) ) . toHaveAttribute (
542+ "aria-current" ,
543+ "page" ,
544+ ) ;
545+ rerender ( shell ( "buzz.me/me" ) ) ;
546+ expect ( within ( nav ) . getByRole ( "tab" , { name : "Me" } ) ) . toHaveAttribute (
547+ "aria-selected" ,
548+ "true" ,
549+ ) ;
550+ rerender ( shell ( "settings" ) ) ;
551+ expect (
552+ within ( nav ) . queryByRole ( "tab" , { selected : true } ) ,
553+ ) . not . toBeInTheDocument ( ) ;
554+ rerender (
555+ shell (
556+ "buzz.inbox/inbox" ,
557+ entries . filter ( ( entry ) => entry . key !== "buzz.inbox/inbox" ) ,
558+ ) ,
559+ ) ;
560+ expect (
561+ within ( nav ) . queryByRole ( "tab" , { selected : true } ) ,
562+ ) . not . toBeInTheDocument ( ) ;
563+ } ,
564+ ) ;
0 commit comments