Skip to content

Commit cbce7df

Browse files
authored
fix: i18n test case (#7778)
1 parent d9f1ef6 commit cbce7df

File tree

13 files changed

+83
-55
lines changed

13 files changed

+83
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ node_modules/
1111
.eslintcache
1212

1313
dist/
14+
dist-ssg/
1415
doc_build/
1516
!**/compiled/**/dist
1617
coverage/

tests/integration/i18n/app-csr/tests/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('app-csr-i18n', () => {
3939
const targetText = await page.evaluate(el => el?.textContent, root);
4040
expect(targetText?.trim()).toEqual('Hello World');
4141
await page.click('#zh-button');
42-
await new Promise(resolve => setTimeout(resolve, 3000));
42+
await new Promise(resolve => setTimeout(resolve, 5000));
4343
const targetTextZh = await page.evaluate(el => el?.textContent, root);
4444
expect(targetTextZh?.trim()).toEqual('你好,世界');
4545
});
@@ -54,7 +54,7 @@ describe('app-csr-i18n', () => {
5454
);
5555
expect(targetTextAbout?.trim()).toEqual('About');
5656
await page.click('#zh-button');
57-
await new Promise(resolve => setTimeout(resolve, 3000));
57+
await new Promise(resolve => setTimeout(resolve, 5000));
5858
const targetTextAboutZh = await page.evaluate(
5959
el => el?.textContent,
6060
rootAbout,

tests/integration/i18n/app-ssr/modern.ssg.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export default defineConfig({
77
ssr: process.env.NODE_ENV === 'development',
88
},
99
output: {
10+
distPath: {
11+
root: 'dist-ssg',
12+
},
1013
ssg: {
1114
routes: ['/zh', '/en'],
1215
},

tests/integration/i18n/app-ssr/tests/index.test.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ describe('app-ssr-i18n', () => {
5050
const text = await page.$('#key');
5151
const targetText = await page.evaluate(el => el?.textContent, text);
5252
expect(targetText?.trim()).toEqual('你好,世界');
53-
page.click('#en-button');
54-
await new Promise(resolve => setTimeout(resolve, 3000));
55-
const textEn = await page.$('#key');
56-
const targetTextEn = await page.evaluate(el => el?.textContent, textEn);
57-
expect(targetTextEn?.trim()).toEqual('Hello World');
53+
await page.click('#en-button');
54+
await new Promise(resolve => setTimeout(resolve, 5000));
55+
await page.waitForFunction(
56+
() => {
57+
const el = document.querySelector('#key');
58+
return (
59+
el && el.textContent !== null && el.textContent === 'Hello World'
60+
);
61+
},
62+
{ timeout: 10000 },
63+
);
5864
});
5965
test('page-en', async () => {
6066
const response = await page.goto(`http://localhost:${appPort}/en`, {
@@ -65,10 +71,14 @@ describe('app-ssr-i18n', () => {
6571
const text = await page.$('#key');
6672
const targetText = await page.evaluate(el => el?.textContent, text);
6773
expect(targetText?.trim()).toEqual('Hello World');
68-
page.click('#zh-button');
69-
await new Promise(resolve => setTimeout(resolve, 3000));
70-
const textZh = await page.$('#key');
71-
const targetTextZh = await page.evaluate(el => el?.textContent, textZh);
72-
expect(targetTextZh?.trim()).toEqual('你好,世界');
74+
await page.click('#zh-button');
75+
await new Promise(resolve => setTimeout(resolve, 5000));
76+
await page.waitForFunction(
77+
() => {
78+
const el = document.querySelector('#key');
79+
return el && el.textContent !== null && el.textContent === '你好,世界';
80+
},
81+
{ timeout: 10000 },
82+
);
7383
});
7484
});

tests/integration/i18n/app-ssr/tests/ssg.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ describe('ssg', () => {
1111
const appDir = projectDir;
1212
await modernBuild(appDir, ['--config', 'modern.ssg.config.ts']);
1313

14-
const zhHtmlPath = path.join(appDir, './dist/html/main/zh/index.html');
15-
const enHtmlPath = path.join(appDir, './dist/html/main/en/index.html');
14+
const zhHtmlPath = path.join(appDir, './dist-ssg/html/main/zh/index.html');
15+
const enHtmlPath = path.join(appDir, './dist-ssg/html/main/en/index.html');
1616
const zhContent = fs.readFileSync(zhHtmlPath, 'utf-8');
1717
const enContent = fs.readFileSync(enHtmlPath, 'utf-8');
1818
expect(zhContent).toMatch('你好,世界');

tests/integration/i18n/routes-csr/test/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ describe('router-csr-i18n', () => {
7272
const text = await page.$('#about');
7373
const targetText = await page.evaluate(el => el?.textContent, text);
7474
expect(targetText?.trim()).toEqual('关于');
75-
page.click('#en-button');
76-
await new Promise(resolve => setTimeout(resolve, 3000));
75+
await page.click('#en-button');
76+
await new Promise(resolve => setTimeout(resolve, 5000));
7777
const textEn = await page.$('#about');
7878
const targetTextEn = await page.evaluate(el => el?.textContent, textEn);
7979
expect(targetTextEn?.trim()).toEqual('About');
@@ -85,8 +85,8 @@ describe('router-csr-i18n', () => {
8585
const text = await page.$('#about');
8686
const targetText = await page.evaluate(el => el?.textContent, text);
8787
expect(targetText?.trim()).toEqual('About');
88-
page.click('#zh-button');
89-
await new Promise(resolve => setTimeout(resolve, 3000));
88+
await page.click('#zh-button');
89+
await new Promise(resolve => setTimeout(resolve, 5000));
9090
const textZh = await page.$('#about');
9191
const targetTextZh = await page.evaluate(el => el?.textContent, textZh);
9292
expect(targetTextZh?.trim()).toEqual('关于');

tests/integration/i18n/routes-ssr/modern.ssg.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export default defineConfig({
77
ssr: process.env.NODE_ENV === 'development',
88
},
99
output: {
10+
distPath: {
11+
root: 'dist-ssg',
12+
},
1013
ssg: {
1114
routes: ['/zh/about', '/en/about', '/en', '/zh'],
1215
},

tests/integration/i18n/routes-ssr/src/i18n.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,23 @@ import originalI18next from 'i18next';
22

33
const i18next = originalI18next.createInstance();
44

5-
export { i18next };
5+
i18next.init({
6+
lng: 'en',
7+
fallbackLng: 'en',
8+
resources: {
9+
en: {
10+
translation: {
11+
key: 'Hello World',
12+
about: 'About',
13+
},
14+
},
15+
zh: {
16+
translation: {
17+
key: '你好,世界',
18+
about: '关于',
19+
},
20+
},
21+
},
22+
});
23+
24+
export default i18next;
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
import { defineRuntimeConfig } from '@modern-js/runtime';
2-
import { i18next } from './i18n';
2+
import i18next from './i18n';
33

44
export default defineRuntimeConfig({
55
i18n: {
66
i18nInstance: i18next,
7-
initOptions: {
8-
resources: {
9-
en: {
10-
translation: {
11-
key: 'Hello World',
12-
about: 'About',
13-
},
14-
},
15-
zh: {
16-
translation: {
17-
key: '你好,世界',
18-
about: '关于',
19-
},
20-
},
21-
},
22-
},
237
},
248
});

tests/integration/i18n/routes-ssr/src/routes/[lang]/page.data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { i18next } from '../../i18n';
1+
import i18next from '../../i18n';
22
export interface ProfileData {
33
/* some types */
44
data: string;

0 commit comments

Comments
 (0)