|
| 1 | +/* eslint-disable prettier/prettier */ |
| 2 | +/** |
| 3 | + * @jest-environment node |
| 4 | + */ |
| 5 | +import { app, dialog, BrowserWindow } from 'electron'; |
| 6 | +import fs from 'fs-extra'; |
| 7 | +import { sendIpcEventToRenderer } from 'src/ipc-utils'; |
| 8 | +import { handleAddSiteWithBlueprint } from 'src/lib/deeplink/handlers/add-site-blueprint-with-url'; |
| 9 | +import { download } from 'src/lib/download'; |
| 10 | +import { getMainWindow } from 'src/main-window'; |
| 11 | + |
| 12 | +jest.mock( 'electron' ); |
| 13 | +jest.mock( 'fs-extra' ); |
| 14 | +jest.mock( 'src/ipc-utils' ); |
| 15 | +jest.mock( 'src/lib/download' ); |
| 16 | +jest.mock( 'src/main-window' ); |
| 17 | + |
| 18 | +// Silence console.error output |
| 19 | +beforeAll( () => { |
| 20 | + jest.spyOn( console, 'error' ).mockImplementation( () => {} ); |
| 21 | +} ); |
| 22 | + |
| 23 | +afterAll( () => { |
| 24 | + jest.spyOn( console, 'error' ).mockRestore(); |
| 25 | +} ); |
| 26 | + |
| 27 | +describe( 'handleAddSiteWithBlueprint', () => { |
| 28 | + const mockMainWindow = { |
| 29 | + isMinimized: jest.fn().mockReturnValue( false ), |
| 30 | + restore: jest.fn(), |
| 31 | + focus: jest.fn(), |
| 32 | + } as unknown as BrowserWindow; |
| 33 | + |
| 34 | + beforeEach( () => { |
| 35 | + jest.clearAllMocks(); |
| 36 | + jest.mocked( app.getPath ).mockReturnValue( '/tmp' ); |
| 37 | + ( fs.mkdir as unknown as jest.Mock ).mockResolvedValue( undefined ); |
| 38 | + jest.mocked( getMainWindow ).mockResolvedValue( mockMainWindow ); |
| 39 | + jest.mocked( dialog.showMessageBox ).mockResolvedValue( { |
| 40 | + response: 0, |
| 41 | + checkboxChecked: false, |
| 42 | + } ); |
| 43 | + } ); |
| 44 | + |
| 45 | + it( 'should handle add-site with valid blueprint_url', async () => { |
| 46 | + const blueprintUrl = 'https://example.com/blueprint.json'; |
| 47 | + const encodedUrl = encodeURIComponent( blueprintUrl ); |
| 48 | + const url = new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` ); |
| 49 | + |
| 50 | + jest.mocked( download ).mockResolvedValue( undefined ); |
| 51 | + |
| 52 | + await handleAddSiteWithBlueprint( url ); |
| 53 | + |
| 54 | + expect( download ).toHaveBeenCalledWith( |
| 55 | + blueprintUrl, |
| 56 | + expect.stringContaining( 'blueprint-' ), |
| 57 | + false, |
| 58 | + 'blueprint' |
| 59 | + ); |
| 60 | + expect( sendIpcEventToRenderer ).toHaveBeenCalledWith( 'add-site-blueprint', { |
| 61 | + blueprintPath: expect.stringContaining( 'blueprint-' ), |
| 62 | + } ); |
| 63 | + expect( mockMainWindow.focus ).toHaveBeenCalled(); |
| 64 | + } ); |
| 65 | + |
| 66 | + it( 'should not send event if blueprint_url parameter is missing', async () => { |
| 67 | + const url = new URL( 'wpcom-local-dev://add-site' ); |
| 68 | + |
| 69 | + await handleAddSiteWithBlueprint( url ); |
| 70 | + |
| 71 | + expect( download ).not.toHaveBeenCalled(); |
| 72 | + expect( sendIpcEventToRenderer ).not.toHaveBeenCalled(); |
| 73 | + } ); |
| 74 | + |
| 75 | + it( 'should handle invalid blueprint_url gracefully', async () => { |
| 76 | + const invalidUrl = 'not-a-valid-url'; |
| 77 | + const encodedUrl = encodeURIComponent( invalidUrl ); |
| 78 | + const url = new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` ); |
| 79 | + |
| 80 | + await handleAddSiteWithBlueprint( url ); |
| 81 | + |
| 82 | + expect( download ).not.toHaveBeenCalled(); |
| 83 | + expect( sendIpcEventToRenderer ).not.toHaveBeenCalled(); |
| 84 | + expect( dialog.showMessageBox ).not.toHaveBeenCalled(); |
| 85 | + } ); |
| 86 | + |
| 87 | + it( 'should handle download failure gracefully', async () => { |
| 88 | + const blueprintUrl = 'https://example.com/blueprint.json'; |
| 89 | + const encodedUrl = encodeURIComponent( blueprintUrl ); |
| 90 | + const url = new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` ); |
| 91 | + |
| 92 | + const downloadError = new Error( 'Download failed' ); |
| 93 | + jest.mocked( download ).mockRejectedValue( downloadError ); |
| 94 | + ( fs.remove as unknown as jest.Mock ).mockResolvedValue( undefined ); |
| 95 | + |
| 96 | + await handleAddSiteWithBlueprint( url ); |
| 97 | + |
| 98 | + expect( download ).toHaveBeenCalled(); |
| 99 | + expect( sendIpcEventToRenderer ).not.toHaveBeenCalled(); |
| 100 | + expect( fs.remove ).toHaveBeenCalledWith( |
| 101 | + expect.stringContaining( 'blueprint-' ) |
| 102 | + ); |
| 103 | + expect( dialog.showMessageBox ).toHaveBeenCalledWith( mockMainWindow, { |
| 104 | + type: 'error', |
| 105 | + message: expect.any( String ), |
| 106 | + detail: expect.any( String ), |
| 107 | + buttons: expect.any( Array ), |
| 108 | + } ); |
| 109 | + } ); |
| 110 | + |
| 111 | + it( 'should restore and focus window when minimized', async () => { |
| 112 | + const blueprintUrl = 'https://example.com/blueprint.json'; |
| 113 | + const encodedUrl = encodeURIComponent( blueprintUrl ); |
| 114 | + const url = new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` ); |
| 115 | + |
| 116 | + ( mockMainWindow.isMinimized as jest.Mock ).mockReturnValue( true ); |
| 117 | + jest.mocked( download ).mockResolvedValue( undefined ); |
| 118 | + |
| 119 | + await handleAddSiteWithBlueprint( url ); |
| 120 | + |
| 121 | + expect( mockMainWindow.restore ).toHaveBeenCalled(); |
| 122 | + expect( mockMainWindow.focus ).toHaveBeenCalled(); |
| 123 | + } ); |
| 124 | + |
| 125 | + it( 'should handle cleanup errors gracefully on download failure', async () => { |
| 126 | + const blueprintUrl = 'https://example.com/blueprint.json'; |
| 127 | + const encodedUrl = encodeURIComponent( blueprintUrl ); |
| 128 | + const url = new URL( `wpcom-local-dev://add-site?blueprint_url=${ encodedUrl }` ); |
| 129 | + |
| 130 | + const downloadError = new Error( 'Download failed' ); |
| 131 | + jest.mocked( download ).mockRejectedValue( downloadError ); |
| 132 | + ( fs.remove as unknown as jest.Mock ).mockRejectedValue( new Error( 'Cleanup failed' ) ); |
| 133 | + |
| 134 | + await expect( handleAddSiteWithBlueprint( url ) ).resolves.not.toThrow(); |
| 135 | + |
| 136 | + expect( dialog.showMessageBox ).toHaveBeenCalled(); |
| 137 | + } ); |
| 138 | +} ); |
0 commit comments