1
1
import { Yok } from "../../../lib/common/yok" ;
2
+ import * as _ from 'lodash' ;
2
3
import { LoggerStub , ErrorsStub } from "../../stubs" ;
3
4
import { FilePayload , Device , FilesPayload } from "nativescript-preview-sdk" ;
4
5
import { PreviewAppLiveSyncService } from "../../../lib/services/livesync/playground/preview-app-livesync-service" ;
@@ -16,12 +17,14 @@ interface ITestCase {
16
17
17
18
interface IActOptions {
18
19
callGetInitialFiles : boolean ;
20
+ hmr : boolean ;
19
21
}
20
22
21
23
interface IAssertOptions {
22
24
checkWarnings ?: boolean ;
23
25
isComparePluginsOnDeviceCalled ?: boolean ;
24
26
checkInitialFiles ?: boolean ;
27
+ hmr ?: boolean ;
25
28
}
26
29
27
30
interface IActInput {
@@ -32,6 +35,7 @@ interface IActInput {
32
35
}
33
36
34
37
let isComparePluginsOnDeviceCalled = false ;
38
+ let isHookCalledWithHMR = false ;
35
39
let applyChangesParams : FilePayload [ ] = [ ] ;
36
40
let initialFiles : FilePayload [ ] = [ ] ;
37
41
let readTextParams : string [ ] = [ ] ;
@@ -57,15 +61,16 @@ const syncFilesMockData = {
57
61
projectDir : projectDirPath ,
58
62
appFilesUpdaterOptions : {
59
63
release : false ,
60
- bundle : false
64
+ bundle : false ,
65
+ useHotModuleReload : false
61
66
} ,
62
67
env : { }
63
68
} ;
64
69
65
70
class PreviewSdkServiceMock implements IPreviewSdkService {
66
71
public getInitialFiles : ( device : Device ) => Promise < FilesPayload > ;
67
72
68
- public getQrCodeUrl ( options : { useHmr : boolean } ) {
73
+ public getQrCodeUrl ( options : IHasUseHotModuleReloadOption ) {
69
74
return "my_cool_qr_code_url" ;
70
75
}
71
76
@@ -146,7 +151,9 @@ function createTestInjector(options?: {
146
151
}
147
152
} ) ;
148
153
injector . register ( "hooksService" , {
149
- executeBeforeHooks : ( ) => ( { } )
154
+ executeBeforeHooks : ( name : string , args : any ) => {
155
+ isHookCalledWithHMR = args . hookArgs . config . appFilesUpdaterOptions . useHotModuleReload ;
156
+ }
150
157
} ) ;
151
158
152
159
return injector ;
@@ -169,8 +176,9 @@ async function initialSync(input?: IActInput) {
169
176
input = input || { } ;
170
177
171
178
const { previewAppLiveSyncService, previewSdkService, actOptions } = input ;
172
-
173
- await previewAppLiveSyncService . initialize ( syncFilesMockData ) ;
179
+ const syncFilesData = _ . cloneDeep ( syncFilesMockData ) ;
180
+ syncFilesData . appFilesUpdaterOptions . useHotModuleReload = actOptions . hmr ;
181
+ await previewAppLiveSyncService . initialize ( syncFilesData ) ;
174
182
if ( actOptions . callGetInitialFiles ) {
175
183
await previewSdkService . getInitialFiles ( deviceMockData ) ;
176
184
}
@@ -181,7 +189,9 @@ async function syncFiles(input?: IActInput) {
181
189
182
190
const { previewAppLiveSyncService, previewSdkService, projectFiles, actOptions } = input ;
183
191
184
- await previewAppLiveSyncService . initialize ( syncFilesMockData ) ;
192
+ const syncFilesData = _ . cloneDeep ( syncFilesMockData ) ;
193
+ syncFilesData . appFilesUpdaterOptions . useHotModuleReload = actOptions . hmr ;
194
+ await previewAppLiveSyncService . initialize ( syncFilesData ) ;
185
195
if ( actOptions . callGetInitialFiles ) {
186
196
await previewSdkService . getInitialFiles ( deviceMockData ) ;
187
197
}
@@ -193,6 +203,7 @@ async function assert(expectedFiles: string[], options?: IAssertOptions) {
193
203
options = options || { } ;
194
204
const actualFiles = options . checkInitialFiles ? initialFiles : applyChangesParams ;
195
205
206
+ chai . assert . equal ( isHookCalledWithHMR , options . hmr || false ) ;
196
207
chai . assert . deepEqual ( actualFiles , mapFiles ( expectedFiles ) ) ;
197
208
198
209
if ( options . checkWarnings ) {
@@ -206,6 +217,7 @@ async function assert(expectedFiles: string[], options?: IAssertOptions) {
206
217
207
218
function reset ( ) {
208
219
isComparePluginsOnDeviceCalled = false ;
220
+ isHookCalledWithHMR = false ;
209
221
applyChangesParams = [ ] ;
210
222
initialFiles = [ ] ;
211
223
readTextParams = [ ] ;
@@ -230,7 +242,8 @@ function mapFiles(files: string[]): FilePayload[] {
230
242
function setDefaults ( testCase : ITestCase ) : ITestCase {
231
243
if ( ! testCase . actOptions ) {
232
244
testCase . actOptions = {
233
- callGetInitialFiles : true
245
+ callGetInitialFiles : true ,
246
+ hmr : false
234
247
} ;
235
248
}
236
249
@@ -327,6 +340,33 @@ describe("previewAppLiveSyncService", () => {
327
340
}
328
341
] ;
329
342
343
+ const hmrTestCases : ITestCase [ ] = [
344
+ {
345
+ name : "when set to true" ,
346
+ appFiles : [ ] ,
347
+ expectedFiles : [ ] ,
348
+ actOptions : {
349
+ hmr : true ,
350
+ callGetInitialFiles : true
351
+ } ,
352
+ assertOptions : {
353
+ hmr : true
354
+ }
355
+ } ,
356
+ {
357
+ name : "when set to false" ,
358
+ appFiles : [ ] ,
359
+ expectedFiles : [ ] ,
360
+ actOptions : {
361
+ hmr : false ,
362
+ callGetInitialFiles : true
363
+ } ,
364
+ assertOptions : {
365
+ hmr : false
366
+ }
367
+ }
368
+ ] ;
369
+
330
370
const noAppFilesTestCases : ITestCase [ ] = [
331
371
{
332
372
name : "should transfer correctly default project files" ,
@@ -352,6 +392,10 @@ describe("previewAppLiveSyncService", () => {
352
392
{
353
393
name : "should handle correctly when no files are provided" ,
354
394
testCases : noAppFilesTestCases
395
+ } ,
396
+ {
397
+ name : "should pass the hmr option to the hook" ,
398
+ testCases : hmrTestCases
355
399
}
356
400
] ;
357
401
0 commit comments