Skip to content

Commit 0ea9e30

Browse files
committed
Refactor tests
1 parent 9ff510d commit 0ea9e30

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/lib/deeplink/tests/add-site.test.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { app, dialog, BrowserWindow } from 'electron';
55
import fs from 'fs-extra';
66
import { sendIpcEventToRenderer } from 'src/ipc-utils';
7+
import { validateBlueprintData } from 'src/lib/blueprint-features';
78
import { handleAddSiteWithBlueprint } from 'src/lib/deeplink/handlers/add-site-blueprint-with-url';
89
import { download } from 'src/lib/download';
910
import { getMainWindow } from 'src/main-window';
@@ -33,8 +34,14 @@ describe( 'handleAddSiteWithBlueprint', () => {
3334
focus: jest.fn(),
3435
} as unknown as BrowserWindow;
3536

37+
const createBlueprintUrl = ( blueprintUrl: string ) => {
38+
const encodedUrl = encodeURIComponent( blueprintUrl );
39+
return new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` );
40+
};
41+
3642
beforeEach( () => {
3743
jest.clearAllMocks();
44+
( mockMainWindow.isMinimized as jest.Mock ).mockReturnValue( false );
3845
jest.mocked( app.getPath ).mockReturnValue( '/tmp' );
3946
( fs.mkdir as unknown as jest.Mock ).mockResolvedValue( undefined );
4047
jest.mocked( getMainWindow ).mockResolvedValue( mockMainWindow );
@@ -46,12 +53,10 @@ describe( 'handleAddSiteWithBlueprint', () => {
4653

4754
it( 'should handle add-site with valid blueprint_url', async () => {
4855
const blueprintUrl = 'https://example.com/blueprint.json';
49-
const encodedUrl = encodeURIComponent( blueprintUrl );
50-
const url = new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` );
56+
const url = createBlueprintUrl( blueprintUrl );
5157

52-
const { validateBlueprintData } = await import( 'src/lib/blueprint-features' );
5358
jest.mocked( download ).mockResolvedValue( undefined );
54-
( fs.readJson as unknown as jest.Mock ).mockResolvedValue( { steps: [] } );
59+
jest.mocked( fs.readJson ).mockResolvedValue( { steps: [] } );
5560
jest.mocked( validateBlueprintData ).mockResolvedValue( { valid: true } );
5661

5762
await handleAddSiteWithBlueprint( url );
@@ -90,9 +95,7 @@ describe( 'handleAddSiteWithBlueprint', () => {
9095
} );
9196

9297
it( 'should handle download failure gracefully', async () => {
93-
const blueprintUrl = 'https://example.com/blueprint.json';
94-
const encodedUrl = encodeURIComponent( blueprintUrl );
95-
const url = new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` );
98+
const url = createBlueprintUrl( 'https://example.com/blueprint.json' );
9699

97100
const downloadError = new Error( 'Download failed' );
98101
jest.mocked( download ).mockRejectedValue( downloadError );
@@ -112,11 +115,8 @@ describe( 'handleAddSiteWithBlueprint', () => {
112115
} );
113116

114117
it( 'should restore and focus window when minimized', async () => {
115-
const blueprintUrl = 'https://example.com/blueprint.json';
116-
const encodedUrl = encodeURIComponent( blueprintUrl );
117-
const url = new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` );
118+
const url = createBlueprintUrl( 'https://example.com/blueprint.json' );
118119

119-
const { validateBlueprintData } = await import( 'src/lib/blueprint-features' );
120120
( mockMainWindow.isMinimized as jest.Mock ).mockReturnValue( true );
121121
jest.mocked( download ).mockResolvedValue( undefined );
122122
( fs.readJson as unknown as jest.Mock ).mockResolvedValue( { steps: [] } );
@@ -129,9 +129,7 @@ describe( 'handleAddSiteWithBlueprint', () => {
129129
} );
130130

131131
it( 'should handle cleanup errors gracefully on download failure', async () => {
132-
const blueprintUrl = 'https://example.com/blueprint.json';
133-
const encodedUrl = encodeURIComponent( blueprintUrl );
134-
const url = new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` );
132+
const url = createBlueprintUrl( 'https://example.com/blueprint.json' );
135133

136134
const downloadError = new Error( 'Download failed' );
137135
jest.mocked( download ).mockRejectedValue( downloadError );
@@ -143,11 +141,8 @@ describe( 'handleAddSiteWithBlueprint', () => {
143141
} );
144142

145143
it( 'should handle invalid blueprint and show error dialog', async () => {
146-
const blueprintUrl = 'https://example.com/blueprint.json';
147-
const encodedUrl = encodeURIComponent( blueprintUrl );
148-
const url = new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` );
144+
const url = createBlueprintUrl( 'https://example.com/blueprint.json' );
149145

150-
const { validateBlueprintData } = await import( 'src/lib/blueprint-features' );
151146
jest.mocked( download ).mockResolvedValue( undefined );
152147
( fs.readJson as unknown as jest.Mock ).mockResolvedValue( { invalid: 'data' } );
153148
jest.mocked( validateBlueprintData ).mockResolvedValue( {

0 commit comments

Comments
 (0)