Skip to content

Commit

Permalink
wip: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Dec 15, 2023
1 parent fc33301 commit 1127b80
Show file tree
Hide file tree
Showing 33 changed files with 623 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"i18n-ally.localesPaths": ["playground/spa/src/locales"],
"i18n-ally.keystyle": "flat",
"i18n-ally.keystyle": "nested",
"i18n-ally.enabledParsers": ["json", "json5", "yaml"],
"i18n-ally.enabledFrameworks": ["react", "i18next", "react-i18next"],
"i18n-ally.namespace": true,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"@types/debug": "^4.1.12",
"@types/js-yaml": "^4.0.9",
"@types/language-tags": "^1.0.4",
"@types/node": "^20.10.4",
"@types/react": "^18.2.45",
"bumpp": "^9.2.0",
"conventional-changelog-cli": "^4.1.0",
Expand Down
1 change: 1 addition & 0 deletions playground/spa/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dist-ssr
# Editor directories and files
.vscode/*
!.vscode/extensions.json
!.vscode/settings.json
.idea
.DS_Store
*.suo
Expand Down
7 changes: 3 additions & 4 deletions playground/spa/__tests__/spa.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ describe.skipIf(isBuild)('server related tests', () => {
describe('hmr', () => {
test('should trigger hmr when locale files changed', async () => {
await page.click('#en')
editFile('src/locales/en/test.json', (text) => {
return text.replace(`"key": "en"`, `"key": "updated en"`)
})

editFile('src/locales/en/test.json', (text) => text.replace(`"key": "en"`, `"key": "updated en"`))

await untilUpdated(() => page.textContent('#language'), 'updated en')
})
Expand Down Expand Up @@ -120,7 +119,7 @@ describe.skipIf(isBuild)('server related tests', () => {

test('should page reload when locale file added', async () => {
const request = page.waitForResponse(/src\/App\.tsx$/, { timeout: 500 })
addFile('src/locales/en/test2.json', '{}')
addFile('src/locales/en/test.json', '{}')
const response = await request.then(() => ({ status: () => 1 }))
expect(response.status()).toBe(1)
})
Expand Down
3 changes: 3 additions & 0 deletions playground/spa/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default defineConfig({
react(),
i18nDetector({
root: __dirname,
localesPaths: ['./src/locales'],
namespace: true,
dotVscodePath: false,
}),
],
})
25 changes: 25 additions & 0 deletions playground/vscode-setting/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
!.vscode/settings.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
6 changes: 6 additions & 0 deletions playground/vscode-setting/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"i18n-ally.localesPaths": ["src/locales"],
"i18n-ally.keystyle": "nested",
"i18n-ally.namespace": true,
"i18n-ally.pathMatcher": "{locale}/{namespaces}.json"
}
128 changes: 128 additions & 0 deletions playground/vscode-setting/__tests__/vscode-setting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import {
addFile,
editFile,
isBuild,
isServe,
page,
removeDir,
removeFile,
renameDir,
untilBrowserLogAfter,
untilUpdated,
viteTestUrl,
} from '~utils'
import { describe, expect, test } from 'vitest'

describe('e2e', () => {
test('should render en by default', async () => {
expect(await page.textContent('#language')).toBe('en')
})

test.runIf(isServe)('should lazyload locale js after click', async () => {
let request = page.waitForResponse((res) => res.url().includes('@i18n/virtual:i18n-zh'), {
timeout: 500,
})
await page.click('#zh')
let response = await request.then(() => ({ status: () => 1 }))
expect(response.status()).toBe(1)

request = page.waitForResponse((res) => res.url().includes('@i18n/virtual:i18n-de'), {
timeout: 500,
})
await page.click('#de')
response = await request.then(() => ({ status: () => 1 }))
expect(response.status()).toBe(1)
})

test('should change language', async () => {
await page.click('#zh')

await untilUpdated(() => page.textContent('#language'), '中文')

await page.click('#de')

await untilUpdated(() => page.textContent('#language'), 'Deutsch')

await page.click('#en')
})

test('should set html attribute lang', async () => {
expect(await page.getAttribute('html', 'lang')).toBe('en')

await page.click('#zh')

expect(await page.getAttribute('html', 'lang')).toBe('zh')

await page.click('#de')

expect(await page.getAttribute('html', 'lang')).toBe('de')

await page.click('#en')
})

test('should set url query', async () => {
await page.click('#zh')
let currentUrl = page.url()
let urlSearchParams = new URLSearchParams(currentUrl.split('?')[1])
let lang = urlSearchParams.get('lang')
expect(lang).toBe('zh')

await page.click('#en')
currentUrl = page.url()
urlSearchParams = new URLSearchParams(currentUrl.split('?')[1])
lang = urlSearchParams.get('lang')
expect(lang).toBe('en')
})

test('should fallback to fallbackLng when language is not found', async () => {
await untilBrowserLogAfter(
() => page.goto(`${viteTestUrl}/?lang=not-exist`),
/.*Language 'not-exist' is detected.*/,
)
})
})

