016173a9c5
- 将子组件移至 components/ 子目录(JsonDiffInput、DiffResult、JsonFormatSection、JsonConvertSection、DiffNavigator、JsonTree) - 将 diffEngine.ts 工具函数移至 src/utils/ - 提取 ConvertFunction/ConvertResult 类型到 types.ts,解决循环依赖 - 创建 __tests__/ 目录结构 - 更新所有相关导入路径 Co-authored-by: Cursor <cursoragent@cursor.com>
72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { diffJson } from '../diffEngine';
|
|
|
|
describe('diffJson', () => {
|
|
it('should detect added property', () => {
|
|
const left = { a: 1 };
|
|
const right = { a: 1, b: 2 };
|
|
const result = diffJson(left, right);
|
|
expect(result.diffCount).toBe(1);
|
|
expect(result.diffPaths).toContain('$.b');
|
|
const bNode = result.root.children?.find((n) => n.key === 'b');
|
|
expect(bNode?.type).toBe('added');
|
|
});
|
|
|
|
it('should detect removed property', () => {
|
|
const left = { a: 1, b: 2 };
|
|
const right = { a: 1 };
|
|
const result = diffJson(left, right);
|
|
expect(result.diffCount).toBe(1);
|
|
expect(result.diffPaths).toContain('$.b');
|
|
});
|
|
|
|
it('should detect modified value', () => {
|
|
const left = { a: 1 };
|
|
const right = { a: 2 };
|
|
const result = diffJson(left, right);
|
|
expect(result.diffCount).toBe(1);
|
|
expect(result.diffPaths).toContain('$.a');
|
|
const aNode = result.root.children?.find((n) => n.key === 'a');
|
|
expect(aNode?.type).toBe('modified');
|
|
expect(aNode?.oldValue).toBe(1);
|
|
expect(aNode?.newValue).toBe(2);
|
|
});
|
|
|
|
it('should handle nested objects', () => {
|
|
const left = { a: { b: 1 } };
|
|
const right = { a: { b: 2 } };
|
|
const result = diffJson(left, right);
|
|
expect(result.diffCount).toBe(1);
|
|
expect(result.diffPaths).toContain('$.a.b');
|
|
});
|
|
|
|
it('should handle arrays', () => {
|
|
const left = [1, 2];
|
|
const right = [1, 3];
|
|
const result = diffJson(left, right);
|
|
expect(result.diffCount).toBe(1);
|
|
expect(result.diffPaths).toContain('$[1]');
|
|
});
|
|
|
|
it('should return unchanged for identical objects', () => {
|
|
const obj = { a: 1, b: { c: 2 } };
|
|
const result = diffJson(obj, obj);
|
|
expect(result.diffCount).toBe(0);
|
|
expect(result.root.type).toBe('unchanged');
|
|
});
|
|
|
|
it('should handle type mismatch', () => {
|
|
const left = { a: 1 };
|
|
const right = { a: '1' };
|
|
const result = diffJson(left, right);
|
|
expect(result.diffCount).toBe(1);
|
|
const aNode = result.root.children?.find((n) => n.key === 'a');
|
|
expect(aNode?.type).toBe('modified');
|
|
});
|
|
|
|
it('should handle empty objects', () => {
|
|
const result = diffJson({}, {});
|
|
expect(result.diffCount).toBe(0);
|
|
});
|
|
});
|