@@ -17,6 +17,7 @@ import {
17
17
} from '@salesforce/lwc-dev-mobile-core' ;
18
18
import { stubSpinner , stubUx } from '@salesforce/sf-plugins-core' ;
19
19
import { expect } from 'chai' ;
20
+ import esmock from 'esmock' ;
20
21
import sinon from 'sinon' ;
21
22
import LightningPreviewApp , {
22
23
androidSalesforceAppPreviewConfig ,
@@ -49,11 +50,20 @@ describe('lightning preview app', () => {
49
50
'34'
50
51
) ;
51
52
const testEmulatorPort = 1234 ;
53
+ let MockedLightningPreviewApp : typeof LightningPreviewApp ;
52
54
53
55
beforeEach ( async ( ) => {
54
56
stubUx ( $$ . SANDBOX ) ;
55
57
stubSpinner ( $$ . SANDBOX ) ;
56
58
await $$ . stubAuths ( testOrgData ) ;
59
+ MockedLightningPreviewApp = await esmock < typeof LightningPreviewApp > (
60
+ '../../../../src/commands/lightning/preview/app.js' ,
61
+ {
62
+ '../../../../src/lwc-dev-server/index.js' : {
63
+ startLWCServer : async ( ) => ( { stopServer : ( ) => { } } ) ,
64
+ } ,
65
+ }
66
+ ) ;
57
67
} ) ;
58
68
59
69
afterEach ( ( ) => {
@@ -63,7 +73,7 @@ describe('lightning preview app', () => {
63
73
it ( 'throws when app not found' , async ( ) => {
64
74
try {
65
75
$$ . SANDBOX . stub ( OrgUtils , 'getAppId' ) . resolves ( undefined ) ;
66
- await LightningPreviewApp . run ( [ '--name' , 'blah' , '-o' , testOrgData . username ] ) ;
76
+ await MockedLightningPreviewApp . run ( [ '--name' , 'blah' , '-o' , testOrgData . username ] ) ;
67
77
} catch ( err ) {
68
78
expect ( err )
69
79
. to . be . an ( 'error' )
@@ -77,7 +87,7 @@ describe('lightning preview app', () => {
77
87
$$ . SANDBOX . stub ( PreviewUtils , 'generateWebSocketUrlForLocalDevServer' ) . throws (
78
88
new Error ( 'Cannot determine LDP url.' )
79
89
) ;
80
- await LightningPreviewApp . run ( [ '--name' , 'Sales' , '-o' , testOrgData . username ] ) ;
90
+ await MockedLightningPreviewApp . run ( [ '--name' , 'Sales' , '-o' , testOrgData . username ] ) ;
81
91
} catch ( err ) {
82
92
expect ( err ) . to . be . an ( 'error' ) . with . property ( 'message' , 'Cannot determine LDP url.' ) ;
83
93
}
@@ -97,9 +107,9 @@ describe('lightning preview app', () => {
97
107
$$ . SANDBOX . stub ( PreviewUtils , 'generateWebSocketUrlForLocalDevServer' ) . returns ( testServerUrl ) ;
98
108
const runCmdStub = $$ . SANDBOX . stub ( Config . prototype , 'runCommand' ) . resolves ( ) ;
99
109
if ( appName ) {
100
- await LightningPreviewApp . run ( [ '--name' , appName , '-o' , testOrgData . username ] ) ;
110
+ await MockedLightningPreviewApp . run ( [ '--name' , appName , '-o' , testOrgData . username ] ) ;
101
111
} else {
102
- await LightningPreviewApp . run ( [ '-o' , testOrgData . username ] ) ;
112
+ await MockedLightningPreviewApp . run ( [ '-o' , testOrgData . username ] ) ;
103
113
}
104
114
105
115
expect ( runCmdStub . calledOnce ) ;
@@ -192,7 +202,10 @@ describe('lightning preview app', () => {
192
202
const expectedCertFilePath = '/path/to/cert.pem' ;
193
203
$$ . SANDBOX . stub ( PreviewUtils , 'generateSelfSignedCert' ) . returns ( expectedCertFilePath ) ;
194
204
195
- const waitForUserToInstallCertStub = $$ . SANDBOX . stub ( LightningPreviewApp , 'waitForUserToInstallCert' ) . resolves ( ) ;
205
+ const waitForUserToInstallCertStub = $$ . SANDBOX . stub (
206
+ MockedLightningPreviewApp ,
207
+ 'waitForUserToInstallCert'
208
+ ) . resolves ( ) ;
196
209
197
210
$$ . SANDBOX . stub ( PreviewUtils , 'verifyMobileAppInstalled' ) . resolves ( true ) ;
198
211
$$ . SANDBOX . stub ( PreviewUtils , 'launchMobileApp' ) . resolves ( ) ;
@@ -225,10 +238,10 @@ describe('lightning preview app', () => {
225
238
const expectedCertFilePath = '/path/to/cert.pem' ;
226
239
$$ . SANDBOX . stub ( PreviewUtils , 'generateSelfSignedCert' ) . returns ( expectedCertFilePath ) ;
227
240
228
- $$ . SANDBOX . stub ( LightningPreviewApp , 'waitForUserToInstallCert' ) . resolves ( ) ;
241
+ $$ . SANDBOX . stub ( MockedLightningPreviewApp , 'waitForUserToInstallCert' ) . resolves ( ) ;
229
242
230
243
const verifyMobileAppInstalledStub = $$ . SANDBOX . stub ( PreviewUtils , 'verifyMobileAppInstalled' ) . resolves ( false ) ;
231
- $$ . SANDBOX . stub ( LightningPreviewApp . prototype , 'confirm' ) . resolves ( false ) ;
244
+ $$ . SANDBOX . stub ( MockedLightningPreviewApp . prototype , 'confirm' ) . resolves ( false ) ;
232
245
233
246
await verifyMobileThrowsWhenUserDeclinesToInstallApp ( Platform . ios , verifyMobileAppInstalledStub ) ;
234
247
await verifyMobileThrowsWhenUserDeclinesToInstallApp ( Platform . android , verifyMobileAppInstalledStub ) ;
@@ -250,10 +263,10 @@ describe('lightning preview app', () => {
250
263
const expectedCertFilePath = '/path/to/cert.pem' ;
251
264
$$ . SANDBOX . stub ( PreviewUtils , 'generateSelfSignedCert' ) . returns ( expectedCertFilePath ) ;
252
265
253
- $$ . SANDBOX . stub ( LightningPreviewApp , 'waitForUserToInstallCert' ) . resolves ( ) ;
266
+ $$ . SANDBOX . stub ( MockedLightningPreviewApp , 'waitForUserToInstallCert' ) . resolves ( ) ;
254
267
255
268
$$ . SANDBOX . stub ( PreviewUtils , 'verifyMobileAppInstalled' ) . resolves ( false ) ;
256
- $$ . SANDBOX . stub ( LightningPreviewApp . prototype , 'confirm' ) . resolves ( true ) ;
269
+ $$ . SANDBOX . stub ( MockedLightningPreviewApp . prototype , 'confirm' ) . resolves ( true ) ;
257
270
258
271
const iosBundlePath = '/path/to/bundle.zip' ;
259
272
const androidBundlePath = '/path/to/bundle.apk' ;
@@ -269,15 +282,24 @@ describe('lightning preview app', () => {
269
282
270
283
async function verifyMobileThrowsWithUnmetRequirements ( platform : Platform . ios | Platform . android ) {
271
284
try {
272
- await LightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
285
+ await MockedLightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
273
286
} catch ( err ) {
274
287
expect ( err ) . to . be . an ( 'error' ) . with . property ( 'message' ) . that . contains ( 'Requirement blah not met' ) ;
275
288
}
276
289
}
277
290
278
291
async function verifyMobileThrowsWhenDeviceNotFound ( platform : Platform . ios | Platform . android ) {
279
292
try {
280
- await LightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform , '-i' , 'some_device' ] ) ;
293
+ await MockedLightningPreviewApp . run ( [
294
+ '-n' ,
295
+ 'Sales' ,
296
+ '-o' ,
297
+ testOrgData . username ,
298
+ '-t' ,
299
+ platform ,
300
+ '-i' ,
301
+ 'some_device' ,
302
+ ] ) ;
281
303
} catch ( err ) {
282
304
expect ( err )
283
305
. to . be . an ( 'error' )
@@ -290,7 +312,7 @@ describe('lightning preview app', () => {
290
312
bootStub : sinon . SinonStub
291
313
) {
292
314
try {
293
- await LightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
315
+ await MockedLightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
294
316
} catch ( err ) {
295
317
expect ( err ) . to . be . an ( 'error' ) . with . property ( 'message' , 'Failed to boot device' ) ;
296
318
@@ -308,7 +330,7 @@ describe('lightning preview app', () => {
308
330
309
331
async function verifyMobileThrowsWhenFailedToGenerateCert ( platform : Platform . ios | Platform . android ) {
310
332
try {
311
- await LightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
333
+ await MockedLightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
312
334
} catch ( err ) {
313
335
expect ( err ) . to . be . an ( 'error' ) . with . property ( 'message' , 'Failed to generate certificate' ) ;
314
336
}
@@ -320,7 +342,7 @@ describe('lightning preview app', () => {
320
342
waitForUserToInstallCertStub : sinon . SinonStub
321
343
) {
322
344
const expectedDevice = platform === Platform . ios ? testIOSDevice : testAndroidDevice ;
323
- await LightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
345
+ await MockedLightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
324
346
expect ( waitForUserToInstallCertStub . calledWith ( platform , expectedDevice , expectedCertFilePath ) ) . to . be . true ;
325
347
waitForUserToInstallCertStub . resetHistory ( ) ;
326
348
}
@@ -333,7 +355,7 @@ describe('lightning preview app', () => {
333
355
const deviceId = platform === Platform . ios ? testIOSDevice . udid : testAndroidDevice . name ;
334
356
335
357
try {
336
- await LightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
358
+ await MockedLightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
337
359
} catch ( err ) {
338
360
expect ( err )
339
361
. to . be . an ( 'error' )
@@ -362,9 +384,13 @@ describe('lightning preview app', () => {
362
384
const expectedAppConfig =
363
385
platform === Platform . ios ? iOSSalesforceAppPreviewConfig : androidSalesforceAppPreviewConfig ;
364
386
// eslint-disable-next-line camelcase
365
- expectedAppConfig . launch_arguments = PreviewUtils . generateMobileAppPreviewLaunchArguments ( expectedLdpServerUrl ) ;
387
+ expectedAppConfig . launch_arguments = PreviewUtils . generateMobileAppPreviewLaunchArguments (
388
+ expectedLdpServerUrl ,
389
+ 'Sales' ,
390
+ testAppId
391
+ ) ;
366
392
367
- await LightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
393
+ await MockedLightningPreviewApp . run ( [ '-n' , 'Sales' , '-o' , testOrgData . username , '-t' , platform ] ) ;
368
394
expect ( downloadStub . calledOnce ) . to . be . true ;
369
395
370
396
if ( platform === Platform . ios ) {
0 commit comments