This commit is contained in:
101
tests/file-watcher.test.ts
Normal file
101
tests/file-watcher.test.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import { getDeclarationPath, getInterfaceName, toPascalCase } from '../src/file-watcher.js';
|
||||
|
||||
describe('File Utilities', () => {
|
||||
describe('getDeclarationPath', () => {
|
||||
it('should convert .json to .d.ts in same directory', () => {
|
||||
const result = getDeclarationPath('/path/to/file.json');
|
||||
expect(result).toBe('/path/to/file.d.ts');
|
||||
});
|
||||
|
||||
it('should respect output directory', () => {
|
||||
const result = getDeclarationPath('/path/to/file.json', '/output');
|
||||
expect(result).toBe('/output/file.d.ts');
|
||||
});
|
||||
|
||||
it('should handle paths with multiple dots', () => {
|
||||
const result = getDeclarationPath('/path/to/my.data.json');
|
||||
expect(result).toBe('/path/to/my.data.d.ts');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getInterfaceName', () => {
|
||||
it('should convert filename to PascalCase', () => {
|
||||
const result = getInterfaceName('/path/to/user-data.json');
|
||||
expect(result).toBe('UserData');
|
||||
});
|
||||
|
||||
it('should handle kebab-case', () => {
|
||||
const result = getInterfaceName('/path/to/my-config.json');
|
||||
expect(result).toBe('MyConfig');
|
||||
});
|
||||
|
||||
it('should handle snake_case', () => {
|
||||
const result = getInterfaceName('/path/to/my_config.json');
|
||||
expect(result).toBe('MyConfig');
|
||||
});
|
||||
|
||||
it('should handle spaces', () => {
|
||||
const result = getInterfaceName('/path/to/my config.json');
|
||||
expect(result).toBe('MyConfig');
|
||||
});
|
||||
});
|
||||
|
||||
describe('toPascalCase', () => {
|
||||
it('should convert kebab-case to PascalCase', () => {
|
||||
expect(toPascalCase('hello-world')).toBe('HelloWorld');
|
||||
});
|
||||
|
||||
it('should convert snake_case to PascalCase', () => {
|
||||
expect(toPascalCase('hello_world')).toBe('HelloWorld');
|
||||
});
|
||||
|
||||
it('should convert spaces to PascalCase', () => {
|
||||
expect(toPascalCase('hello world')).toBe('HelloWorld');
|
||||
});
|
||||
|
||||
it('should handle single word', () => {
|
||||
expect(toPascalCase('hello')).toBe('Hello');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Integration: File Processing', () => {
|
||||
const tempDir = path.join(os.tmpdir(), 'type-from-json-test');
|
||||
|
||||
beforeAll(() => {
|
||||
fs.mkdirSync(tempDir, { recursive: true });
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('should create declaration file from JSON', async () => {
|
||||
const jsonPath = path.join(tempDir, 'test.json');
|
||||
const dtsPath = path.join(tempDir, 'test.d.ts');
|
||||
|
||||
fs.writeFileSync(jsonPath, JSON.stringify({ name: 'John', age: 30 }));
|
||||
|
||||
const { readJsonFile, writeDeclarationFile } = await import('../src/file-watcher.js');
|
||||
const { TypeInferrer } = await import('../src/type-inferrer.js');
|
||||
const { DeclarationGenerator } = await import('../src/declaration-generator.js');
|
||||
|
||||
const data = await readJsonFile(jsonPath);
|
||||
const inferrer = new TypeInferrer({ rootName: 'Test' });
|
||||
const result = inferrer.infer(data);
|
||||
|
||||
const generator = new DeclarationGenerator();
|
||||
const declaration = generator.generate(result.types);
|
||||
|
||||
await writeDeclarationFile(dtsPath, declaration);
|
||||
|
||||
expect(fs.existsSync(dtsPath)).toBe(true);
|
||||
const content = fs.readFileSync(dtsPath, 'utf-8');
|
||||
expect(content).toContain('export interface Test');
|
||||
expect(content).toContain('name: string;');
|
||||
expect(content).toContain('age: number;');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user