describe.skipIf(isBuild)('server related tests', () => {
describe('hmr', () => {
test('should trigger hmr when locale files changed', async () => {
await page.click('#en')
editFile('src/locales/en/test.json', (text) => {
return text.replace(`"key": "en"`, `"key": "updated en"`)
})

await untilUpdated(() => page.textContent('#language'), 'updated en')
})

test('should page reload when locale dir removed', async () => {
const request = page.waitForResponse(/src\/App\.tsx$/, { timeout: 500 })
removeDir('src/locales/zh-tw/')
const response = await request.then(() => ({ status: () => 1 }))
expect(response.status()).toBe(1)
})

test('should page reload when locale files removed', async () => {
const request = page.waitForResponse(/src\/App\.tsx$/, { timeout: 500 })
removeFile('src/locales/de/test.json')
const response = await request.then(() => ({ status: () => 1 }))
expect(response.status()).toBe(1)
})

test('should page reload when locale dir name changed', async () => {
const request = page.waitForResponse(/src\/App\.tsx$/, { timeout: 500 })

renameDir('src/locales/en/', 'src/locales/en-US/')
let response = await request.then(() => ({ status: () => 1 }))
expect(response.status()).toBe(1)
renameDir('src/locales/en-US/', 'src/locales/en/')
response = await request.then(() => ({ status: () => 1 }))
expect(response.status()).toBe(1)
})

test('should page reload when locale file added', async () => {
const request = page.waitForResponse(/src\/App\.tsx$/, { timeout: 500 })
addFile('src/locales/en/test.json', '{}')
const response = await request.then(() => ({ status: () => 1 }))
expect(response.status()).toBe(1)
})
})
})
16 changes: 16 additions & 0 deletions playground/vscode-setting/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>vite-plugin-i18n-detector-demo</title>
</head>

<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>

</html>
27 changes: 27 additions & 0 deletions playground/vscode-setting/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "spa",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "cross-env DEBUG=vite-plugin-i18n* vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"i18next": "^23.5.1",
"i18next-browser-languagedetector": "^7.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^13.2.2",
"vite-plugin-i18n-detector": "workspace:*"
},
"devDependencies": {
"@types/react": "^18.2.28",
"@types/react-dom": "^18.2.13",
"@vitejs/plugin-react": "^4.1.0",
"cross-env": "^7.0.3",
"typescript": "^5.2.2",
"vite": "^4.4.11"
}
}
1 change: 1 addition & 0 deletions playground/vscode-setting/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions playground/vscode-setting/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
62 changes: 62 additions & 0 deletions playground/vscode-setting/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useReducer } from 'react'
import { useTranslation } from 'react-i18next'
import './App.css'

function App() {
const { t, i18n } = useTranslation()

const [, update] = useReducer((x) => ++x, 0)

return (
<div className='App'>
<h2>{t('test.tip')}</h2>

<div id='language' style={{ marginBottom: '16px' }}>
{t('test.key')}
</div>

<button
onClick={async () => {
await i18n.changeLanguage('zh')
update()
}}
id='zh'
style={{ marginRight: 8 }}
>
中文
</button>
<button
onClick={async () => {
await i18n.changeLanguage('en')
update()
}}
id='en'
style={{ marginRight: 8 }}
>
english
</button>
<button
onClick={async () => {
await i18n.changeLanguage('de')
update()
}}
id='de'
style={{ marginRight: 8 }}
>
Deutsch
</button>
<button
onClick={async () => {
await i18n.changeLanguage('zh-tw')
update()
}}
id='zh-tw'
style={{ marginRight: 8 }}
>
繁體
</button>
</div>
)
}

export default App
1 change: 1 addition & 0 deletions playground/vscode-setting/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions playground/vscode-setting/src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const lookupTarget = 'lang'
export const fallbackLng = 'en'
Loading

0 comments on commit 1127b80

Please sign in to comment.