Skip to content

Commit f3189bd

Browse files
committed
Add tests for SSR rendering of v-t directive
1 parent 4b7fab3 commit f3189bd

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

__tests__/utils/ssr.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as TestUtils from '@vue/test-utils'
2+
import type { ComponentOptions } from 'vue-3'
3+
import { install } from 'vue-demi'
4+
5+
import type { FluentVue } from '../../src'
6+
7+
install()
8+
9+
export function renderSSR<T extends object>(
10+
fluent: FluentVue | null,
11+
component: ComponentOptions<T>,
12+
): Promise<string> {
13+
const { renderToString } = TestUtils
14+
15+
const plugins = fluent ? [fluent] : []
16+
17+
return renderToString(component, {
18+
global: {
19+
plugins,
20+
},
21+
})
22+
}

__tests__/vue/directive.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ describe('directive', () => {
141141
`),
142142
)
143143

144+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
145+
144146
const component = {
145147
data: () => ({
146148
name: 'John',
@@ -153,6 +155,10 @@ describe('directive', () => {
153155

154156
// Assert
155157
expect(mounted.html()).toEqual('<a aria-label="Hello \u{2068}John\u{2069}">Text</a>')
158+
expect(warn).toHaveBeenCalledTimes(1)
159+
expect(warn).toHaveBeenCalledWith(
160+
'[fluent-vue] Attribute \'not-allowed\' on element <a> is not localizable. Remove it from the translation. Translation key: link',
161+
)
156162
})
157163

158164
it('works without fallbacks', () => {

__tests__/vue/ssr.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { beforeEach, describe, expect, it } from 'vitest'
2+
import { isVue3 } from 'vue-demi'
3+
4+
import { FluentBundle, FluentResource } from '@fluent/bundle'
5+
import ftl from '@fluent/dedent'
6+
7+
import { renderSSR } from '../utils/ssr'
8+
9+
import type { FluentVue } from '../../src'
10+
import { createFluentVue } from '../../src'
11+
12+
describe.skipIf(!isVue3)('sSR directive', () => {
13+
let fluent: FluentVue
14+
let bundle: FluentBundle
15+
16+
beforeEach(() => {
17+
bundle = new FluentBundle('en-US')
18+
19+
fluent = createFluentVue({
20+
bundles: [bundle],
21+
})
22+
})
23+
24+
it('translates text content', async () => {
25+
// Arrange
26+
bundle.addResource(
27+
new FluentResource(ftl`
28+
link = Link text
29+
.aria-label = Link aria label
30+
`),
31+
)
32+
33+
const component = {
34+
data: () => ({
35+
name: 'John',
36+
}),
37+
template: '<a v-t:link href="/foo">Fallback text</a>',
38+
}
39+
40+
// Act
41+
const rendered = await renderSSR(fluent, component)
42+
43+
// Assert
44+
// This has fallback text because the textContent is not supported by Vue getSSRProps
45+
// Text will be translated using directive transform
46+
expect(rendered).toEqual('<a href="/foo" aria-label="Link aria label">Fallback text</a>')
47+
})
48+
})

src/vue/directive.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export function createVue3Directive(rootContext: TranslationContext): Vue3Direct
6363
attrs[attr] = attrValue
6464
}
6565

66+
// TODO: Include textContent when https://github.com/vuejs/core/issues/8112 is resolved
6667
return attrs
6768
},
6869
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy