Skip to content

Commit c024baf

Browse files
committed
Moved callback implementation, added test
1 parent 35eee8a commit c024baf

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,7 @@ export class I18n {
158158
* Loads the language.
159159
*/
160160
load(): void {
161-
const lang = this.getActiveLanguage()
162-
if (isServer) {
163-
this.loadLanguage(lang)
164-
this.options.onLoad(lang)
165-
} else {
166-
this.loadLanguageAsync(lang).then(this.options.onLoad)
167-
}
161+
this[isServer ? 'loadLanguage' : 'loadLanguageAsync'](this.getActiveLanguage())
168162
}
169163

170164
/**
@@ -312,6 +306,7 @@ export class I18n {
312306
}
313307
}
314308

309+
this.options.onLoad(lang)
315310
return lang
316311
}
317312

test/class.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,19 @@ it('allows resetting all data', async () => {
6868
expect(i18n.getActiveLanguage()).toBe('en')
6969
expect(i18n.trans('Welcome!')).toBe('Welcome!')
7070
})
71+
72+
it('calls onLoad when loaded', async () => {
73+
const onLoadFunction = jest.fn()
74+
75+
const i18n = new I18n({
76+
lang: 'pt',
77+
resolve: lang => import(`./fixtures/lang/${lang}.json`),
78+
onLoad: onLoadFunction
79+
})
80+
81+
await i18n.loadLanguageAsync('en')
82+
83+
expect(onLoadFunction).toHaveBeenCalledTimes(2)
84+
expect(onLoadFunction).toHaveBeenCalledWith('en')
85+
expect(onLoadFunction).toHaveBeenCalledWith('pt')
86+
})

0 commit comments

Comments
 (0)