Skip to content

Commit 26a3f5c

Browse files
committed
feat: Add reset method
1 parent b2c1a6b commit 26a3f5c

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/index.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import { ReplacementsInterface } from './interfaces/replacements'
55
import { choose } from './pluralization'
66

77
/**
8-
* The Default language will be used if there is no lang provided.
8+
* The default options, for the plugin.
99
*/
10-
const DEFAULT_LANG: string = document.documentElement.lang || 'en'
10+
const DEFAULT_OPTIONS: OptionsInterface = {
11+
lang: document.documentElement.lang || 'en',
12+
resolve: (lang: string) => new Promise((resolve) => resolve({ default: {} }))
13+
}
1114

1215
/**
1316
* Stores the current options.
1417
*/
15-
let options: OptionsInterface = {
16-
lang: DEFAULT_LANG,
17-
resolve: (lang: string) => new Promise((resolve) => resolve({ default: {} }))
18-
}
18+
let options: OptionsInterface = DEFAULT_OPTIONS;
1919

2020
/**
2121
* Stores the loaded languages.
2222
*/
23-
const loaded: LanguageInterface[] = []
23+
let loaded: LanguageInterface[] = []
2424

2525
/**
2626
* The active messages to use.
@@ -106,6 +106,18 @@ function makeReplacements(message: string, replacements?: ReplacementsInterface)
106106
return message
107107
}
108108

109+
/**
110+
* Resets all the data stored in memory.
111+
*/
112+
export const reset = (): void => {
113+
loaded = [];
114+
options = DEFAULT_OPTIONS;
115+
116+
for (const [key] of Object.entries(activeMessages)) {
117+
activeMessages[key] = null;
118+
}
119+
}
120+
109121
/**
110122
* The Vue Plugin. to be used on your Vue app like this: `app.use(i18nVue)`
111123
*/

test/translate.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { mount } from '@vue/test-utils'
2-
import { i18nVue, trans, trans_choice, loadLanguageAsync } from '../src'
2+
import { i18nVue, trans, trans_choice, loadLanguageAsync, reset } from '../src'
3+
4+
beforeEach(() => reset());
35

46
it('translates with $t mixin', async () => {
57
const wrapper = await global.mountPlugin(`<h1 v-text="$t('Welcome!')" />`);
@@ -13,6 +15,19 @@ it('translates with "trans" helper', async () => {
1315
expect(trans('Welcome!')).toBe('Bem-vindo!');
1416
})
1517

18+
it('returns the same message if there is no resolve method provided', async () => {
19+
const wrapper = mount({ template: `<h1>{{ $t('Welcome!') }}</h1>` }, {
20+
global: {
21+
plugins: [i18nVue]
22+
}
23+
});
24+
25+
await new Promise(resolve => setTimeout(resolve));
26+
27+
expect(wrapper.html()).toBe('<h1>Welcome!</h1>');
28+
expect(trans('Welcome!')).toBe('Welcome!');
29+
})
30+
1631
it('returns the same string given if it is not found on the lang file', async () => {
1732
const wrapper = await global.mountPlugin(`<h1>{{ $t('This has no translation') }}</h1>`);
1833

0 commit comments

Comments
 (0)