Skip to content

Commit 122fdc8

Browse files
committed
fix unit tests
1 parent 0163988 commit 122fdc8

File tree

3 files changed

+58
-68
lines changed

3 files changed

+58
-68
lines changed

cli/commands/site/tests/list.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe( 'CLI: studio site list', () => {
5555

5656
const { runCommand } = await import( '../list' );
5757

58-
await expect( runCommand( 'table' ) ).rejects.toThrow( 'Failed to read appdata' );
58+
await expect( runCommand( 'table', false ) ).rejects.toThrow( 'Failed to read appdata' );
5959
expect( disconnect ).toHaveBeenCalled();
6060
} );
6161
} );
@@ -65,7 +65,7 @@ describe( 'CLI: studio site list', () => {
6565
const consoleSpy = jest.spyOn( console, 'log' ).mockImplementation();
6666
const { runCommand } = await import( '../list' );
6767

68-
await runCommand( 'table' );
68+
await runCommand( 'table', false );
6969

7070
expect( readAppdata ).toHaveBeenCalled();
7171
expect( consoleSpy ).toHaveBeenCalled();
@@ -78,18 +78,20 @@ describe( 'CLI: studio site list', () => {
7878
const consoleSpy = jest.spyOn( console, 'log' ).mockImplementation();
7979
const { runCommand } = await import( '../list' );
8080

81-
await runCommand( 'json' );
81+
await runCommand( 'json', false );
8282

8383
expect( consoleSpy ).toHaveBeenCalledWith(
8484
JSON.stringify(
8585
[
8686
{
87+
id: 'site-1',
8788
status: '🔴 Offline',
8889
name: 'Test Site 1',
8990
path: '/path/to/site1',
9091
url: 'http://localhost:8080',
9192
},
9293
{
94+
id: 'site-2',
9395
status: '🔴 Offline',
9496
name: 'Test Site 2',
9597
path: '/path/to/site2',
@@ -110,7 +112,7 @@ describe( 'CLI: studio site list', () => {
110112

111113
const { runCommand } = await import( '../list' );
112114

113-
await runCommand( 'table' );
115+
await runCommand( 'table', false );
114116

115117
expect( readAppdata ).toHaveBeenCalled();
116118
expect( disconnect ).toHaveBeenCalled();
@@ -120,7 +122,7 @@ describe( 'CLI: studio site list', () => {
120122
const consoleSpy = jest.spyOn( console, 'log' ).mockImplementation();
121123
const { runCommand } = await import( '../list' );
122124

123-
await runCommand( 'json' );
125+
await runCommand( 'json', false );
124126

125127
expect( consoleSpy ).toHaveBeenCalledWith( expect.stringContaining( 'my-site.wp.local' ) );
126128
expect( disconnect ).toHaveBeenCalled();

src/tests/ipc-handlers.test.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ import { readFile } from 'atomically';
99
import { bumpStat } from 'common/lib/bump-stat';
1010
import { isEmptyDir, pathExists } from 'common/lib/fs-utils';
1111
import { StatsGroup, StatsMetric } from 'common/types/stats';
12-
import { createSite, startServer, isFullscreen, importSite } from 'src/ipc-handlers';
12+
import { createSite, isFullscreen, importSite } from 'src/ipc-handlers';
1313
import { importBackup, defaultImporterOptions } from 'src/lib/import-export/import/import-manager';
1414
import { BackupArchiveInfo } from 'src/lib/import-export/import/types';
15-
import { keepSqliteIntegrationUpdated } from 'src/lib/sqlite-versions';
1615
import { getMainWindow } from 'src/main-window';
1716
import { SiteServer, createSiteWorkingDirectory } from 'src/site-server';
1817

1918
jest.mock( 'fs' );
2019
jest.mock( 'fs-extra' );
2120
jest.mock( 'common/lib/fs-utils' );
2221
jest.mock( 'src/site-server' );
23-
jest.mock( 'src/lib/sqlite-versions' );
2422
jest.mock( 'src/lib/wordpress-provider', () => ( {
2523
downloadWordPress: jest.fn(),
2624
downloadWpCli: jest.fn(),
@@ -141,22 +139,6 @@ describe( 'createSite', () => {
141139
} );
142140
} );
143141

144-
describe( 'startServer', () => {
145-
it( 'should keep SQLite integration up-to-date', async () => {
146-
const mockSitePath = 'mock-site-path';
147-
( keepSqliteIntegrationUpdated as jest.Mock ).mockResolvedValue( undefined );
148-
( SiteServer.get as jest.Mock ).mockReturnValue( {
149-
details: { path: mockSitePath },
150-
start: jest.fn(),
151-
updateSiteDetails: jest.fn(),
152-
updateCachedThumbnail: jest.fn( () => Promise.resolve() ),
153-
} );
154-
155-
await startServer( mockIpcMainInvokeEvent, 'mock-site-id' );
156-
157-
expect( keepSqliteIntegrationUpdated ).toHaveBeenCalledWith( mockSitePath );
158-
} );
159-
} );
160142

161143
describe( 'isFullscreen', () => {
162144
it( 'should return false when window is not in fullscreen', async () => {

src/tests/site-server.test.ts

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
* @jest-environment node
33
*/
44
import { SiteServer } from 'src/site-server';
5+
import { createCliServerProcess } from 'src/modules/cli/lib/cli-server-process';
56

67
// Electron's Node.js environment provides `bota`/`atob`, but Jests' does not
78
jest.mock( 'common/lib/passwords' );
89

9-
// `SiteServer::start` uses `getPreferredSiteLanguage` to set the site language
10-
jest.mock( 'src/lib/site-language', () => ( {
11-
getPreferredSiteLanguage: jest.fn().mockResolvedValue( 'en' ),
12-
} ) );
10+
// Mock the CLI server process
11+
jest.mock( 'src/modules/cli/lib/cli-server-process' );
1312

1413
// Mock the WordPress provider
1514
jest.mock( 'src/lib/wordpress-provider', () => {
@@ -18,31 +17,6 @@ jest.mock( 'src/lib/wordpress-provider', () => {
1817
DEFAULT_WORDPRESS_VERSION: 'latest',
1918
ALLOWED_PHP_VERSIONS: [ '8.0', '8.1', '8.2', '8.3' ],
2019
SQLITE_FILENAME: 'sqlite-database-integration',
21-
getWordPressVersionPath: jest.fn( ( version ) => `/mock/path/to/wp-${ version }` ),
22-
getSqlitePath: jest.fn( () => '/mock/path/to/sqlite' ),
23-
getWpCliPath: jest.fn( () => '/mock/path/to/wp-cli' ),
24-
getWpCliFolderPath: jest.fn( () => '/mock/path/to/wp-cli-folder' ),
25-
downloadWordPress: jest.fn(),
26-
downloadWpCli: jest.fn(),
27-
downloadSQLiteCommand: jest.fn(),
28-
setupWordPressSite: jest.fn( () => Promise.resolve( true ) ),
29-
startServer: jest.fn( () =>
30-
Promise.resolve( {
31-
url: 'http://localhost:1234',
32-
options: { port: 1234, phpVersion: '8.0' },
33-
_internal: { mode: 'wordpress', port: 1234 },
34-
} )
35-
),
36-
createServerProcess: jest.fn( () => ( {
37-
url: 'http://localhost:1234',
38-
php: {},
39-
start: jest.fn( () => Promise.resolve() ),
40-
stop: jest.fn( () => Promise.resolve() ),
41-
runPhp: jest.fn( () => Promise.resolve( '' ) ),
42-
} ) ),
43-
executeWPCli: jest.fn(),
44-
isValidWordPressVersion: jest.fn( () => true ),
45-
getConfig: jest.fn( () => Promise.resolve( {} ) ),
4620
};
4721

4822
return {
@@ -51,23 +25,32 @@ jest.mock( 'src/lib/wordpress-provider', () => {
5125
};
5226
} );
5327

54-
// Mock the wp-now config that the provider uses internally
55-
jest.mock( 'vendor/wp-now/src', () => ( {
56-
getWpNowConfig: jest.fn( () => ( { mode: 'wordpress', port: 1234 } ) ),
28+
// Mock port finder
29+
jest.mock( 'common/lib/port-finder', () => ( {
30+
portFinder: {
31+
isPortAvailable: jest.fn( () => Promise.resolve( true ) ),
32+
},
33+
} ) );
34+
35+
// Mock user data
36+
jest.mock( 'src/storage/user-data', () => ( {
37+
loadUserData: jest.fn( () => Promise.resolve( { sites: [] } ) ),
5738
} ) );
5839

5940
describe( 'SiteServer', () => {
41+
beforeEach( () => {
42+
jest.clearAllMocks();
43+
} );
44+
6045
describe( 'start', () => {
61-
it( 'should throw if the server starts with a non-WordPress mode', async () => {
62-
const { getWpNowConfig } = require( 'vendor/wp-now/src' );
63-
( getWpNowConfig as jest.Mock ).mockReturnValue( { mode: 'theme', port: 1234 } );
46+
it( 'should throw if the CLI server fails to start', async () => {
47+
const mockStart = jest.fn().mockRejectedValue( new Error( 'Failed to start site' ) );
48+
( createCliServerProcess as jest.Mock ).mockReturnValue( {
49+
url: 'http://localhost:1234',
50+
start: mockStart,
51+
stop: jest.fn(),
52+
} );
6453

65-
const { startServer } = require( 'src/lib/wordpress-provider' );
66-
( startServer as jest.Mock ).mockRejectedValue(
67-
new Error(
68-
"Site server started with Playground's 'theme' mode. Studio only supports 'wordpress' mode."
69-
)
70-
);
7154
const server = SiteServer.create( {
7255
id: 'test-id',
7356
name: 'test-name',
@@ -79,9 +62,32 @@ describe( 'SiteServer', () => {
7962
themeDetails: undefined,
8063
} );
8164

82-
await expect( server.start() ).rejects.toThrow(
83-
"Site server started with Playground's 'theme' mode. Studio only supports 'wordpress' mode."
84-
);
65+
await expect( server.start() ).rejects.toThrow( 'Failed to start site' );
66+
} );
67+
68+
it( 'should start the server successfully', async () => {
69+
const mockStart = jest.fn().mockResolvedValue( undefined );
70+
( createCliServerProcess as jest.Mock ).mockReturnValue( {
71+
url: 'http://localhost:1234',
72+
start: mockStart,
73+
stop: jest.fn(),
74+
} );
75+
76+
const server = SiteServer.create( {
77+
id: 'test-id',
78+
name: 'test-name',
79+
path: 'test-path',
80+
port: 1234,
81+
adminPassword: 'test-password',
82+
phpVersion: '8.3',
83+
running: false,
84+
themeDetails: undefined,
85+
} );
86+
87+
await server.start();
88+
89+
expect( mockStart ).toHaveBeenCalled();
90+
expect( server.details.running ).toBe( true );
8591
} );
8692
} );
8793
} );

0 commit comments

Comments
 (0)