Repository navigation
Expand file tree
/
Copy pathLocalizationController.spec.ts
More file actions
250 lines (191 loc) · 8.66 KB
/
Copy pathLocalizationController.spec.ts
File metadata and controls
250 lines (191 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { ApplicationEvent, InternalEventInterface } from '@standardnotes/snjs'
import { addToast, ToastType } from '@standardnotes/toast'
import { clearPersistedLocale, getPersistedLocale, persistLocale } from '@/Utils/LocalePersistence'
import { LocalizationController } from './LocalizationController'
import { localizationStore } from './LocalizationStore'
const catalog = { en_US: 'English', fr_FR: 'Français' }
const workspaceIdentifier = 'workspace-1'
let mockCurrentLocale = 'en_US'
const mockLocaleService = {
getCurrentLocale: (): string => mockCurrentLocale,
getAvailableLocales: jest.fn(),
loadAndActivateLocale: jest.fn(),
}
jest.mock('./WebLocaleService', () => ({ getWebLocaleService: () => mockLocaleService }))
jest.mock('@/Utils/DateLocale', () => ({ configureDateLocale: jest.fn() }))
jest.mock('@/Application/Device/SyncDesktopMainProcessLocalization', () => ({
syncDesktopMainProcessLocalization: jest.fn(),
}))
jest.mock('@/Application/Device/SyncMobileNativeLocalization', () => ({ syncMobileNativeLocalization: jest.fn() }))
jest.mock('@standardnotes/toast', () => ({
addToast: jest.fn(),
ToastType: { Success: 'success', Error: 'error' },
}))
describe('LocalizationController', () => {
const originalLocation = window.location
let reload: jest.Mock
let localizationEnabled: boolean
const featuresController = {
isLocalizationEnabled: () => localizationEnabled,
toggleLocalization: () => {
localizationEnabled = !localizationEnabled
},
}
const createController = () =>
new LocalizationController(
featuresController as never,
workspaceIdentifier,
{} as never,
{ getRoute: () => ({ type: 'none' }) } as never,
{ addEventHandler: jest.fn() } as never,
)
const sendEvent = (controller: LocalizationController, type: ApplicationEvent) =>
controller.handleEvent({ type } as InternalEventInterface)
const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 0))
const setBrowserLanguages = (languages: string[]) => {
jest.spyOn(window.navigator, 'languages', 'get').mockReturnValue(languages)
}
const bootWithLocale = async (locale: string) => {
mockCurrentLocale = locale
localizationStore.setCurrentLocale(locale)
const controller = createController()
await sendEvent(controller, ApplicationEvent.LocalDataLoaded)
await flushPromises()
return controller
}
beforeEach(() => {
reload = jest.fn()
Object.defineProperty(window, 'location', {
configurable: true,
writable: true,
value: { ...originalLocation, protocol: 'http:', reload },
})
localizationEnabled = true
setBrowserLanguages(['en-US'])
localStorage.setItem('sn-has-seen-localization-spotlight', 'true')
mockCurrentLocale = 'en_US'
mockLocaleService.getAvailableLocales.mockResolvedValue(catalog)
mockLocaleService.loadAndActivateLocale.mockImplementation(async (locale: string) => {
mockCurrentLocale = locale
})
clearPersistedLocale(workspaceIdentifier)
sessionStorage.clear()
localizationStore.setAvailableLocales({})
localizationStore.setCurrentLocale('en_US')
})
afterAll(() => {
Object.defineProperty(window, 'location', { configurable: true, writable: true, value: originalLocation })
})
it('shows a toast after the reload when switching languages', async () => {
const controller = await bootWithLocale('en_US')
expect(await controller.changeLocaleAndReload('fr_FR')).toBe(true)
expect(reload).toHaveBeenCalledTimes(1)
expect(getPersistedLocale(workspaceIdentifier)).toBe('fr_FR')
expect(addToast).not.toHaveBeenCalled()
await bootWithLocale('fr_FR')
expect(addToast).toHaveBeenCalledWith(
expect.objectContaining({ type: ToastType.Success, message: expect.stringContaining('Français') }),
)
})
it('does not show a toast on a regular reload', async () => {
persistLocale(workspaceIdentifier, 'fr_FR')
await bootWithLocale('fr_FR')
expect(addToast).not.toHaveBeenCalled()
expect(reload).not.toHaveBeenCalled()
})
it('keeps the boot language when the saved language failed to load at startup', async () => {
persistLocale(workspaceIdentifier, 'fr_FR')
const controller = await bootWithLocale('en_US')
expect(reload).not.toHaveBeenCalled()
expect(localizationStore.currentLocale).toBe('en_US')
expect(localizationStore.availableLocales).toEqual(catalog)
expect(getPersistedLocale(workspaceIdentifier)).toBe('fr_FR')
expect(await controller.changeLocaleAndReload('en_US')).toBe(true)
expect(getPersistedLocale(workspaceIdentifier)).toBe('en_US')
expect(reload).toHaveBeenCalledTimes(1)
})
it('reloads into English when localization is off but the page is in another language', async () => {
localizationEnabled = false
await bootWithLocale('fr_FR')
expect(reload).toHaveBeenCalledTimes(1)
expect(sessionStorage.getItem('sn-pending-locale-switch')).toBe('en_US')
})
it('clears the saved language on sign out', async () => {
persistLocale(workspaceIdentifier, 'fr_FR')
const controller = await bootWithLocale('fr_FR')
await sendEvent(controller, ApplicationEvent.SignedOut)
expect(getPersistedLocale(workspaceIdentifier)).toBeUndefined()
})
it('loads the language list while localization is still off', async () => {
localizationEnabled = false
await bootWithLocale('en_US')
expect(localizationStore.availableLocales).toEqual(catalog)
})
it('keeps the saved language and reports an error when the catalog fails to load', async () => {
persistLocale(workspaceIdentifier, 'fr_FR')
mockLocaleService.getAvailableLocales.mockRejectedValue(new Error('offline'))
jest.spyOn(console, 'error').mockImplementation(() => undefined)
await bootWithLocale('fr_FR')
expect(getPersistedLocale(workspaceIdentifier)).toBe('fr_FR')
expect(localizationStore.availableLocalesFailedToLoad).toBe(true)
expect(reload).not.toHaveBeenCalled()
})
it('reloads once, after the switch animation, when turning localization on', async () => {
localizationEnabled = false
setBrowserLanguages(['fr-FR'])
const controller = await bootWithLocale('en_US')
let finishAnimation: () => void = () => undefined
const animation = new Promise<void>((resolve) => {
finishAnimation = resolve
})
const toggling = controller.toggleLocalization(animation)
await sendEvent(controller, ApplicationEvent.FeaturesAvailabilityChanged)
await flushPromises()
expect(reload).not.toHaveBeenCalled()
finishAnimation()
await toggling
expect(reload).toHaveBeenCalledTimes(1)
expect(getPersistedLocale(workspaceIdentifier)).toBe('fr_FR')
expect(sessionStorage.getItem('sn-pending-locale-switch')).toBe('fr_FR')
})
it('does not reload when turning localization on while already in the resolved language', async () => {
localizationEnabled = false
const controller = await bootWithLocale('en_US')
expect(await controller.toggleLocalization(Promise.resolve())).toBe(false)
expect(reload).not.toHaveBeenCalled()
expect(getPersistedLocale(workspaceIdentifier)).toBe('en_US')
expect(localizationStore.availableLocales).toEqual(catalog)
})
it('does not reload when turning localization off while already in English', async () => {
localizationEnabled = true
const controller = await bootWithLocale('en_US')
expect(await controller.toggleLocalization(Promise.resolve())).toBe(false)
expect(reload).not.toHaveBeenCalled()
expect(getPersistedLocale(workspaceIdentifier)).toBeUndefined()
})
it('can toggle localization on and off again without reloading', async () => {
localizationEnabled = false
const controller = await bootWithLocale('en_US')
expect(await controller.toggleLocalization(Promise.resolve())).toBe(false)
expect(await controller.toggleLocalization(Promise.resolve())).toBe(false)
expect(reload).not.toHaveBeenCalled()
expect(localizationEnabled).toBe(false)
expect(getPersistedLocale(workspaceIdentifier)).toBeUndefined()
})
describe('labs spotlight', () => {
beforeEach(() => {
localizationEnabled = false
localStorage.removeItem('sn-has-seen-localization-spotlight')
})
it('opens on launch when it has not been seen', async () => {
const controller = await bootWithLocale('en_US')
expect(controller.labsSpotlightOpen).toBe(true)
})
it('stays dismissed when the feature availability is checked again', async () => {
const controller = await bootWithLocale('en_US')
controller.dismissLabsSpotlight()
await sendEvent(controller, ApplicationEvent.FeaturesAvailabilityChanged)
expect(controller.labsSpotlightOpen).toBe(false)
})
})
})