1- import * as NodeChildProcess from "node:child_process" ;
2- import * as NodeFS from "node:fs" ;
3- import * as NodeOS from "node:os" ;
4- import * as NodePath from "node:path" ;
5- import type { ReactNode } from "react" ;
6- import { renderToStaticMarkup } from "react-dom/server" ;
1+ import { act , type ReactNode } from "react" ;
2+ import { createRoot } from "react-dom/client" ;
73import { expect , it , vi } from "vite-plus/test" ;
84
95const checkout = vi . hoisted ( ( ) => ( {
@@ -59,60 +55,32 @@ vi.mock("../../../state/vcs", () => ({ vcsEnvironment: { status: () => null } })
5955import { GitBranchesSheet } from "./GitBranchesSheet" ;
6056
6157it ( "recognizes both the main checkout and a linked worktree as this thread's checkout" , ( ) => {
62- const directory = NodeFS . realpathSync (
63- NodeFS . mkdtempSync ( NodePath . join ( NodeOS . tmpdir ( ) , "branch-occupancy-" ) ) ,
64- ) ;
65- const git = ( ...args : string [ ] ) =>
66- NodeChildProcess . execFileSync ( "git" , [ "-c" , "commit.gpgsign=false" , ...args ] , {
67- cwd : directory ,
68- encoding : "utf8" ,
69- stdio : "pipe" ,
70- } ) ;
71- try {
72- git ( "init" , "--initial-branch=main" ) ;
73- git (
74- "-c" ,
75- "user.name=Test" ,
76- "-c" ,
77- 78- "commit" ,
79- "--allow-empty" ,
80- "-m" ,
81- "initial" ,
82- ) ;
83- const linked = NodePath . join ( directory , "linked" ) ;
84- git ( "worktree" , "add" , "-b" , "feature/linked" , linked ) ;
85- git ( "branch" , "feature/available" ) ;
86- checkout . projectRoot = directory ;
87- checkout . branches = git (
88- "for-each-ref" ,
89- "--format=%(refname:short)%00%(worktreepath)" ,
90- "refs/heads" ,
91- )
92- . trimEnd ( )
93- . split ( "\n" )
94- . map ( ( line ) => {
95- const [ name = "" , worktreePath ] = line . split ( "\0" ) ;
96- return { name, worktreePath : worktreePath || null } ;
97- } ) ;
98-
99- const rows = ( ) => {
100- const html = renderToStaticMarkup (
58+ const directory = "/repo" ;
59+ const linked = "/repo-linked" ;
60+ checkout . projectRoot = directory ;
61+ checkout . branches = [
62+ { name : "feature/available" , worktreePath : null } ,
63+ { name : "feature/linked" , worktreePath : linked } ,
64+ { name : "main" , worktreePath : directory } ,
65+ ] ;
66+ ( globalThis as { IS_REACT_ACT_ENVIRONMENT ?: boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
67+ const container = document . createElement ( "div" ) ;
68+ const root = createRoot ( container ) ;
69+ const rows = ( ) => {
70+ act ( ( ) => {
71+ root . render (
10172 < GitBranchesSheet
102- route = { {
103- params : { environmentId : "environment-1" , threadId : "thread-1" } ,
104- } }
73+ route = { { params : { environmentId : "environment-1" , threadId : "thread-1" } } }
10574 /> ,
10675 ) ;
107- return [
108- ...html . matchAll ( / < b u t t o n ( [ ^ > ] * ) > < s p a n > ( .* ?) < \/ s p a n > < s p a n > ( .* ?) < \/ s p a n > < \/ b u t t o n > / g) ,
109- ] . map ( ( [ , attributes , name , subtitle ] ) => ( {
110- name,
111- subtitle,
112- disabled : attributes ?. includes ( "disabled" ) ,
113- } ) ) ;
114- } ;
115-
76+ } ) ;
77+ return [ ...container . querySelectorAll ( "button" ) ] . map ( ( button ) => ( {
78+ name : button . querySelectorAll ( "span" ) [ 0 ] ?. textContent ,
79+ subtitle : button . querySelectorAll ( "span" ) [ 1 ] ?. textContent ,
80+ disabled : button . disabled ,
81+ } ) ) ;
82+ } ;
83+ try {
11684 checkout . worktreePath = null ;
11785 expect ( rows ( ) ) . toEqual ( [
11886 { name : "feature/available" , subtitle : "Local branch" , disabled : false } ,
@@ -127,6 +95,6 @@ it("recognizes both the main checkout and a linked worktree as this thread's che
12795 { name : "main" , subtitle : "Checked out in another worktree" , disabled : true } ,
12896 ] ) ;
12997 } finally {
130- NodeFS . rmSync ( directory , { recursive : true , force : true } ) ;
98+ act ( ( ) => root . unmount ( ) ) ;
13199 }
132100} ) ;
0 commit comments