Skip to content
This repository was archived by the owner on Sep 17, 2022. It is now read-only.

Commit 1adf6d5

Browse files
committed
Add tests
1 parent 90e8881 commit 1adf6d5

File tree

4 files changed

+198
-2
lines changed

4 files changed

+198
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`vite plugin with external support > works with vue 2 1`] = `
4+
"import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/components/external.vue\\");import.meta.env = {\\"BASE_URL\\":\\"/\\",\\"MODE\\":\\"development\\",\\"DEV\\":true,\\"PROD\\":false,\\"SSR\\":false};import { FluentResource } from '/@id/@fluent/bundle';
5+
import en_ftl from '/fixtures/ftl/en/components/external.vue.ftl?import';
6+
import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
7+
const __vue2_script = {}
8+
import { render as __vue2_render, staticRenderFns as __vue2_staticRenderFns } from \\"/fixtures/components/external.vue?vue&type=template&lang.js\\"
9+
const __cssModules = {}
10+
11+
/* normalize component */
12+
import __vue2_normalizer from \\"/@id/__x00__/vite/vueComponentNormalizer\\"
13+
var __component__ = /*#__PURE__*/__vue2_normalizer(
14+
__vue2_script,
15+
__vue2_render,
16+
__vue2_staticRenderFns,
17+
false,
18+
__vue2_injectStyles,
19+
null,
20+
null,
21+
null
22+
)
23+
24+
function __vue2_injectStyles (context) {
25+
for(let o in __cssModules){
26+
this[o] = __cssModules[o]
27+
}
28+
}
29+
30+
__component__.options.fluent = { en: en_ftl, da: da_ftl };
31+
__component__.options.__file = \\"fixtures/components/external.vue\\"
32+
/* hot reload */
33+
import __VUE_HMR_RUNTIME__ from \\"/@id/__x00__/vite/vueHotReload\\"
34+
import vue from \\"/@id/vue\\"
35+
__VUE_HMR_RUNTIME__.install(vue)
36+
if(!import.meta.env.SSR && __VUE_HMR_RUNTIME__.compatible){
37+
if (!__VUE_HMR_RUNTIME__.isRecorded(\\"4c0553ad\\")) {
38+
__VUE_HMR_RUNTIME__.createRecord(\\"4c0553ad\\", __component__.options)
39+
}
40+
import.meta.hot.accept((update) => {
41+
__VUE_HMR_RUNTIME__.reload(\\"4c0553ad\\", update.default)
42+
})
43+
import.meta.hot.accept(\\"/fixtures/components/external.vue?vue&type=template&lang.js\\", (update) => {
44+
__VUE_HMR_RUNTIME__.rerender(\\"4c0553ad\\", update)
45+
})
46+
}
47+
export default /*#__PURE__*/(function () { return __component__.exports })()"
48+
`;
49+
50+
exports[`vite plugin with external support > works with vue 3 1`] = `
51+
"import { FluentResource } from '/@id/@fluent/bundle';
52+
import en_ftl from '/fixtures/ftl/en/components/external.vue.ftl?import';
53+
import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
54+
const script = {}
55+
import { render } from \\"/fixtures/components/external.vue?vue&type=template&id=8d0ca2ca&lang.js\\"
56+
57+
58+
script.render = render
59+
script.fluent = { en: en_ftl, da: da_ftl };
60+
script.__file = \\"__tests__/fixtures/components/external.vue\\"
61+
export default script"
62+
`;
63+
64+
exports[`vite plugin with external support > works with vue 3 script setup 1`] = `
65+
"import { FluentResource } from '/@id/@fluent/bundle';
66+
import en_ftl from '/fixtures/ftl/en/components/external.setup.vue.ftl?import';
67+
import da_ftl from '/fixtures/ftl/da/components/external.setup.vue.ftl?import';
68+
import script from \\"/fixtures/components/external.setup.vue?vue&type=script&setup=true&lang.js\\"
69+
export * from \\"/fixtures/components/external.setup.vue?vue&type=script&setup=true&lang.js\\"
70+
71+
72+
73+
74+
script.fluent = { en: en_ftl, da: da_ftl };
75+
script.__file = \\"__tests__/fixtures/components/external.setup.vue\\"
76+
export default script"
77+
`;

