|
1 | 1 | import * as t from "@babel/types"; |
2 | 2 | import { bundlePackages, getPackagesBundled } from "../src/bundle"; |
3 | 3 | import generate from "@babel/generator"; |
4 | | -import { recursiveModuleBundle } from "@cosmology/ast"; |
| 4 | +import { exportAllFromRelPath, exportTypesWithAlias, recursiveModuleBundle } from "@cosmology/ast"; |
| 5 | +import { restoreExtension } from "@cosmology/utils"; |
5 | 6 | import { getTestProtoStore } from "../test-utils"; |
6 | 7 | import { getImportStatements } from "../src/imports"; |
7 | 8 |
|
@@ -124,3 +125,47 @@ it("get import statements with ext without .", () => { |
124 | 125 |
|
125 | 126 | expect(importStat).toMatchSnapshot(); |
126 | 127 | }); |
| 128 | + |
| 129 | +describe("module bundle type with restoreImportExtension", () => { |
| 130 | + it("exportAllFromRelPath applies restoreExtension with .js", () => { |
| 131 | + const relPath = "./auth/v1beta1/auth"; |
| 132 | + const restored = restoreExtension(relPath, ".js"); |
| 133 | + const node = exportAllFromRelPath(restored); |
| 134 | + const code = generate(t.program([node])).code; |
| 135 | + expect(code).toBe('export * from "./auth/v1beta1/auth.js";'); |
| 136 | + }); |
| 137 | + |
| 138 | + it("exportAllFromRelPath applies restoreExtension with .mjs", () => { |
| 139 | + const relPath = "./bank/v1beta1/tx"; |
| 140 | + const restored = restoreExtension(relPath, ".mjs"); |
| 141 | + const node = exportAllFromRelPath(restored); |
| 142 | + const code = generate(t.program([node])).code; |
| 143 | + expect(code).toBe('export * from "./bank/v1beta1/tx.mjs";'); |
| 144 | + }); |
| 145 | + |
| 146 | + it("exportTypesWithAlias applies restoreExtension", () => { |
| 147 | + const relPath = "./auth/v1beta1/auth"; |
| 148 | + const restored = restoreExtension(relPath, ".js"); |
| 149 | + const types = [ |
| 150 | + { name: "BaseAccount", alias: "BaseAccount" }, |
| 151 | + { name: "ModuleAccount", alias: "ModuleAccount" }, |
| 152 | + ]; |
| 153 | + const node = exportTypesWithAlias(types, restored); |
| 154 | + const code = generate(t.program([node])).code; |
| 155 | + expect(code).toBe( |
| 156 | + 'export { BaseAccount, ModuleAccount } from "./auth/v1beta1/auth.js";' |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + it("restoreExtension does not double-add extension", () => { |
| 161 | + const relPath = "./auth/v1beta1/auth.js"; |
| 162 | + const restored = restoreExtension(relPath, ".js"); |
| 163 | + expect(restored).toBe("./auth/v1beta1/auth.js"); |
| 164 | + }); |
| 165 | + |
| 166 | + it("restoreExtension without ext returns original path", () => { |
| 167 | + const relPath = "./auth/v1beta1/auth"; |
| 168 | + const restored = restoreExtension(relPath, undefined); |
| 169 | + expect(restored).toBe("./auth/v1beta1/auth"); |
| 170 | + }); |
| 171 | +}); |
0 commit comments