|
| 1 | +import {Decorator} from './typescript/decorator'; |
| 2 | +import {TypeScriptFile} from './typescript/file'; |
| 3 | +import {Refactory} from '../refactory'; |
| 4 | +import {VirtualRefactoryHost} from '../utils/virtual_refactory_host'; |
| 5 | +import {NullRefactoryHost} from '../host'; |
| 6 | + |
| 7 | +const tsFileSystem: {[path: string]: string} = { |
| 8 | + '/tsconfig.json': '{}', |
| 9 | + '/file.ts': `console.log('hello');`, |
| 10 | + '/file1.ts': ` |
| 11 | + /** |
| 12 | + * @annotation |
| 13 | + */ |
| 14 | + function MyAnnotation(obj: any) { return () => {}; } |
| 15 | + function NotAnnotation() { return () => {}; } |
| 16 | + function NotFunctionCall() {} |
| 17 | + |
| 18 | + @NotFunctionCall |
| 19 | + @NotAnnotation() |
| 20 | + @MyAnnotation({ hello: 'world' }) |
| 21 | + class MyClass {} |
| 22 | + |
| 23 | + export class MyOther {} |
| 24 | + `, |
| 25 | + '/import1.ts': ` |
| 26 | + import {Symbol11} from './import1_def'; |
| 27 | + import * as Def from './import1_def'; |
| 28 | + import DefaultImport from './import1_def'; |
| 29 | + |
| 30 | + import 'import2_def'; |
| 31 | + `, |
| 32 | + '/import1_def.ts': ` |
| 33 | + export function Symbol11() {} |
| 34 | + export default function Symbol12() {} |
| 35 | + `, |
| 36 | + '/import2_def.ts': ` |
| 37 | + export function Symbol21() {} |
| 38 | + export default function Symbol22() {} |
| 39 | + ` |
| 40 | +}; |
| 41 | + |
| 42 | + |
| 43 | +describe('TypeScriptFile', () => { |
| 44 | + let refactory: Refactory; |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + const host = new VirtualRefactoryHost(new NullRefactoryHost('/')); |
| 48 | + for (const p of Object.keys(tsFileSystem)) { |
| 49 | + host.write(p, tsFileSystem[p]); |
| 50 | + } |
| 51 | + |
| 52 | + refactory = Refactory.fromTsConfig('/tsconfig.json', host); |
| 53 | + }); |
| 54 | + |
| 55 | + it('works with a file', () => { |
| 56 | + let file: TypeScriptFile = refactory.getFile('/file.ts') as TypeScriptFile; |
| 57 | + expect(file).not.toBeNull(); |
| 58 | + expect(file.transpile({}).outputText).toMatch(/console.log\('hello'\)/); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('classes', () => { |
| 62 | + it('can see class names', () => { |
| 63 | + let file1: TypeScriptFile = refactory.getFile('/file1.ts') as TypeScriptFile; |
| 64 | + expect(file1.classes.map(c => c.name)).toEqual(['MyClass', 'MyOther']); |
| 65 | + }); |
| 66 | + |
| 67 | + it('can see decorators', () => { |
| 68 | + let file1: TypeScriptFile = refactory.getFile('/file1.ts') as TypeScriptFile; |
| 69 | + // Should ignore NotAnnotation. |
| 70 | + expect(file1.classes[0].decorators.map((a: Decorator) => a.name)) |
| 71 | + .toEqual(['NotFunctionCall', 'NotAnnotation', 'MyAnnotation']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('can remove()', () => { |
| 75 | + let file1: TypeScriptFile = refactory.getFile('/file1.ts') as TypeScriptFile; |
| 76 | + file1.classes[0].remove(); |
| 77 | + |
| 78 | + const output = file1.transpile({}).outputText; |
| 79 | + expect(output).toContain('MyOther'); |
| 80 | + expect(output).not.toContain('MyClass'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('knows if its exported', () => { |
| 84 | + let file1: TypeScriptFile = refactory.getFile('/file1.ts') as TypeScriptFile; |
| 85 | + expect(file1.classes[0].isExported).toBe(false); |
| 86 | + expect(file1.classes[1].isExported).toBe(true); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('imports', () => { |
| 91 | + it('understands imports', () => { |
| 92 | + let import1: TypeScriptFile = refactory.getFile('/import1.ts') as TypeScriptFile; |
| 93 | + expect(import1.imports.map(i => i.name)).toEqual(['Symbol11']); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments