-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathextract-code-samples.test.js
More file actions
113 lines (86 loc) · 3.56 KB
/
extract-code-samples.test.js
File metadata and controls
113 lines (86 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { describe, it, expect } from "vitest";
import parser from "@babel/parser";
import { generateCodeForOneLiner, run } from "../extract-code-samples.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const outputDir = path.join(__dirname, "../../dist/generated/");
function parseCode(code) {
return parser.parse(code, {
sourceType: "module",
plugins: ["typescript", "jsx"]
});
}
describe("generateCodeForOneLiner", () => {
it("should return generated code for a regular variable declaration", () => {
const code = `const myComponent = () => <div>Hello</div>;`;
const ast = parseCode(code);
const result = generateCodeForOneLiner(ast, "myComponent");
expect(result).not.toBeNull();
expect(result).toContain("const");
expect(result).toContain("myComponent");
});
it("should return empty string for a createComponentTemplate call", () => {
const code = `const myTemplate = createComponentTemplate(SomeComponent);`;
const ast = parseCode(code);
const result = generateCodeForOneLiner(ast, "myTemplate");
expect(result).toBe("");
});
it("should return null when the variable name is not found", () => {
const code = `const other = 42;`;
const ast = parseCode(code);
const result = generateCodeForOneLiner(ast, "nonExistent");
expect(result).toBeNull();
});
it("should handle multiple variable declarations and find the correct one", () => {
const code = `
const first = createComponentTemplate(A);
const second = () => <span>Test</span>;
const third = 42;
`;
const ast = parseCode(code);
expect(generateCodeForOneLiner(ast, "first")).toBe("");
expect(generateCodeForOneLiner(ast, "second")).toContain("second");
expect(generateCodeForOneLiner(ast, "third")).toContain("third");
});
it("should prepend 'const ' to the generated code", () => {
const code = `const myVar = "hello";`;
const ast = parseCode(code);
const result = generateCodeForOneLiner(ast, "myVar");
expect(result).toMatch(/^const /);
});
});
describe("generateCodeForOneLiner - JSX handling", () => {
it("should generate code for a variable with JSX value", () => {
const code = `const myElement = <div className="wrapper"><span>Hello</span></div>;`;
const ast = parseCode(code);
const result = generateCodeForOneLiner(ast, "myElement");
expect(result).toContain("myElement");
expect(result).toContain("div");
expect(result).toContain("wrapper");
});
it("should generate code for an arrow function returning JSX", () => {
const code = `const MyComponent = () => <Button onClick={handler}>Click</Button>;`;
const ast = parseCode(code);
const result = generateCodeForOneLiner(ast, "MyComponent");
expect(result).not.toBeNull();
expect(result).toContain("MyComponent");
expect(result).toContain("Button");
});
it("should handle a variable with a nested function call (not createComponentTemplate)", () => {
const code = `const myVar = someOtherFunction(Arg1, Arg2);`;
const ast = parseCode(code);
const result = generateCodeForOneLiner(ast, "myVar");
expect(result).not.toBeNull();
expect(result).toContain("const");
expect(result).toContain("someOtherFunction");
});
});
describe("run - integration", () => {
it("should generate markdown files from story files", () => {
run();
const outputFiles = fs.readdirSync(outputDir).filter(f => f.endsWith(".md"));
expect(outputFiles.length).toBeGreaterThan(0);
});
});