__tests__/__snapshots__/external.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ module.exports = script;
164164
exports[`external ftl file support > works with vue 3 script setup 1`] = `
165165
"'use strict';
166166
167-
require('@fluent/bundle');
167+
var bundle = require('@fluent/bundle');
168168
var vue = require('vue');
169169
170-
var en_ftl = null;
170+
var en_ftl = new bundle.FluentResource(\\"# Simple things are simple.\\\\nhello-user = Hello, {$userName}!\\\\n\\\\n# Complex things are possible.\\\\nshared-photos =\\\\n {$userName} {$photoCount ->\\\\n [one] added one photo\\\\n *[other] added {$photoCount} new photos\\\\n } to {$userGender ->\\\\n [male] his stream\\\\n [female] her stream\\\\n *[other] their stream\\\\n }.\\\\n\\");
171171
172172
var da_ftl = null;
173173

__tests__/external.hmr.spec.ts

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { dirname, resolve } from 'path'
2+
import { fileURLToPath } from 'url'
3+
import { describe, expect, it } from 'vitest'
4+
5+
import type { InlineConfig, Plugin } from 'vite'
6+
import { createServer } from 'vite'
7+
import vue3 from 'rollup-plugin-vue'
8+
import { createVuePlugin as vue2 } from 'vite-plugin-vue2'
9+
10+
import fluentPlugin from '../src'
11+
12+
const baseDir = dirname(fileURLToPath(import.meta.url))
13+
14+
const testBundle = async(options: InlineConfig, file: string): Promise<string | undefined> => {
15+
const vite = await createServer(options)
16+
17+
const result = await vite.transformRequest(file)
18+
return result?.code
19+
}
20+
21+
function externals(): Plugin {
22+
const externals = [
23+
'vue',
24+
'@fluent/bundle',
25+
]
26+
27+
return {
28+
name: 'externals',
29+
resolveId(id) {
30+
if (externals.includes(id))
31+
return id
32+
},
33+
load(id) {
34+
if (externals.includes(id))
35+
return 'export default function() {}'
36+
},
37+
}
38+
}
39+
40+
describe('vite plugin with external support', () => {
41+
it('works with vue 3', async() => {
42+
// Arrange
43+
// Act
44+
const code = await testBundle({
45+
root: baseDir,
46+
plugins: [
47+
externals(),
48+
vue3(),
49+
fluentPlugin({
50+
external: {
51+
baseDir: resolve(baseDir, 'fixtures'),
52+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
53+
locales: ['en', 'da'],
54+
},
55+
}),
56+
],
57+
}, '/fixtures/components/external.vue')
58+
59+
// Assert
60+
expect(code).toMatchSnapshot()
61+
})
62+
63+
it('works with vue 3 script setup', async() => {
64+
// Arrange
65+
// Act
66+
const code = await testBundle({
67+
root: baseDir,
68+
plugins: [
69+
externals(),
70+
vue3(),
71+
fluentPlugin({
72+
external: {
73+
baseDir: resolve(baseDir, 'fixtures'),
74+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
75+
locales: ['en', 'da'],
76+
},
77+
}),
78+
],
79+
}, '/fixtures/components/external.setup.vue')
80+
81+
// Assert
82+
expect(code).toMatchSnapshot()
83+
})
84+
85+
it('works with vue 2', async() => {
86+
// Arrange
87+
// Act
88+
const code = await testBundle({
89+
root: baseDir,
90+
plugins: [
91+
externals(),
92+
vue2(),
93+
fluentPlugin({
94+
external: {
95+
baseDir: resolve(baseDir, 'fixtures'),
96+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
97+
locales: ['en', 'da'],
98+
},
99+
}),
100+
],
101+
}, '/fixtures/components/external.vue')
102+
103+
// Assert
104+
expect(code).toMatchSnapshot()
105+
})
106+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Simple things are simple.
2+
hello-user = Hello, {$userName}!
3+
4+
# Complex things are possible.
5+
shared-photos =
6+
{$userName} {$photoCount ->
7+
[one] added one photo
8+
*[other] added {$photoCount} new photos
9+
} to {$userGender ->
10+
[male] his stream
11+
[female] her stream
12+
*[other] their stream
13+
}.

0 commit comments

Comments
 (0)