Skip to content

Commit 433a905

Browse files
committed
feat: add vite plugin tests
1 parent 562a911 commit 433a905

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

__tests__/vite/plugin.spec.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
const { i18nextHMRPlugin } = require('../../lib/vite/plugin');
2+
3+
function initPlugin({ localesDir, localesDirs, root = '/root', platform = process.platform } = {}) {
4+
if (process.platform !== platform) {
5+
Object.defineProperty(process, 'platform', {
6+
value: platform,
7+
});
8+
}
9+
10+
const plugin = i18nextHMRPlugin({ localesDir, localesDirs });
11+
plugin.configResolved({
12+
root,
13+
});
14+
15+
return plugin;
16+
}
17+
18+
function triggerHotUpdateWith({ file }, plugin) {
19+
if (process.platform === 'win32') {
20+
//hack append process.cwd() to file path
21+
file = `${process.cwd()}\\${file}`;
22+
}
23+
const mockServer = {
24+
ws: {
25+
send: jest.fn(),
26+
},
27+
};
28+
29+
plugin.handleHotUpdate({ file, server: mockServer });
30+
31+
return mockServer;
32+
}
33+
34+
describe('Vite - plugin', () => {
35+
let plugin;
36+
let originalPlatform;
37+
beforeEach(() => {
38+
originalPlatform = process.platform;
39+
plugin = initPlugin();
40+
});
41+
42+
afterEach(() => {
43+
Object.defineProperty(process, 'platform', {
44+
value: originalPlatform,
45+
});
46+
});
47+
48+
it('should support localesDir as absolute path', () => {
49+
const plugin = initPlugin({ localesDir: '/absolute/path/to/locales' });
50+
const mockServer = triggerHotUpdateWith({ file: '/absolute/path/to/locales/en.json' }, plugin);
51+
52+
expect(mockServer.ws.send).toHaveBeenCalledWith('i18next-hmr:locale-changed', {
53+
changedFiles: ['en'],
54+
});
55+
});
56+
57+
it('should support localesDir as relative path', () => {
58+
const plugin = initPlugin({ localesDir: './public/locales' });
59+
const mockServer = triggerHotUpdateWith({ file: '/root/public/locales/en.json' }, plugin);
60+
61+
expect(mockServer.ws.send).toHaveBeenCalledWith('i18next-hmr:locale-changed', {
62+
changedFiles: ['en'],
63+
});
64+
});
65+
66+
it('should support localesDirs as absolute path', () => {
67+
const plugin = initPlugin({ localesDirs: ['/absolute/path/to/locales'] });
68+
const mockServer = triggerHotUpdateWith({ file: '/absolute/path/to/locales/en.json' }, plugin);
69+
70+
expect(mockServer.ws.send).toHaveBeenCalledWith('i18next-hmr:locale-changed', {
71+
changedFiles: ['en'],
72+
});
73+
});
74+
75+
it('should support localesDirs as relative path', () => {
76+
const plugin = initPlugin({ localesDirs: ['./public/locales'] });
77+
const mockServer = triggerHotUpdateWith({ file: '/root/public/locales/en.json' }, plugin);
78+
79+
expect(mockServer.ws.send).toHaveBeenCalledWith('i18next-hmr:locale-changed', {
80+
changedFiles: ['en'],
81+
});
82+
});
83+
84+
it('should process.cwd if root is not specified', () => {
85+
const plugin = initPlugin({ localesDirs: ['./public/locales'], root: '' });
86+
const mockServer = triggerHotUpdateWith(
87+
{ file: `${process.cwd()}/public/locales/en.json` },
88+
plugin
89+
);
90+
91+
expect(mockServer.ws.send).toHaveBeenCalledWith('i18next-hmr:locale-changed', {
92+
changedFiles: ['en'],
93+
});
94+
});
95+
96+
it('should support namespaces', () => {
97+
const plugin = initPlugin({ localesDirs: ['./public/locales'], root: '/root' });
98+
const mockServer = triggerHotUpdateWith(
99+
{ file: '/root/public/locales/namespace/en.json' },
100+
plugin
101+
);
102+
103+
expect(mockServer.ws.send).toHaveBeenCalledWith('i18next-hmr:locale-changed', {
104+
changedFiles: ['namespace/en'],
105+
});
106+
});
107+
});

0 commit comments

Comments
 (0)