Skip to content

Commit 69c5def

Browse files
committed
tests: Improve testing
1 parent 6fc2cb7 commit 69c5def

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

src/loader.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,11 @@ const convertToDotsSyntax = (list) => {
106106

107107
return flatten(list);
108108
}
109+
110+
export const reset = (folderPath) => {
111+
const dir = fs.readdirSync(folderPath);
112+
113+
dir.filter(file => file.match(/^php_/)).forEach(file => {
114+
fs.unlinkSync(folderPath + file);
115+
});
116+
}

test/loader.test.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import fs from 'fs';
2-
import { parseAll, parse, hasPhpTranslations } from '../src/loader';
2+
import { parseAll, parse, hasPhpTranslations, reset } from '../src/loader';
33

4-
beforeEach(() => {
5-
const folderPath = __dirname + '/fixtures/lang/';
6-
const dir = fs.readdirSync(folderPath);
7-
8-
dir.filter(file => file.match(/^php_/)).forEach(file => {
9-
fs.unlinkSync(folderPath + file);
10-
});
11-
});
4+
beforeEach(() => reset(__dirname + '/fixtures/lang/'));
125

136
it('creates a file for each lang', () => {
147
const files = parseAll(__dirname + '/fixtures/lang/');

test/setup.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mount } from '@vue/test-utils'
22
import { i18nVue } from '../src'
3+
import { parseAll } from '../src/loader'
34

45
global.mountPlugin = async (template = '<div />', lang = 'pt') => {
56
const wrapper = mount({ template }, {
@@ -15,3 +16,11 @@ global.mountPlugin = async (template = '<div />', lang = 'pt') => {
1516

1617
return wrapper;
1718
}
19+
20+
global.mixLoader = () => {
21+
parseAll(__dirname + '/fixtures/lang/');
22+
23+
process.env = Object.assign(process.env, {
24+
LARAVEL_VUE_I18N_HAS_PHP: 'true',
25+
});
26+
}

test/translate.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { mount } from '@vue/test-utils'
22
import { i18nVue, trans, trans_choice, loadLanguageAsync, reset, getActiveLanguage, isLoaded, wTrans } from '../src'
3+
import { reset as resetLoader } from '../src/loader'
34

45
beforeEach(() => reset());
6+
afterEach(() => resetLoader(__dirname + '/fixtures/lang/'));
57

68
it('translates with $t mixin', async () => {
79
const wrapper = await global.mountPlugin(`<h1 v-text="$t('Welcome!')" />`);
@@ -136,3 +138,10 @@ it('resolves translated data with require', async () => {
136138

137139
expect(trans('Welcome!')).toBe('Bem-vindo!');
138140
});
141+
142+
it('resolves translated data from .php files', async () => {
143+
global.mixLoader();
144+
const wrapper = await global.mountPlugin(`<h1 v-text="$t('auth.failed')" />`);
145+
146+
expect(wrapper.html()).toBe('<h1>As credenciais indicadas não coincidem com as registadas no sistema.</h1>')
147+
});

0 commit comments

Comments
 (0)