@@ -7,6 +7,35 @@ import { StudioFileConflictError } from "../utils/studioSaveDiagnostics";
77
88( globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT : boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
99
10+ type WriteProjectFile = ( path : string , content : string , expectedContent ?: string ) => Promise < void > ;
11+
12+ async function mountEditorSave ( writeProjectFile : WriteProjectFile ) {
13+ const captured : { handle : EditorSaveHandle | null } = { handle : null } ;
14+
15+ function Probe ( ) {
16+ captured . handle = useEditorSave ( {
17+ editingPathRef : { current : "index.html" } ,
18+ projectIdRef : { current : "project-a" } ,
19+ readProjectFile : vi . fn ( async ( ) => "before" ) ,
20+ writeProjectFile,
21+ recordEdit : vi . fn ( async ( ) => undefined ) ,
22+ domEditSaveTimestampRef : { current : 0 } ,
23+ setRefreshKey : vi . fn ( ) ,
24+ showToast : vi . fn ( ) ,
25+ } ) ;
26+ return null ;
27+ }
28+
29+ const root = createRoot ( document . createElement ( "div" ) ) ;
30+ await act ( async ( ) => root . render ( < Probe /> ) ) ;
31+ if ( ! captured . handle ) throw new Error ( "Editor save handle was not mounted" ) ;
32+
33+ return {
34+ handle : captured . handle ,
35+ unmount : ( ) => act ( async ( ) => root . unmount ( ) ) ,
36+ } ;
37+ }
38+
1039describe ( "useEditorSave pending work" , ( ) => {
1140 beforeEach ( ( ) => {
1241 vi . stubGlobal (
@@ -20,35 +49,18 @@ describe("useEditorSave pending work", () => {
2049
2150 it ( "exposes and flushes the latest rAF-buffered source candidate" , async ( ) => {
2251 const writeProjectFile = vi . fn ( async ( ) => undefined ) ;
23- const captured : { handle : EditorSaveHandle | null } = { handle : null } ;
24-
25- function Probe ( ) {
26- captured . handle = useEditorSave ( {
27- editingPathRef : { current : "index.html" } ,
28- projectIdRef : { current : "project-a" } ,
29- readProjectFile : vi . fn ( async ( ) => "before" ) ,
30- writeProjectFile,
31- recordEdit : vi . fn ( async ( ) => undefined ) ,
32- domEditSaveTimestampRef : { current : 0 } ,
33- setRefreshKey : vi . fn ( ) ,
34- showToast : vi . fn ( ) ,
35- } ) ;
36- return null ;
37- }
38-
39- const root = createRoot ( document . createElement ( "div" ) ) ;
40- await act ( async ( ) => root . render ( < Probe /> ) ) ;
41- act ( ( ) => captured . handle ?. handleContentChange ( "studio candidate" ) ) ;
42-
43- expect ( captured . handle ?. getPendingCandidate ( ) ) . toEqual ( {
52+ const mounted = await mountEditorSave ( writeProjectFile ) ;
53+ act ( ( ) => mounted . handle . handleContentChange ( "studio candidate" ) ) ;
54+
55+ expect ( mounted . handle . getPendingCandidate ( ) ) . toEqual ( {
4456 projectId : "project-a" ,
4557 path : "index.html" ,
4658 content : "studio candidate" ,
4759 } ) ;
48- await expect ( captured . handle ? .flushPendingSave ( ) ) . resolves . toEqual ( { status : "clean" } ) ;
60+ await expect ( mounted . handle . flushPendingSave ( ) ) . resolves . toEqual ( { status : "clean" } ) ;
4961 expect ( writeProjectFile ) . toHaveBeenCalledWith ( "index.html" , "studio candidate" , "before" ) ;
5062
51- await act ( async ( ) => root . unmount ( ) ) ;
63+ await mounted . unmount ( ) ;
5264 } ) ;
5365
5466 it ( "joins an in-flight source save instead of writing the frozen candidate twice" , async ( ) => {
@@ -70,32 +82,17 @@ describe("useEditorSave pending work", () => {
7082 } ) ,
7183 )
7284 . mockResolvedValue ( undefined ) ;
73- const captured : { handle : EditorSaveHandle | null } = { handle : null } ;
74- function Probe ( ) {
75- captured . handle = useEditorSave ( {
76- editingPathRef : { current : "index.html" } ,
77- projectIdRef : { current : "project-a" } ,
78- readProjectFile : vi . fn ( async ( ) => "before" ) ,
79- writeProjectFile,
80- recordEdit : vi . fn ( async ( ) => undefined ) ,
81- domEditSaveTimestampRef : { current : 0 } ,
82- setRefreshKey : vi . fn ( ) ,
83- showToast : vi . fn ( ) ,
84- } ) ;
85- return null ;
86- }
87- const root = createRoot ( document . createElement ( "div" ) ) ;
88- await act ( async ( ) => root . render ( < Probe /> ) ) ;
89- act ( ( ) => captured . handle ?. handleContentChange ( "candidate" ) ) ;
85+ const mounted = await mountEditorSave ( writeProjectFile ) ;
86+ act ( ( ) => mounted . handle . handleContentChange ( "candidate" ) ) ;
9087 act ( ( ) => frame ?.( 0 ) ) ;
9188 await vi . waitFor ( ( ) => expect ( writeProjectFile ) . toHaveBeenCalledOnce ( ) ) ;
9289
93- const drained = captured . handle ? .flushPendingSave ( ) ;
90+ const drained = mounted . handle . flushPendingSave ( ) ;
9491 expect ( writeProjectFile ) . toHaveBeenCalledOnce ( ) ;
9592 finishWrite ( ) ;
9693 await expect ( drained ) . resolves . toEqual ( { status : "clean" } ) ;
9794 expect ( writeProjectFile ) . toHaveBeenCalledOnce ( ) ;
98- await act ( async ( ) => root . unmount ( ) ) ;
95+ await mounted . unmount ( ) ;
9996 } ) ;
10097
10198 it ( "preserves conflict details when flushing a buffered source candidate" , async ( ) => {
@@ -105,64 +102,30 @@ describe("useEditorSave pending work", () => {
105102 currentContent : "external" ,
106103 attemptedContent : "studio candidate" ,
107104 } ) ;
108- const captured : { handle : EditorSaveHandle | null } = { handle : null } ;
109-
110- function Probe ( ) {
111- captured . handle = useEditorSave ( {
112- editingPathRef : { current : "index.html" } ,
113- projectIdRef : { current : "project-a" } ,
114- readProjectFile : vi . fn ( async ( ) => "before" ) ,
115- writeProjectFile : vi . fn ( async ( ) => {
116- throw conflict ;
117- } ) ,
118- recordEdit : vi . fn ( async ( ) => undefined ) ,
119- domEditSaveTimestampRef : { current : 0 } ,
120- setRefreshKey : vi . fn ( ) ,
121- showToast : vi . fn ( ) ,
122- } ) ;
123- return null ;
124- }
125-
126- const root = createRoot ( document . createElement ( "div" ) ) ;
127- await act ( async ( ) => root . render ( < Probe /> ) ) ;
128- act ( ( ) => captured . handle ?. handleContentChange ( "studio candidate" ) ) ;
129-
130- await expect ( captured . handle ?. flushPendingSave ( ) ) . resolves . toEqual ( {
105+ const mounted = await mountEditorSave ( async ( ) => {
106+ throw conflict ;
107+ } ) ;
108+ act ( ( ) => mounted . handle . handleContentChange ( "studio candidate" ) ) ;
109+
110+ await expect ( mounted . handle . flushPendingSave ( ) ) . resolves . toEqual ( {
131111 status : "conflict" ,
132112 error : conflict ,
133113 } ) ;
134114
135- await act ( async ( ) => root . unmount ( ) ) ;
115+ await mounted . unmount ( ) ;
136116 } ) ;
137117
138118 it ( "discards an rAF-buffered candidate without persisting it" , async ( ) => {
139119 const writeProjectFile = vi . fn ( async ( ) => undefined ) ;
140- const captured : { handle : EditorSaveHandle | null } = { handle : null } ;
141-
142- function Probe ( ) {
143- captured . handle = useEditorSave ( {
144- editingPathRef : { current : "index.html" } ,
145- projectIdRef : { current : "project-a" } ,
146- readProjectFile : vi . fn ( async ( ) => "before" ) ,
147- writeProjectFile,
148- recordEdit : vi . fn ( async ( ) => undefined ) ,
149- domEditSaveTimestampRef : { current : 0 } ,
150- setRefreshKey : vi . fn ( ) ,
151- showToast : vi . fn ( ) ,
152- } ) ;
153- return null ;
154- }
155-
156- const root = createRoot ( document . createElement ( "div" ) ) ;
157- await act ( async ( ) => root . render ( < Probe /> ) ) ;
158- act ( ( ) => captured . handle ?. handleContentChange ( "discard me" ) ) ;
159- act ( ( ) => captured . handle ?. discardPendingSave ( ) ) ;
160-
161- expect ( captured . handle ?. getPendingCandidate ( ) ) . toBeNull ( ) ;
162- await expect ( captured . handle ?. flushPendingSave ( ) ) . resolves . toEqual ( { status : "clean" } ) ;
120+ const mounted = await mountEditorSave ( writeProjectFile ) ;
121+ act ( ( ) => mounted . handle . handleContentChange ( "discard me" ) ) ;
122+ act ( ( ) => mounted . handle . discardPendingSave ( ) ) ;
123+
124+ expect ( mounted . handle . getPendingCandidate ( ) ) . toBeNull ( ) ;
125+ await expect ( mounted . handle . flushPendingSave ( ) ) . resolves . toEqual ( { status : "clean" } ) ;
163126 expect ( writeProjectFile ) . not . toHaveBeenCalled ( ) ;
164127 expect ( cancelAnimationFrame ) . toHaveBeenCalledWith ( 41 ) ;
165128
166- await act ( async ( ) => root . unmount ( ) ) ;
129+ await mounted . unmount ( ) ;
167130 } ) ;
168131} ) ;
0 commit comments