1+ import { act , render as renderScreen , type Screen } from "../../../test/render" ;
12import {
23 type ContributionStatusEntry ,
34 EnvironmentId ,
@@ -6,24 +7,17 @@ import {
67 ProviderSessionId ,
78 ThreadId ,
89} from "@t3tools/contracts" ;
9- import { act } from "react" ;
10- import { createRoot , type Root } from "react-dom/client" ;
11- import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
12-
10+ import { describe , expect , it , vi } from "vite-plus/test" ;
1311const testState = vi . hoisted ( ( ) => ( {
1412 entries : [ ] as ReadonlyArray < ContributionStatusEntry > ,
1513} ) ) ;
16-
1714vi . mock ( "../../state/contributionStatus" , ( ) => ( {
1815 useThreadContributionStatus : ( ) => testState . entries ,
1916} ) ) ;
20-
2117import { ThreadContributionStatus } from "./ThreadContributionStatus" ;
22-
2318const THREAD = ThreadId . make ( "thread-1" ) ;
2419const ENVIRONMENT = EnvironmentId . make ( "environment-1" ) ;
2520const PI_ORIGIN = "From a Pi extension. It can lag a session change." ;
26-
2721const entry = ( items : ContributionStatusEntry [ "items" ] ) : ContributionStatusEntry => ( {
2822 threadId : THREAD ,
2923 source : {
@@ -34,108 +28,95 @@ const entry = (items: ContributionStatusEntry["items"]): ContributionStatusEntry
3428 } ,
3529 items,
3630} ) ;
37-
38- let root : Root ;
39- let container : HTMLDivElement ;
40-
41- beforeEach ( ( ) => {
42- vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
43- container = document . createElement ( "div" ) ;
44- document . body . append ( container ) ;
45- root = createRoot ( container ) ;
46- } ) ;
47-
48- afterEach ( async ( ) => {
49- await act ( async ( ) => root . unmount ( ) ) ;
50- container . remove ( ) ;
51- testState . entries = [ ] ;
52- vi . unstubAllGlobals ( ) ;
53- } ) ;
54-
55- async function renderStatus ( entries : ReadonlyArray < ContributionStatusEntry > ) {
31+ async function renderStatus ( screen : Screen , entries : ReadonlyArray < ContributionStatusEntry > ) {
5632 testState . entries = entries ;
5733 await act ( async ( ) => {
58- root . render ( < ThreadContributionStatus environmentId = { ENVIRONMENT } threadId = { THREAD } /> ) ;
34+ await screen . rerender (
35+ < ThreadContributionStatus environmentId = { ENVIRONMENT } threadId = { THREAD } /> ,
36+ ) ;
5937 } ) ;
6038}
61-
6239/** The trigger's accessible name: its explicit label, else its text. */
6340function accessibleName ( element : Element ) {
6441 return element . getAttribute ( "aria-label" ) ?? element . textContent ?? "" ;
6542}
66-
67- function statusButton ( ) {
68- const button = [ ...container . querySelectorAll ( "button" ) ] . find ( ( candidate ) =>
43+ function statusButton ( screen : Screen ) {
44+ const button = [ ...screen . container . querySelectorAll ( "button" ) ] . find ( ( candidate ) =>
6945 accessibleName ( candidate ) . startsWith ( "Provider status" ) ,
7046 ) ;
7147 if ( ! button ) throw new Error ( "Status button was not rendered" ) ;
7248 return button ;
7349}
74-
7550function popupText ( ) {
7651 return document . querySelector ( '[data-slot="popover-popup"]' ) ?. textContent ?? null ;
7752}
78-
7953describe ( "ThreadContributionStatus" , ( ) => {
8054 it ( "counts statuses past the inline ones and lists every one when tapped" , async ( ) => {
81- await renderStatus ( [
55+ using _setup = await setup ( ) ;
56+ await using screen = await renderScreen ( null , { router : false } ) ;
57+ await renderStatus ( screen , [
8258 entry ( [
8359 { key : "a" , text : "● plan" } ,
8460 { key : "b" , text : "build 3/9" } ,
8561 { key : "c" , text : "lint clean" , tone : "success" } ,
8662 { key : "d" , text : "disk almost full" , tone : "warning" } ,
8763 ] ) ,
8864 ] ) ;
89-
90- const button = statusButton ( ) ;
65+ const button = statusButton ( screen ) ;
9166 expect ( button . textContent ) . toBe ( "● planbuild 3/9+2" ) ;
9267 expect ( popupText ( ) ) . toBeNull ( ) ;
93-
9468 await act ( async ( ) => {
9569 button . dispatchEvent (
9670 new PointerEvent ( "pointerdown" , { bubbles : true , pointerType : "touch" } ) ,
9771 ) ;
9872 button . click ( ) ;
9973 } ) ;
100-
10174 const text = popupText ( ) ;
10275 for ( const status of [ "● plan" , "build 3/9" , "lint clean" , "disk almost full" , PI_ORIGIN ] ) {
10376 expect ( text ) . toContain ( status ) ;
10477 }
10578 } ) ;
106-
10779 it ( "is a focusable native button, so keyboard users reach the same list" , async ( ) => {
108- await renderStatus ( [ entry ( [ { key : "mode" , text : "● plan" } ] ) ] ) ;
109-
110- const button = statusButton ( ) ;
80+ using _setup = await setup ( ) ;
81+ await using screen = await renderScreen ( null , { router : false } ) ;
82+ await renderStatus ( screen , [ entry ( [ { key : "mode" , text : "● plan" } ] ) ] ) ;
83+ const button = statusButton ( screen ) ;
11184 button . focus ( ) ;
11285 expect ( document . activeElement ) . toBe ( button ) ;
11386 expect ( button . tagName ) . toBe ( "BUTTON" ) ;
11487 } ) ;
115-
11688 it ( "names the control with every status text, even when an item has a tooltip" , async ( ) => {
89+ using _setup = await setup ( ) ;
90+ await using screen = await renderScreen ( null , { router : false } ) ;
11791 const longText = `● ${ "x" . repeat ( 78 ) } ` ;
118- await renderStatus ( [
92+ await renderStatus ( screen , [
11993 entry ( [
12094 { key : "a" , text : "Plan" , tooltip : "12 files to change" } ,
12195 { key : "b" , text : longText } ,
12296 { key : "c" , text : "disk almost full" , tone : "warning" } ,
12397 ] ) ,
12498 ] ) ;
125-
126- expect ( accessibleName ( statusButton ( ) ) ) . toBe (
99+ expect ( accessibleName ( statusButton ( screen ) ) ) . toBe (
127100 `Provider status: Plan, ${ longText } , disk almost full` ,
128101 ) ;
129-
130- await act ( async ( ) => statusButton ( ) . click ( ) ) ;
102+ await act ( async ( ) => statusButton ( screen ) . click ( ) ) ;
131103 const text = popupText ( ) ;
132104 expect ( text ) . toContain ( "Plan" ) ;
133105 expect ( text ) . toContain ( "12 files to change" ) ;
134106 expect ( text ) . toContain ( longText ) ;
135107 } ) ;
136-
137108 it ( "renders nothing without statuses" , async ( ) => {
138- await renderStatus ( [ ] ) ;
139- expect ( container . childElementCount ) . toBe ( 0 ) ;
109+ using _setup = await setup ( ) ;
110+ await using screen = await renderScreen ( null , { router : false } ) ;
111+ await renderStatus ( screen , [ ] ) ;
112+ expect ( screen . container . childElementCount ) . toBe ( 0 ) ;
140113 } ) ;
141114} ) ;
115+
116+ async function setup ( ) {
117+ return {
118+ [ Symbol . dispose ] ( ) {
119+ testState . entries = [ ] ;
120+ } ,
121+ } ;
122+ }
0 commit comments