i更新
This commit is contained in:
@@ -1,52 +0,0 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { generatorContentHash } from '../hash';
|
||||
|
||||
describe('generatorContentHash', () => {
|
||||
it('should generate an MD5 hash for the content', () => {
|
||||
const content = 'example content';
|
||||
const expectedHash = createHash('md5')
|
||||
.update(content, 'utf8')
|
||||
.digest('hex');
|
||||
const actualHash = generatorContentHash(content);
|
||||
expect(actualHash).toBe(expectedHash);
|
||||
});
|
||||
|
||||
it('should generate an MD5 hash with specified length', () => {
|
||||
const content = 'example content';
|
||||
const hashLength = 10;
|
||||
const generatedHash = generatorContentHash(content, hashLength);
|
||||
expect(generatedHash).toHaveLength(hashLength);
|
||||
});
|
||||
|
||||
it('should correctly generate the hash with specified length', () => {
|
||||
const content = 'example content';
|
||||
const hashLength = 8;
|
||||
const expectedHash = createHash('md5')
|
||||
.update(content, 'utf8')
|
||||
.digest('hex')
|
||||
.slice(0, hashLength);
|
||||
const generatedHash = generatorContentHash(content, hashLength);
|
||||
expect(generatedHash).toBe(expectedHash);
|
||||
});
|
||||
|
||||
it('should return full hash if hash length parameter is not provided', () => {
|
||||
const content = 'example content';
|
||||
const expectedHash = createHash('md5')
|
||||
.update(content, 'utf8')
|
||||
.digest('hex');
|
||||
const actualHash = generatorContentHash(content);
|
||||
expect(actualHash).toBe(expectedHash);
|
||||
});
|
||||
|
||||
it('should handle empty content', () => {
|
||||
const content = '';
|
||||
const expectedHash = createHash('md5')
|
||||
.update(content, 'utf8')
|
||||
.digest('hex');
|
||||
const actualHash = generatorContentHash(content);
|
||||
expect(actualHash).toBe(expectedHash);
|
||||
});
|
||||
});
|
||||
@@ -1,67 +0,0 @@
|
||||
// pathUtils.test.ts
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { toPosixPath } from '../path';
|
||||
|
||||
describe('toPosixPath', () => {
|
||||
// 测试 Windows 风格路径到 POSIX 风格路径的转换
|
||||
it('converts Windows-style paths to POSIX paths', () => {
|
||||
const windowsPath = String.raw`C:\Users\Example\file.txt`;
|
||||
const expectedPosixPath = 'C:/Users/Example/file.txt';
|
||||
expect(toPosixPath(windowsPath)).toBe(expectedPosixPath);
|
||||
});
|
||||
|
||||
// 确认 POSIX 风格路径不会被改变
|
||||
it('leaves POSIX-style paths unchanged', () => {
|
||||
const posixPath = '/home/user/file.txt';
|
||||
expect(toPosixPath(posixPath)).toBe(posixPath);
|
||||
});
|
||||
|
||||
// 测试带有多个分隔符的路径
|
||||
it('converts paths with mixed separators', () => {
|
||||
const mixedPath = String.raw`C:/Users\Example\file.txt`;
|
||||
const expectedPosixPath = 'C:/Users/Example/file.txt';
|
||||
expect(toPosixPath(mixedPath)).toBe(expectedPosixPath);
|
||||
});
|
||||
|
||||
// 测试空字符串
|
||||
it('handles empty strings', () => {
|
||||
const emptyPath = '';
|
||||
expect(toPosixPath(emptyPath)).toBe('');
|
||||
});
|
||||
|
||||
// 测试仅包含分隔符的路径
|
||||
it('handles path with only separators', () => {
|
||||
const separatorsPath = '\\\\\\';
|
||||
const expectedPosixPath = '///';
|
||||
expect(toPosixPath(separatorsPath)).toBe(expectedPosixPath);
|
||||
});
|
||||
|
||||
// 测试不包含任何分隔符的路径
|
||||
it('handles path without separators', () => {
|
||||
const noSeparatorPath = 'file.txt';
|
||||
expect(toPosixPath(noSeparatorPath)).toBe('file.txt');
|
||||
});
|
||||
|
||||
// 测试以分隔符结尾的路径
|
||||
it('handles path ending with a separator', () => {
|
||||
const endingSeparatorPath = 'C:\\Users\\Example\\';
|
||||
const expectedPosixPath = 'C:/Users/Example/';
|
||||
expect(toPosixPath(endingSeparatorPath)).toBe(expectedPosixPath);
|
||||
});
|
||||
|
||||
// 测试以分隔符开头的路径
|
||||
it('handles path starting with a separator', () => {
|
||||
const startingSeparatorPath = String.raw`\Users\Example`;
|
||||
const expectedPosixPath = '/Users/Example';
|
||||
expect(toPosixPath(startingSeparatorPath)).toBe(expectedPosixPath);
|
||||
});
|
||||
|
||||
// 测试包含非法字符的路径
|
||||
it('handles path with invalid characters', () => {
|
||||
const invalidCharsPath = String.raw`C:\Us*?ers\Ex<ample>|file.txt`;
|
||||
const expectedPosixPath = 'C:/Us*?ers/Ex<ample>|file.txt';
|
||||
expect(toPosixPath(invalidCharsPath)).toBe(expectedPosixPath);
|
||||
});
|
||||
});
|
||||
@@ -1,159 +0,0 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { viteCssLayerPlugin } from './css-layer';
|
||||
|
||||
interface FakeAsset {
|
||||
fileName: string;
|
||||
source: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
function createBundle(assets: FakeAsset[]) {
|
||||
return Object.fromEntries(assets.map((asset) => [asset.fileName, asset]));
|
||||
}
|
||||
|
||||
const LAYER_ORDER_STATEMENT =
|
||||
'@layer properties, theme, base, ant, antd, el, td, components, utilities;';
|
||||
|
||||
describe('viteCssLayerPlugin', () => {
|
||||
it('wraps matching css modules in the target layer', () => {
|
||||
const plugin = viteCssLayerPlugin({
|
||||
layerName: 'el',
|
||||
packageName: 'element-plus',
|
||||
});
|
||||
const transform = plugin.transform;
|
||||
|
||||
if (typeof transform !== 'function') return;
|
||||
|
||||
const result = transform.call(
|
||||
undefined as never,
|
||||
'.el-button { color: red; }',
|
||||
'/node_modules/element-plus/es/components/button/style/css/index.css?used',
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
code: '@layer el {\n.el-button { color: red; }\n}',
|
||||
map: null,
|
||||
});
|
||||
});
|
||||
|
||||
it('does not wrap css modules outside the target package', () => {
|
||||
const plugin = viteCssLayerPlugin({
|
||||
layerName: 'el',
|
||||
packageName: 'element-plus',
|
||||
});
|
||||
const transform = plugin.transform;
|
||||
|
||||
if (typeof transform !== 'function') return;
|
||||
|
||||
const result = transform.call(
|
||||
undefined as never,
|
||||
'.local { color: red; }',
|
||||
'/src/views/home/index.css',
|
||||
);
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('injects the layer order statement into matching css assets', () => {
|
||||
const plugin = viteCssLayerPlugin({
|
||||
layerName: 'el',
|
||||
packageName: 'element-plus',
|
||||
});
|
||||
const generateBundle = plugin.generateBundle;
|
||||
|
||||
if (typeof generateBundle !== 'function') return;
|
||||
|
||||
const bundle = createBundle([
|
||||
{
|
||||
fileName: 'element-x.css',
|
||||
source: '@layer el{.el-button{color:red}}',
|
||||
type: 'asset',
|
||||
},
|
||||
{
|
||||
fileName: 'theme-x.css',
|
||||
source: '.local { color: blue; }',
|
||||
type: 'asset',
|
||||
},
|
||||
]);
|
||||
|
||||
generateBundle.call(undefined as never, {} as never, bundle);
|
||||
|
||||
expect(bundle['element-x.css'].source).toBe(
|
||||
`${LAYER_ORDER_STATEMENT}\n@layer el{.el-button{color:red}}`,
|
||||
);
|
||||
expect(bundle['theme-x.css'].source).toBe('.local { color: blue; }');
|
||||
});
|
||||
|
||||
it('does not inject the statement twice when already present', () => {
|
||||
const plugin = viteCssLayerPlugin({
|
||||
layerName: 'el',
|
||||
packageName: 'element-plus',
|
||||
});
|
||||
const generateBundle = plugin.generateBundle;
|
||||
|
||||
if (typeof generateBundle !== 'function') return;
|
||||
|
||||
const bundle = createBundle([
|
||||
{
|
||||
fileName: 'element-x.css',
|
||||
source: `${LAYER_ORDER_STATEMENT}\n@layer el{.el-button{color:red}}`,
|
||||
type: 'asset',
|
||||
},
|
||||
]);
|
||||
|
||||
generateBundle.call(undefined as never, {} as never, bundle);
|
||||
|
||||
expect(bundle['element-x.css'].source).toBe(
|
||||
`${LAYER_ORDER_STATEMENT}\n@layer el{.el-button{color:red}}`,
|
||||
);
|
||||
});
|
||||
|
||||
it('injects the statement even when the asset starts with a BOM', () => {
|
||||
const plugin = viteCssLayerPlugin({
|
||||
layerName: 'el',
|
||||
packageName: 'element-plus',
|
||||
});
|
||||
const generateBundle = plugin.generateBundle;
|
||||
|
||||
if (typeof generateBundle !== 'function') return;
|
||||
|
||||
const bundle = createBundle([
|
||||
{
|
||||
fileName: 'element-x.css',
|
||||
source: `\uFEFF@layer el{.el-button{color:red}}`,
|
||||
type: 'asset',
|
||||
},
|
||||
]);
|
||||
|
||||
generateBundle.call(undefined as never, {} as never, bundle);
|
||||
|
||||
expect(bundle['element-x.css'].source).toBe(
|
||||
`${LAYER_ORDER_STATEMENT}\n\uFEFF@layer el{.el-button{color:red}}`,
|
||||
);
|
||||
});
|
||||
|
||||
it('ignores non-css assets', () => {
|
||||
const plugin = viteCssLayerPlugin({
|
||||
layerName: 'el',
|
||||
packageName: 'element-plus',
|
||||
});
|
||||
const generateBundle = plugin.generateBundle;
|
||||
|
||||
if (typeof generateBundle !== 'function') return;
|
||||
|
||||
const bundle = createBundle([
|
||||
{
|
||||
fileName: 'element-x.js',
|
||||
source: '@layer el{.el-button{color:red}}',
|
||||
type: 'chunk',
|
||||
},
|
||||
]);
|
||||
|
||||
generateBundle.call(undefined as never, {} as never, bundle);
|
||||
|
||||
expect(bundle['element-x.js'].source).toBe(
|
||||
'@layer el{.el-button{color:red}}',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,43 +0,0 @@
|
||||
import type { ResolvedConfig } from 'vite';
|
||||
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import {
|
||||
FORM_FIELD_SLOT_MIGRATION_WARNING,
|
||||
viteFormFieldSlotMigrationWarningPlugin,
|
||||
} from './form-field-slot-migration-warning';
|
||||
|
||||
describe('form field slot migration warning plugin', () => {
|
||||
it('warns in serve mode and injects the same browser message', () => {
|
||||
const plugin = viteFormFieldSlotMigrationWarningPlugin();
|
||||
const warning = vi.fn();
|
||||
|
||||
expect(plugin.apply).toBe('serve');
|
||||
expect(plugin.configResolved).toBeTypeOf('function');
|
||||
if (typeof plugin.configResolved !== 'function') return;
|
||||
|
||||
plugin.configResolved({ logger: { warn: warning } } as ResolvedConfig);
|
||||
|
||||
expect(warning).toHaveBeenCalledOnce();
|
||||
expect(warning).toHaveBeenCalledWith(FORM_FIELD_SLOT_MIGRATION_WARNING);
|
||||
expect(plugin.transformIndexHtml).toBeTypeOf('function');
|
||||
if (typeof plugin.transformIndexHtml !== 'function') return;
|
||||
|
||||
const result = plugin.transformIndexHtml('', {} as never);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
attrs: {
|
||||
'data-vben-form-field-slot-migration-warning': '',
|
||||
},
|
||||
children: `console.warn(${JSON.stringify(FORM_FIELD_SLOT_MIGRATION_WARNING)});`,
|
||||
injectTo: 'body',
|
||||
tag: 'script',
|
||||
},
|
||||
]);
|
||||
expect(FORM_FIELD_SLOT_MIGRATION_WARNING).toContain('`v-bind="slotProps"`');
|
||||
expect(FORM_FIELD_SLOT_MIGRATION_WARNING).toContain(
|
||||
'`v-bind="slotProps.componentProps"`',
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user