@@ -8,6 +8,7 @@ import { XComponentsAdapterDummy } from '../../../__tests__/adapter.dummy';
8
8
import { AnyXModule } from '../../../x-modules/x-modules.types' ;
9
9
import { InitWrapper , InstallXOptions } from '../types' ;
10
10
import { XInstaller } from '../x-installer' ;
11
+ import { SnippetConfig } from '../../api/index' ;
11
12
12
13
describe ( 'testing `XInstaller` utility' , ( ) => {
13
14
const adapter = XComponentsAdapterDummy ;
@@ -25,11 +26,30 @@ describe('testing `XInstaller` utility', () => {
25
26
mounted : jest . fn ( )
26
27
} ;
27
28
28
- const snippetConfig = {
29
+ const getMinimumSnippetConfig = ( ) : SnippetConfig => ( {
29
30
instance : 'test' ,
30
31
lang : 'test' ,
31
32
scope : 'test'
32
- } ;
33
+ } ) ;
34
+
35
+ /**
36
+ * Creates a Vue component injecting the snippet config.
37
+ *
38
+ * @param snippetProperty
39
+ * @returns A Vue component with the injected snippet config.
40
+ *
41
+ * @internal
42
+ */
43
+ function createSnippetConfigComponent (
44
+ snippetProperty : keyof SnippetConfig = 'instance'
45
+ ) : VueConstructor {
46
+ return Vue . extend ( {
47
+ inject : [ 'snippetConfig' ] ,
48
+ render ( h ) {
49
+ return h ( 'h1' , [ ( this as any ) . snippetConfig [ snippetProperty ] ] ) ;
50
+ }
51
+ } ) ;
52
+ }
33
53
34
54
beforeEach ( ( ) => {
35
55
delete window . initX ;
@@ -45,7 +65,7 @@ describe('testing `XInstaller` utility', () => {
45
65
__PRIVATE__xModules,
46
66
initialXModules : [ initialXModule ] ,
47
67
vue : createLocalVue ( )
48
- } ) . init ( snippetConfig ) ;
68
+ } ) . init ( getMinimumSnippetConfig ( ) ) ;
49
69
const params = xPluginMock . install . mock . calls [ 0 ] [ 1 ] ;
50
70
51
71
expect ( xPluginMock . install ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -59,21 +79,23 @@ describe('testing `XInstaller` utility', () => {
59
79
60
80
it ( 'creates the public API in global scope by default' , ( ) => {
61
81
delete window . InterfaceX ;
62
- new XInstaller ( { adapter, plugin, vue : createLocalVue ( ) } ) . init ( snippetConfig ) ;
82
+ new XInstaller ( { adapter, plugin, vue : createLocalVue ( ) } ) . init ( getMinimumSnippetConfig ( ) ) ;
63
83
64
84
expect ( window . InterfaceX ) . toBeDefined ( ) ;
65
85
delete window . InterfaceX ;
66
86
} ) ;
67
87
68
88
it ( 'does not create the public API passing the api parameter to false' , ( ) => {
69
- new XInstaller ( { adapter, plugin, api : false , vue : createLocalVue ( ) } ) . init ( snippetConfig ) ;
89
+ new XInstaller ( { adapter, plugin, api : false , vue : createLocalVue ( ) } ) . init (
90
+ getMinimumSnippetConfig ( )
91
+ ) ;
70
92
71
93
expect ( window . InterfaceX ) . not . toBeDefined ( ) ;
72
94
} ) ;
73
95
74
96
it ( 'installs the XPlugin using the passed vue' , ( ) => {
75
97
const localVue = createLocalVue ( ) ;
76
- new XInstaller ( { adapter, plugin, vue : localVue } ) . init ( snippetConfig ) ;
98
+ new XInstaller ( { adapter, plugin, vue : localVue } ) . init ( getMinimumSnippetConfig ( ) ) ;
77
99
const vueParam = xPluginMock . install . mock . calls [ 0 ] [ 0 ] ;
78
100
79
101
expect ( xPluginMock . install ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -82,7 +104,7 @@ describe('testing `XInstaller` utility', () => {
82
104
83
105
it ( 'creates a Vue application using the component passed in the app option' , async ( ) => {
84
106
await new XInstaller ( { adapter, plugin, app : component , vue : createLocalVue ( ) } ) . init (
85
- snippetConfig
107
+ getMinimumSnippetConfig ( )
86
108
) ;
87
109
88
110
// eslint-disable-next-line @typescript-eslint/unbound-method
@@ -102,7 +124,7 @@ describe('testing `XInstaller` utility', () => {
102
124
vue,
103
125
vueOptions,
104
126
app : component
105
- } ) . init ( snippetConfig ) ;
127
+ } ) . init ( getMinimumSnippetConfig ( ) ) ;
106
128
107
129
expect ( app ) . toHaveProperty ( 'testMethod' ) ;
108
130
expect ( app ) . toHaveProperty ( '$router' ) ;
@@ -128,57 +150,57 @@ describe('testing `XInstaller` utility', () => {
128
150
} ;
129
151
} ,
130
152
app : component
131
- } ) . init ( snippetConfig ) ;
153
+ } ) . init ( getMinimumSnippetConfig ( ) ) ;
132
154
133
155
expect ( app ) . toHaveProperty ( '$router' ) ;
134
156
expect ( app ) . toHaveProperty ( 'bus' ) ;
135
- expect ( app ) . toHaveProperty ( 'snippet' , snippetConfig ) ;
157
+ expect ( app ) . toHaveProperty ( 'snippet' , { ... getMinimumSnippetConfig ( ) , uiLang : 'test' } ) ;
136
158
} ) ;
137
159
138
160
it ( 'initializes the app with the provided snippet config' , async ( ) => {
139
161
const vue = createLocalVue ( ) ;
140
- const { app } = await new XInstaller ( {
162
+ const { app, api } = await new XInstaller ( {
141
163
adapter,
142
164
vue,
143
- app : injectSnippetConfigComponent ( )
144
- } ) . init ( snippetConfig ) ;
165
+ app : createSnippetConfigComponent ( )
166
+ } ) . init ( getMinimumSnippetConfig ( ) ) ;
145
167
146
- expect ( app ?. $el ) . toHaveTextContent ( snippetConfig . instance ) ;
168
+ expect ( app ?. $el ) . toHaveTextContent ( getMinimumSnippetConfig ( ) . instance ) ;
147
169
148
- snippetConfig . instance = 'test-2' ;
170
+ api ?. setSnippetConfig ( { instance : 'test-2' } ) ;
149
171
await vue . nextTick ( ) ;
150
172
expect ( app ?. $el ) . toHaveTextContent ( 'test-2' ) ;
151
173
} ) ;
152
174
153
175
it ( 'initializes the app when window.initX has the snippet config object' , async ( ) => {
154
176
const vue = createLocalVue ( ) ;
155
- window . initX = snippetConfig ;
156
- const { app } = ( await new XInstaller ( {
177
+ window . initX = getMinimumSnippetConfig ( ) ;
178
+ const { app, api } = ( await new XInstaller ( {
157
179
adapter,
158
180
vue,
159
- app : injectSnippetConfigComponent ( )
181
+ app : createSnippetConfigComponent ( )
160
182
} ) . init ( ) ) as InitWrapper ;
161
183
162
- expect ( app ?. $el ) . toHaveTextContent ( snippetConfig . instance ) ;
184
+ expect ( app ?. $el ) . toHaveTextContent ( getMinimumSnippetConfig ( ) . instance ) ;
163
185
164
- snippetConfig . instance = 'test-2' ;
186
+ api ?. setSnippetConfig ( { instance : 'test-2' } ) ;
165
187
await vue . nextTick ( ) ;
166
188
expect ( app ?. $el ) . toHaveTextContent ( 'test-2' ) ;
167
189
} ) ;
168
190
169
191
// eslint-disable-next-line max-len
170
192
it ( 'initializes the app when window.initX is a function retrieving the snippet config' , async ( ) => {
171
193
const vue = createLocalVue ( ) ;
172
- window . initX = ( ) => snippetConfig ;
173
- const { app } = ( await new XInstaller ( {
194
+ window . initX = ( ) => getMinimumSnippetConfig ( ) ;
195
+ const { app, api } = ( await new XInstaller ( {
174
196
adapter,
175
197
vue,
176
- app : injectSnippetConfigComponent ( )
198
+ app : createSnippetConfigComponent ( )
177
199
} ) . init ( ) ) as InitWrapper ;
178
200
179
- expect ( app ?. $el ) . toHaveTextContent ( snippetConfig . instance ) ;
201
+ expect ( app ?. $el ) . toHaveTextContent ( getMinimumSnippetConfig ( ) . instance ) ;
180
202
181
- snippetConfig . instance = 'test-2' ;
203
+ api ?. setSnippetConfig ( { instance : 'test-2' } ) ;
182
204
await vue . nextTick ( ) ;
183
205
expect ( app ?. $el ) . toHaveTextContent ( 'test-2' ) ;
184
206
} ) ;
@@ -187,22 +209,50 @@ describe('testing `XInstaller` utility', () => {
187
209
it ( 'does not initialize XComponents when no snippet config is passed and no window.initX is not defined' , async ( ) => {
188
210
expect ( await new XInstaller ( { adapter, plugin, vue : createLocalVue ( ) } ) . init ( ) ) . toBeUndefined ( ) ;
189
211
} ) ;
190
- } ) ;
191
212
192
- /**
193
- * Creates a Vue component injecting the snippet config.
194
- *
195
- * @returns A Vue component with the injected snippet config.
196
- *
197
- * @internal
198
- */
199
- function injectSnippetConfigComponent ( ) : VueConstructor {
200
- return Vue . extend ( {
201
- inject : [ 'snippetConfig' ] ,
202
- render ( h ) {
203
- // Vue does not provide type safety for inject
204
- const instance = ( this as any ) . snippetConfig . instance ;
205
- return h ( 'h1' , [ instance ] ) ;
206
- }
213
+ describe ( '`lang` & `uiLang`' , ( ) => {
214
+ it ( 'provides a `uiLang` by default' , async ( ) => {
215
+ const { app } = await new XInstaller ( {
216
+ adapter,
217
+ plugin,
218
+ vue : createLocalVue ( ) ,
219
+ app : createSnippetConfigComponent ( 'uiLang' )
220
+ } ) . init ( { ...getMinimumSnippetConfig ( ) , lang : 'en' } ) ;
221
+
222
+ expect ( app ?. $el ) . toHaveTextContent ( 'en' ) ;
223
+ } ) ;
224
+
225
+ it ( 'respects user `uiLang` value' , async ( ) => {
226
+ const { app } = await new XInstaller ( {
227
+ adapter,
228
+ plugin,
229
+ vue : createLocalVue ( ) ,
230
+ app : createSnippetConfigComponent ( 'uiLang' )
231
+ } ) . init ( { ...getMinimumSnippetConfig ( ) , lang : 'en' , uiLang : 'it' } ) ;
232
+ expect ( app ?. $el ) . toHaveTextContent ( 'it' ) ;
233
+ } ) ;
234
+
235
+ it ( 'updates `uiLang` when `lang` is changed' , async ( ) => {
236
+ const vue = createLocalVue ( ) ;
237
+ const { app, api } = await new XInstaller ( {
238
+ adapter,
239
+ plugin,
240
+ vue,
241
+ app : createSnippetConfigComponent ( 'uiLang' )
242
+ } ) . init ( { ...getMinimumSnippetConfig ( ) , lang : 'en' , uiLang : 'it' } ) ;
243
+ expect ( app ?. $el ) . toHaveTextContent ( 'it' ) ;
244
+
245
+ api ! . setSnippetConfig ( { lang : 'es' } ) ;
246
+ await vue . nextTick ( ) ;
247
+ expect ( app ?. $el ) . toHaveTextContent ( 'es' ) ;
248
+
249
+ api ! . setSnippetConfig ( { uiLang : 'en' } ) ;
250
+ await vue . nextTick ( ) ;
251
+ expect ( app ?. $el ) . toHaveTextContent ( 'en' ) ;
252
+
253
+ api ! . setSnippetConfig ( { lang : 'fr' , uiLang : 'it' } ) ;
254
+ await vue . nextTick ( ) ;
255
+ expect ( app ?. $el ) . toHaveTextContent ( 'it' ) ;
256
+ } ) ;
207
257
} ) ;
208
- }
258
+ } ) ;
0 commit comments