Skip to content

Commit d52803c

Browse files
committed
refactor: extract dialog stub recording into E2ESession helper
1 parent bb29549 commit d52803c

3 files changed

Lines changed: 39 additions & 21 deletions

File tree

apps/studio/e2e/e2e-helpers.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,37 @@ export class E2ESession {
5959
await this.launchFirstWindow( testEnv );
6060
}
6161

62+
/**
63+
* Stub native message boxes to auto-answer with the given response index,
64+
* recording each dialog's text so tests can assert on what was shown (see
65+
* `getRecordedDialogs`). The stub handles both `showMessageBox( options )`
66+
* and `showMessageBox( parentWindow, options )` call shapes.
67+
*/
68+
async stubMessageBox( response = 0 ) {
69+
await this.electronApp.evaluate( ( { dialog }, autoResponse ) => {
70+
const dialogGlobal = globalThis as typeof globalThis & { __e2eDialogs: string[] };
71+
dialogGlobal.__e2eDialogs = [];
72+
dialog.showMessageBox = ( async ( ...args: unknown[] ) => {
73+
const options = ( args.length > 1 ? args[ 1 ] : args[ 0 ] ) as {
74+
title?: string;
75+
message?: string;
76+
detail?: string;
77+
};
78+
dialogGlobal.__e2eDialogs.push(
79+
[ options?.title, options?.message, options?.detail ].filter( Boolean ).join( ' — ' )
80+
);
81+
return { response: autoResponse, checkboxChecked: false };
82+
} ) as typeof dialog.showMessageBox;
83+
}, response );
84+
}
85+
86+
/** Dialog texts recorded by the `stubMessageBox` stub, oldest first. */
87+
async getRecordedDialogs(): Promise< string[] > {
88+
return this.electronApp.evaluate(
89+
() => ( globalThis as typeof globalThis & { __e2eDialogs?: string[] } ).__e2eDialogs ?? []
90+
);
91+
}
92+
6293
async closeApp() {
6394
console.log( 'Closing app...' );
6495
const childProcess = this.electronApp.process();

apps/studio/e2e/import-formats.test.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,7 @@ test.describe( 'Import backup formats', () => {
166166
// The tab's import flow asks for confirmation via a native dialog.
167167
// Record every dialog so a failed import surfaces its message below
168168
// instead of silently timing out on the completion banner.
169-
await session.electronApp.evaluate( ( { dialog } ) => {
170-
const dialogGlobal = globalThis as typeof globalThis & { __e2eDialogs?: string[] };
171-
dialogGlobal.__e2eDialogs = [];
172-
dialog.showMessageBox = ( async ( ...args: unknown[] ) => {
173-
const options = ( args.length > 1 ? args[ 1 ] : args[ 0 ] ) as {
174-
title?: string;
175-
message?: string;
176-
detail?: string;
177-
};
178-
dialogGlobal.__e2eDialogs?.push(
179-
[ options?.title, options?.message, options?.detail ].filter( Boolean ).join( ' — ' )
180-
);
181-
return { response: 0, checkboxChecked: false };
182-
} ) as typeof dialog.showMessageBox;
183-
} );
169+
await session.stubMessageBox();
184170

185171
const tab = await siteContent.navigateToTab( 'import-export' );
186172
if ( ! ( 'uploadFile' in tab ) ) {
@@ -190,19 +176,16 @@ test.describe( 'Import backup formats', () => {
190176
// Unlike the new-site flow, the tab's import UI reports completion with
191177
// "Import complete!". Errors are reported via a (stubbed) native dialog,
192178
// so fail fast with the dialog's message rather than timing out.
193-
const completeBanner = session.mainWindow.getByText( 'Import complete!' );
194179
await expect
195180
.poll(
196181
async () => {
197-
const dialogs = await session.electronApp.evaluate(
198-
() =>
199-
( globalThis as typeof globalThis & { __e2eDialogs?: string[] } ).__e2eDialogs ?? []
182+
const failure = ( await session.getRecordedDialogs() ).find( ( entry ) =>
183+
entry.includes( 'Failed importing site' )
200184
);
201-
const failure = dialogs.find( ( entry ) => entry.includes( 'Failed importing site' ) );
202185
if ( failure ) {
203186
throw new Error( `Import failed: ${ failure }` );
204187
}
205-
return completeBanner.isVisible();
188+
return tab.importCompleteBanner.isVisible();
206189
},
207190
{ timeout: 120_000 }
208191
)

apps/studio/e2e/page-objects/import-export-tab.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export default class ImportExportTab {
1919
return this.locator.getByRole( 'progressbar' );
2020
}
2121

22+
get importCompleteBanner() {
23+
return this.locator.getByText( 'Import complete!' );
24+
}
25+
2226
get importStatusMessage() {
2327
return this.locator.getByText( /Import/i );
2428
}

0 commit comments

Comments
 (0)