1
1
import { beforeEach , describe , expect , it } from 'vitest'
2
2
3
- import { nextTick } from 'vue-demi'
3
+ import { isVue3 , nextTick } from 'vue-demi'
4
4
import { FluentBundle , FluentResource } from '@fluent/bundle'
5
5
import ftl from '@fluent/dedent'
6
6
7
7
import type { FluentVue } from '../src'
8
- import { createFluentVue } from '../src'
8
+ import { createFluentVue , useFluent } from '../src'
9
9
import { mountWithFluent } from './utils'
10
10
11
11
describe ( 'language change' , ( ) => {
@@ -104,6 +104,111 @@ describe('language change', () => {
104
104
expect ( mounted . html ( ) ) . toEqual ( '<a href="/foo">link text</a>' )
105
105
} )
106
106
107
+ it ( 'updates locale even if component is unmounted github.com/orgs/fluent-vue/discussions/834' , async ( ) => {
108
+ // Arrange
109
+ bundleEn = new FluentBundle ( 'en-US' )
110
+ bundleUk = new FluentBundle ( 'uk-UA' )
111
+
112
+ const fluent = createFluentVue ( {
113
+ bundles : [ bundleUk ] ,
114
+ } )
115
+
116
+ const child = {
117
+ template : '<span>{{ $t("child") }}</span>' ,
118
+ fluent : {
119
+ 'en-US' : new FluentResource ( ftl `
120
+ child = Child message
121
+ ` ) ,
122
+ 'uk-UA' : new FluentResource ( ftl `
123
+ child = Повідомлення
124
+ ` ) ,
125
+ } ,
126
+ }
127
+
128
+ const component = {
129
+ components : {
130
+ child,
131
+ } ,
132
+ data : ( ) => ( {
133
+ show : true ,
134
+ } ) ,
135
+ template : '<child v-if="show" />' ,
136
+ }
137
+
138
+ // Act
139
+ const mounted = mountWithFluent ( fluent , component )
140
+
141
+ expect ( mounted . html ( ) ) . toEqual ( '<span>Повідомлення</span>' )
142
+
143
+ await mounted . setData ( { show : false } )
144
+
145
+ if ( isVue3 )
146
+ expect ( mounted . html ( ) ) . toEqual ( '<!--v-if-->' )
147
+ else
148
+ expect ( mounted . html ( ) ) . toEqual ( '' )
149
+
150
+ fluent . bundles = [ bundleEn ]
151
+
152
+ await mounted . setData ( { show : true } )
153
+
154
+ // Assert
155
+ expect ( mounted . html ( ) ) . toEqual ( '<span>Child message</span>' )
156
+ } )
157
+
158
+ it ( 'useFluent updates locale even if component is unmounted github.com/orgs/fluent-vue/discussions/834' , async ( ) => {
159
+ // Arrange
160
+ bundleEn = new FluentBundle ( 'en-US' )
161
+ bundleUk = new FluentBundle ( 'uk-UA' )
162
+
163
+ const fluent = createFluentVue ( {
164
+ bundles : [ bundleUk ] ,
165
+ } )
166
+
167
+ const child = {
168
+ setup ( ) {
169
+ useFluent ( )
170
+ } ,
171
+ template : '<span>{{ $t("child") }}</span>' ,
172
+ fluent : {
173
+ 'en-US' : new FluentResource ( ftl `
174
+ child = Child message
175
+ ` ) ,
176
+ 'uk-UA' : new FluentResource ( ftl `
177
+ child = Повідомлення
178
+ ` ) ,
179
+ } ,
180
+ }
181
+
182
+ const component = {
183
+ components : {
184
+ child,
185
+ } ,
186
+ data : ( ) => ( {
187
+ show : true ,
188
+ } ) ,
189
+ template : '<child v-if="show" />' ,
190
+ }
191
+
192
+ // Act
193
+ const mounted = mountWithFluent ( fluent , component )
194
+
195
+ expect ( mounted . html ( ) ) . toEqual ( '<span>Повідомлення</span>' )
196
+
197
+ await mounted . setData ( { show : false } )
198
+
199
+ if ( isVue3 )
200
+ expect ( mounted . html ( ) ) . toEqual ( '<!--v-if-->' )
201
+ else
202
+ expect ( mounted . html ( ) ) . toEqual ( '' )
203
+
204
+ fluent . bundles = [ bundleEn ]
205
+
206
+ await mounted . setData ( { show : true } )
207
+
208
+ // Assert
209
+ expect ( mounted . html ( ) ) . toEqual ( '<span>Child message</span>' )
210
+ } )
211
+
107
212
it ( 'works when dynamically adding bundles' , async ( ) => {
108
213
// Arrange
109
214
bundleEn = new FluentBundle ( 'en-US' )
0 commit comments