Skip to content

Commit 34fb631

Browse files
authored
add SSR tests (#84)
1 parent 51c29c2 commit 34fb631

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@typescript-eslint/eslint-plugin": "^3.7.0",
3535
"@typescript-eslint/parser": "^3.7.0",
3636
"@typescript-eslint/typescript-estree": "^3.7.0",
37+
"@vue/server-renderer": "^3.0.0-rc.5",
3738
"brotli": "^1.3.2",
3839
"chalk": "^4.0.0",
3940
"convert-hrtime": "^3.0.0",

src/directive.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type VTDirectiveValue = {
1717
locale?: Locale
1818
args?: NamedValue
1919
choice?: number
20+
plural?: number
2021
}
2122

2223
function getComposer<Messages, DateTimeFormats, NumberFormats>(
@@ -97,7 +98,7 @@ function parseValue(value: unknown): VTDirectiveValue {
9798
}
9899

99100
function makeParams(value: VTDirectiveValue): unknown[] {
100-
const { path, locale, args, choice } = value
101+
const { path, locale, args, choice, plural } = value
101102
const options = {} as TranslateOptions
102103
const named: NamedValue = args || {}
103104

@@ -109,5 +110,9 @@ function makeParams(value: VTDirectiveValue): unknown[] {
109110
options.plural = choice
110111
}
111112

113+
if (isNumber(plural)) {
114+
options.plural = plural
115+
}
116+
112117
return [path, named, options]
113118
}

test/diretive.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('basic', () => {
5656
const App = defineComponent({
5757
setup() {
5858
// <p v-t="msg"></p>
59-
const msg = ref('binding')
59+
const msg = ref('hello')
6060
const t = resolveDirective('t')
6161
return () => {
6262
return withDirectives(h('p'), [[t, msg.value]])

test/ssr.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { defineComponent, createSSRApp } from 'vue'
2+
import { renderToString } from '@vue/server-renderer'
3+
import { createI18n, useI18n } from '../src/index'
4+
5+
test('composable mode', async () => {
6+
const i18n = createI18n({
7+
locale: 'en',
8+
messages: {}
9+
})
10+
11+
const App = defineComponent({
12+
template: `<p>{{ t('hello') }}</p>`,
13+
setup() {
14+
return useI18n({
15+
locale: 'ja',
16+
inheritLocale: false,
17+
messages: {
18+
ja: { hello: 'こんにちは!' },
19+
en: { hello: 'hello!' }
20+
}
21+
})
22+
}
23+
})
24+
const app = createSSRApp(App)
25+
app.use(i18n)
26+
27+
expect(await renderToString(app)).toMatch(`<p>こんにちは!</p>`)
28+
})
29+
30+
test('legacy mode', async () => {
31+
const i18n = createI18n({
32+
legacy: true,
33+
locale: 'ja',
34+
messages: {
35+
ja: { hello: 'こんにちは!' },
36+
en: { hello: 'hello!' }
37+
}
38+
})
39+
40+
const App = defineComponent({
41+
template: `<p>{{ $t('hello') }}</p>`
42+
})
43+
const app = createSSRApp(App)
44+
app.use(i18n)
45+
46+
expect(await renderToString(app)).toMatch(`<p>こんにちは!</p>`)
47+
})
48+
49+
test('component: i18n-t', async () => {
50+
const i18n = createI18n({
51+
locale: 'en',
52+
messages: {}
53+
})
54+
55+
const App = defineComponent({
56+
template: `<i18n-t tag="p" keypath="hello"/>`,
57+
setup() {
58+
return useI18n({
59+
locale: 'ja',
60+
inheritLocale: false,
61+
messages: {
62+
ja: { hello: 'こんにちは!' },
63+
en: { hello: 'hello!' }
64+
}
65+
})
66+
}
67+
})
68+
const app = createSSRApp(App)
69+
app.use(i18n)
70+
71+
expect(await renderToString(app)).toMatch(`<p>こんにちは!</p>`)
72+
})

yarn.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,14 @@
10231023
"@vue/compiler-core" "3.0.0-rc.5"
10241024
"@vue/shared" "3.0.0-rc.5"
10251025

1026+
1027+
version "3.0.0-rc.5"
1028+
resolved "https://registry.yarnpkg.com/@vue/compiler-ssr/-/compiler-ssr-3.0.0-rc.5.tgz#878406c59daff362ecdcb199fb9467a769ca8de5"
1029+
integrity sha512-OU5Vl2+bCDMImS9OeCVnl4lfxZ3/sopdwX2SrUWVKQvCxmmmlyWvoVkC6nNGCs/MrOmIMhKmL6etgzLTWyCkUg==
1030+
dependencies:
1031+
"@vue/compiler-dom" "3.0.0-rc.5"
1032+
"@vue/shared" "3.0.0-rc.5"
1033+
10261034
10271035
version "3.0.0-rc.5"
10281036
resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.0.0-rc.5.tgz#45cff8d839d7ad130b1e499239090050fdecff13"
@@ -1047,6 +1055,14 @@
10471055
"@vue/shared" "3.0.0-rc.5"
10481056
csstype "^2.6.8"
10491057

1058+
"@vue/server-renderer@^3.0.0-rc.5":
1059+
version "3.0.0-rc.5"
1060+
resolved "https://registry.yarnpkg.com/@vue/server-renderer/-/server-renderer-3.0.0-rc.5.tgz#524d3827c1352f7b6dbe178bd36902a26a0316cb"
1061+
integrity sha512-lWxs6uSvpw3UsqVxatdtEQBUTEdPTsM5JTqM1haAhaIWI2dhWEhmF3Z3iXnO12e8KUUp7Q4SjriEarzEa6YXtg==
1062+
dependencies:
1063+
"@vue/compiler-ssr" "3.0.0-rc.5"
1064+
"@vue/shared" "3.0.0-rc.5"
1065+
10501066
10511067
version "3.0.0-rc.5"
10521068
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.0.0-rc.5.tgz#cea2378e3e37363ddc1f5dd158edc9c9b5b3fff0"

0 commit comments

Comments
 (0)