@@ -2,9 +2,8 @@ import { PreviewSdkService } from "../../lib/services/livesync/playground/previe
2
2
import { Yok } from "../../lib/common/yok" ;
3
3
import { assert } from "chai" ;
4
4
import { LoggerStub } from "../stubs" ;
5
- import { PubnubKeys } from "../../lib/services/livesync/playground/preview-app-constants" ;
6
5
7
- const getPreviewSdkService = ( ) : IPreviewSdkService => {
6
+ const createTestInjector = ( ) : IInjector => {
8
7
const testInjector = new Yok ( ) ;
9
8
testInjector . register ( "logger" , LoggerStub ) ;
10
9
testInjector . register ( "config" , { } ) ;
@@ -14,114 +13,65 @@ const getPreviewSdkService = (): IPreviewSdkService => {
14
13
testInjector . register ( "httpClient" , {
15
14
httpRequest : async ( options : any , proxySettings ?: IProxySettings ) : Promise < Server . IResponse > => undefined
16
15
} ) ;
16
+ testInjector . register ( "projectDataService" , {
17
+ getProjectData : ( ) => ( { } )
18
+ } ) ;
17
19
18
- return testInjector . resolve ( "previewSdkService" ) ;
20
+ return testInjector ;
19
21
} ;
20
22
21
23
describe ( 'PreviewSdkService' , ( ) => {
24
+ let injector : IInjector , previewSdkService : IPreviewSdkService ;
25
+
26
+ beforeEach ( ( ) => {
27
+ injector = createTestInjector ( ) ;
28
+ previewSdkService = injector . resolve ( "previewSdkService" ) ;
29
+ } ) ;
30
+
22
31
describe ( 'getQrCodeUrl' , ( ) => {
23
32
describe ( "hmr" , ( ) => {
24
33
it ( 'sets hmr to 1 when useHotModuleReload is true' , async ( ) => {
25
- const sdk = getPreviewSdkService ( ) ;
26
-
27
- const previewUrl = sdk . getQrCodeUrl ( { useHotModuleReload : true } ) ;
34
+ const previewUrl = previewSdkService . getQrCodeUrl ( { projectDir : "" , useHotModuleReload : true } ) ;
28
35
29
36
assert . isTrue ( previewUrl . indexOf ( "hmr=1" ) > - 1 ) ;
30
37
} ) ;
31
38
it ( 'sets hmr to 0 when useHotModuleReload is false' , async ( ) => {
32
- const sdk = getPreviewSdkService ( ) ;
33
-
34
- const previewUrl = sdk . getQrCodeUrl ( { useHotModuleReload : false } ) ;
39
+ const previewUrl = previewSdkService . getQrCodeUrl ( { projectDir : "" , useHotModuleReload : false } ) ;
35
40
36
41
assert . isTrue ( previewUrl . indexOf ( "hmr=0" ) > - 1 ) ;
37
42
} ) ;
38
43
} ) ;
39
44
40
45
describe ( "schema" , ( ) => {
41
- const testCases : [ { name : string , schemaFromApi : string , schemaFromNsConfig : string , expectedSchemaName : string } ] = [
46
+ const testCases = [
42
47
{
43
48
name : "should return the schema from api" ,
44
- schemaFromApi : "ksplay" ,
45
- schemaFromNsConfig : null ,
46
- expectedSchemaName : "ksplay"
49
+ schemaFromNsConfig : "nsplay" ,
50
+ expectedSchemaName : "nsplay"
47
51
} ,
48
52
{
49
53
name : "should return the schema from nsconfig" ,
50
- schemaFromApi : null ,
51
54
schemaFromNsConfig : "ksplay" ,
52
55
expectedSchemaName : "ksplay"
53
56
} ,
54
57
{
55
58
name : "should return the default schema" ,
56
- schemaFromApi : null ,
57
59
schemaFromNsConfig : null ,
58
60
expectedSchemaName : "nsplay"
59
61
}
60
62
] ;
61
63
62
64
_ . each ( testCases , testCase => {
63
65
it ( `${ testCase . name } ` , ( ) => {
64
- const qrCodeData = { schemaName : testCase . schemaFromApi } ;
65
- const qrCodeOptions = { nsConfigPreviewAppSchema : testCase . schemaFromNsConfig , qrCodeData , useHotModuleReload : true } ;
66
- const previewSdkService = getPreviewSdkService ( ) ;
66
+ const qrCodeOptions = { projectDir : "myTestDir" , useHotModuleReload : true } ;
67
+ const projectDataService = injector . resolve ( "projectDataService" ) ;
68
+ projectDataService . getProjectData = ( ) => ( { previewAppSchema : testCase . schemaFromNsConfig } ) ;
67
69
68
70
const qrCodeUrl = previewSdkService . getQrCodeUrl ( qrCodeOptions ) ;
69
71
70
72
assert . deepEqual ( qrCodeUrl . split ( ":" ) [ 0 ] , testCase . expectedSchemaName ) ;
71
73
} ) ;
72
74
} ) ;
73
75
} ) ;
74
-
75
- describe ( "publishKey" , ( ) => {
76
- const testCases = [
77
- {
78
- name : "should return the provided key from api" ,
79
- publishKeyFromApi : "myTestPublishKey" ,
80
- expectedPublishKey : "myTestPublishKey"
81
- } ,
82
- {
83
- name : "should return the default key" ,
84
- publishKeyFromApi : null ,
85
- expectedPublishKey : PubnubKeys . PUBLISH_KEY
86
- }
87
- ] ;
88
-
89
- _ . each ( testCases , testCase => {
90
- it ( `${ testCase . name } ` , ( ) => {
91
- const qrCodeOptions = { projectData : < any > { } , qrCodeData : < any > { publishKey : testCase . publishKeyFromApi } , useHotModuleReload : true } ;
92
- const previewSdkService = getPreviewSdkService ( ) ;
93
-
94
- const qrCodeUrl = previewSdkService . getQrCodeUrl ( qrCodeOptions ) ;
95
-
96
- assert . isTrue ( qrCodeUrl . indexOf ( `&pKey=${ testCase . expectedPublishKey } ` ) > - 1 ) ;
97
- } ) ;
98
- } ) ;
99
- } ) ;
100
-
101
- describe ( "subscribeKey" , ( ) => {
102
- const testCases = [
103
- {
104
- name : "should return the provided key from api" ,
105
- subscribeKeyFromApi : "myTestSubscribeKey" ,
106
- expectedSubscribeKey : "myTestSubscribeKey"
107
- } ,
108
- {
109
- name : "should return the default key" ,
110
- subscribeKeyFromApi : null ,
111
- expectedSubscribeKey : PubnubKeys . SUBSCRIBE_KEY
112
- }
113
- ] ;
114
-
115
- _ . each ( testCases , testCase => {
116
- it ( `${ testCase . name } ` , ( ) => {
117
- const qrCodeOptions = { projectData : < any > { } , qrCodeData : < any > { subscribeKey : testCase . subscribeKeyFromApi } , useHotModuleReload : true } ;
118
- const previewSdkService = getPreviewSdkService ( ) ;
119
-
120
- const qrCodeUrl = previewSdkService . getQrCodeUrl ( qrCodeOptions ) ;
121
-
122
- assert . isTrue ( qrCodeUrl . indexOf ( `&sKey=${ testCase . expectedSubscribeKey } ` ) > - 1 ) ;
123
- } ) ;
124
- } ) ;
125
- } ) ;
126
76
} ) ;
127
77
} ) ;
0 commit comments