1- import { FileTree } from "@pierre/trees/react" ;
2- import type { FileTree as FileTreeModel } from "@pierre/trees" ;
31import { EnvironmentId , type ProjectEntry } from "@t3tools/contracts" ;
42import { act , type ReactNode } from "react" ;
5- import { create , type ReactTestRenderer } from "react-test-renderer " ;
3+ import { createRoot , type Root } from "react-dom/client " ;
64import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
75
8- import { InputGroupInput } from "~/components/ui/input-group" ;
96import FileBrowserPanel from "./FileBrowserPanel" ;
107
118const files = vi . hoisted ( ( ) => ( {
129 directoryEntries : [ ] as ProjectEntry [ ] ,
1310 searchEntries : [ ] as ProjectEntry [ ] ,
1411 searchPending : false ,
1512 searchError : null as string | null ,
13+ loadingDirectories : new Set < string > ( ) as ReadonlySet < string > ,
1614 load : vi . fn ( async ( ) => { } ) ,
1715 refresh : vi . fn ( ) ,
1816} ) ) ;
1917
2018vi . mock ( "./useDirectoryEntries" , ( ) => ( {
2119 useDirectoryEntries : ( ) => ( {
2220 entries : files . directoryEntries ,
21+ loadingDirectories : files . loadingDirectories ,
2322 load : files . load ,
2423 refresh : files . refresh ,
2524 ready : true ,
@@ -50,7 +49,8 @@ vi.mock("~/components/ui/tooltip", () => ({
5049} ) ) ;
5150
5251describe ( "Files panel search" , ( ) => {
53- let renderer : ReactTestRenderer | undefined ;
52+ let root : Root | undefined ;
53+ let container : HTMLDivElement | undefined ;
5454 const props = {
5555 environmentId : EnvironmentId . make ( "test-environment" ) ,
5656 cwd : "/workspace" ,
@@ -62,36 +62,47 @@ describe("Files panel search", () => {
6262 } ;
6363
6464 async function render ( ) {
65- await act ( async ( ) => {
66- if ( renderer ) renderer . update ( < FileBrowserPanel { ...props } /> ) ;
67- else renderer = create ( < FileBrowserPanel { ...props } /> ) ;
68- } ) ;
65+ if ( ! container ) {
66+ container = document . body . appendChild ( document . createElement ( "div" ) ) ;
67+ // The tree virtualizes its rows, so it needs a real viewport to render any.
68+ container . style . cssText =
69+ "display: flex; flex-direction: column; width: 320px; height: 480px;" ;
70+ root = createRoot ( container ) ;
71+ }
72+ await act ( async ( ) => root ! . render ( < FileBrowserPanel { ...props } /> ) ) ;
73+ }
74+
75+ function searchInput ( ) {
76+ const input = container ! . querySelector < HTMLInputElement > ( 'input[type="search"]' ) ;
77+ expect ( input ) . not . toBeNull ( ) ;
78+ return input ! ;
6979 }
7080
7181 async function search ( value : string ) {
82+ const input = searchInput ( ) ;
83+ // React tracks the last value it saw on the element, so set it through the native setter.
84+ Object . getOwnPropertyDescriptor ( HTMLInputElement . prototype , "value" ) ! . set ! . call ( input , value ) ;
7285 await act ( async ( ) => {
73- renderer ! . root . findByType ( InputGroupInput ) . props . onChange ( { target : { value } } ) ;
86+ input . dispatchEvent ( new Event ( "input" , { bubbles : true } ) ) ;
7487 } ) ;
7588 }
7689
7790 function visiblePaths ( ) {
78- const trees = renderer ! . root . findAllByType ( FileTree ) ;
79- if ( trees . length === 0 ) return [ ] ;
80- const model : FileTreeModel = trees [ 0 ] ! . props . model ;
81- return model . getVisibleRows ( 0 , model . getVisibleCount ( ) ) . map ( ( row ) => row . path ) ;
91+ const tree = container ! . querySelector ( "file-tree-container" ) ;
92+ if ( ! tree ) return [ ] ;
93+ return [ ...tree . shadowRoot ! . querySelectorAll ( "[data-item-path]" ) ] . map ( ( row ) =>
94+ row . getAttribute ( "data-item-path" ) ,
95+ ) ;
8296 }
8397
8498 function hasMessage ( message : string ) {
85- return (
86- renderer ! . root . findAll ( ( node ) => node . type === "div" && node . props . children === message )
87- . length > 0
99+ return [ ...container ! . querySelectorAll ( "div" ) ] . some (
100+ ( node ) => node . childNodes . length === 1 && node . textContent === message ,
88101 ) ;
89102 }
90103
91104 beforeEach ( ( ) => {
92- vi . useFakeTimers ( ) ;
93105 vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
94- vi . stubGlobal ( "document" , new EventTarget ( ) ) ;
95106 files . directoryEntries = [
96107 { path : "src" , kind : "directory" } ,
97108 { path : "src/app.ts" , kind : "file" } ,
@@ -103,10 +114,10 @@ describe("Files panel search", () => {
103114 } ) ;
104115
105116 afterEach ( async ( ) => {
106- await act ( async ( ) => renderer ?. unmount ( ) ) ;
107- renderer = undefined ;
108- vi . runOnlyPendingTimers ( ) ;
109- vi . useRealTimers ( ) ;
117+ await act ( async ( ) => root ?. unmount ( ) ) ;
118+ container ?. remove ( ) ;
119+ root = undefined ;
120+ container = undefined ;
110121 vi . unstubAllGlobals ( ) ;
111122 } ) ;
112123
@@ -154,12 +165,12 @@ describe("Files panel search", () => {
154165 await search ( "missing-file" ) ;
155166 expect ( visiblePaths ( ) ) . toEqual ( [ ] ) ;
156167
168+ const input = searchInput ( ) ;
169+ input . focus ( ) ;
157170 await act ( async ( ) => {
158- renderer ! . root . findByType ( InputGroupInput ) . props . onKeyDown ( {
159- key : "Escape" ,
160- currentTarget : { blur : vi . fn ( ) } ,
161- } ) ;
171+ input . dispatchEvent ( new KeyboardEvent ( "keydown" , { key : "Escape" , bubbles : true } ) ) ;
162172 } ) ;
173+ expect ( document . activeElement ) . not . toBe ( input ) ;
163174 expect ( visiblePaths ( ) ) . toEqual ( [ "src/" , "package.json" ] ) ;
164175 expect ( hasMessage ( "No matching files." ) ) . toBe ( false ) ;
165176 } ) ;
@@ -170,6 +181,6 @@ describe("Files panel search", () => {
170181 await search ( "missing-file" ) ;
171182 expect ( visiblePaths ( ) ) . toEqual ( [ ] ) ;
172183 expect ( hasMessage ( "No matching files." ) ) . toBe ( false ) ;
173- expect ( JSON . stringify ( renderer ! . toJSON ( ) ) ) . toContain ( "Workspace search failed." ) ;
184+ expect ( container ! . textContent ) . toContain ( "Workspace search failed." ) ;
174185 } ) ;
175186} ) ;
0 commit comments