|
| 1 | +import { code, namekey, Output } from "@alloy-js/core"; |
| 2 | +import "@alloy-js/core/testing"; |
| 3 | +import { d } from "@alloy-js/core/testing"; |
| 4 | +import { expect, it } from "vitest"; |
| 5 | +import * as ts from "../src/index.js"; |
| 6 | +import { decl, declMember, declType } from "../src/symbols/index.js"; |
| 7 | + |
| 8 | +it("declares variables", () => { |
| 9 | + const hi = namekey("hi"); |
| 10 | + |
| 11 | + expect( |
| 12 | + <Output> |
| 13 | + <ts.SourceFile path="test1.ts"> |
| 14 | + {code` |
| 15 | + const ${decl(hi)} = 12; |
| 16 | + console.log(${hi}); |
| 17 | + `} |
| 18 | + </ts.SourceFile> |
| 19 | + <ts.SourceFile path="test2.ts">{hi};</ts.SourceFile> |
| 20 | + </Output>, |
| 21 | + ).toRenderTo({ |
| 22 | + "test1.ts": d` |
| 23 | + const hi = 12; |
| 24 | + console.log(hi); |
| 25 | + `, |
| 26 | + "test2.ts": d` |
| 27 | + import { hi } from "./test1.js"; |
| 28 | +
|
| 29 | + hi; |
| 30 | + `, |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +it("declares types", () => { |
| 35 | + const hi = namekey("hi"); |
| 36 | + |
| 37 | + expect( |
| 38 | + <Output> |
| 39 | + <ts.SourceFile path="test1.ts"> |
| 40 | + {code` |
| 41 | + interface ${declType(hi)} { |
| 42 | + value: number; |
| 43 | + } |
| 44 | + `} |
| 45 | + </ts.SourceFile> |
| 46 | + <ts.SourceFile path="test2.ts"> |
| 47 | + <ts.VarDeclaration name="test" type={hi} /> |
| 48 | + </ts.SourceFile> |
| 49 | + </Output>, |
| 50 | + ).toRenderTo({ |
| 51 | + "test1.ts": d` |
| 52 | + interface hi { |
| 53 | + value: number; |
| 54 | + } |
| 55 | + `, |
| 56 | + "test2.ts": d` |
| 57 | + import type { hi } from "./test1.js"; |
| 58 | +
|
| 59 | + const test: hi |
| 60 | + `, |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +it("declares variables", () => { |
| 65 | + const hi = namekey("hi"); |
| 66 | + |
| 67 | + expect( |
| 68 | + <Output> |
| 69 | + <ts.SourceFile path="test1.ts"> |
| 70 | + {code` |
| 71 | + interface ${declType(hi)} { |
| 72 | + value: number; |
| 73 | + } |
| 74 | + `} |
| 75 | + </ts.SourceFile> |
| 76 | + <ts.SourceFile path="test2.ts"> |
| 77 | + <ts.VarDeclaration name="test" type={hi} /> |
| 78 | + </ts.SourceFile> |
| 79 | + </Output>, |
| 80 | + ).toRenderTo({ |
| 81 | + "test1.ts": d` |
| 82 | + interface hi { |
| 83 | + value: number; |
| 84 | + } |
| 85 | + `, |
| 86 | + "test2.ts": d` |
| 87 | + import type { hi } from "./test1.js"; |
| 88 | +
|
| 89 | + const test: hi |
| 90 | + `, |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +it("declares members", () => { |
| 95 | + const publicMember = namekey("publicMember"); |
| 96 | + const privateMember = namekey("privateMember"); |
| 97 | + const staticMethod = namekey("staticMethod"); |
| 98 | + expect( |
| 99 | + <Output> |
| 100 | + <ts.SourceFile path="test1.ts"> |
| 101 | + <ts.ClassDeclaration name="Foo"> |
| 102 | + {code` |
| 103 | + public ${declMember(publicMember)} = 12; |
| 104 | + #${declMember(privateMember, { jsPrivate: true })} = 34; |
| 105 | + constructor() { |
| 106 | + ${publicMember} = 24; |
| 107 | + ${privateMember} = 10; |
| 108 | + } |
| 109 | + `} |
| 110 | + <hbr /> |
| 111 | + static ${declMember(staticMethod, { static: true })}() {"{}"}; |
| 112 | + </ts.ClassDeclaration> |
| 113 | + <hbr /> |
| 114 | + {staticMethod}(); |
| 115 | + </ts.SourceFile> |
| 116 | + </Output>, |
| 117 | + ).toRenderTo(d` |
| 118 | + class Foo { |
| 119 | + public publicMember = 12; |
| 120 | + #privateMember = 34; |
| 121 | + constructor() { |
| 122 | + this.publicMember = 24; |
| 123 | + this.#privateMember = 10; |
| 124 | + } |
| 125 | + static $staticMethod() {}; |
| 126 | + } |
| 127 | + Foo.staticMethod(); |
| 128 | + `); |
| 129 | +}); |
0 commit comments