UNPKG

storybook-addon-jsdoc-to-mdx

Version:

Storybook addon that automatically generates MDX documentation from JSDoc comments in your TypeScript and JavaScript files. Supports HTML tags in comments, complex TypeScript types, and integrates seamlessly with Storybook 7.x and 8.x.

132 lines (131 loc) 5.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const astAnalysis_1 = require("./astAnalysis"); const ts_morph_1 = require("ts-morph"); describe("getFunctionName", () => { it("returns the name for a method declaration", () => { const mockNode = { getName: () => "methodName", getKind: () => ts_morph_1.SyntaxKind.MethodDeclaration, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("methodName"); }); it("returns the function name for a function declaration", () => { const mockNode = { getName: () => "functionName", getKind: () => ts_morph_1.SyntaxKind.FunctionDeclaration, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("functionName"); }); it('returns "default" for default exported arrow function', () => { const mockNode = { getExpression: () => ({ getName: () => "", getKind: () => ts_morph_1.SyntaxKind.ArrowFunction, }), getKind: () => ts_morph_1.SyntaxKind.ExportAssignment, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("default"); }); it('returns "default" for anonymous arrow function', () => { const mockNode = { getName: () => "", getKind: () => ts_morph_1.SyntaxKind.ArrowFunction, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("default"); }); it("returns the name for a class declaration", () => { const mockNode = { getName: () => "MyClass", getKind: () => ts_morph_1.SyntaxKind.ClassDeclaration, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("MyClass"); }); it("returns the name for an interface declaration", () => { const mockNode = { getName: () => "MyInterface", getKind: () => ts_morph_1.SyntaxKind.InterfaceDeclaration, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("MyInterface"); }); it("returns the name for an enum declaration", () => { const mockNode = { getName: () => "MyEnum", getKind: () => ts_morph_1.SyntaxKind.EnumDeclaration, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("MyEnum"); }); it("returns the name for a module declaration", () => { const mockNode = { getName: () => "MyModule", getKind: () => ts_morph_1.SyntaxKind.ModuleDeclaration, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("MyModule"); }); it("returns the name for a type alias declaration", () => { const mockNode = { getName: () => "MyTypeAlias", getKind: () => ts_morph_1.SyntaxKind.TypeAliasDeclaration, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("MyTypeAlias"); }); it("returns the name for a variable statement", () => { const mockNode = { getDeclarations: () => [{ getName: () => "myVariable" }], getKind: () => ts_morph_1.SyntaxKind.VariableStatement, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("myVariable"); }); it('returns "FunctionType" for function type with no return type', () => { const mockNode = { getType: () => ({ getCallSignatures: () => [ { getReturnType: () => ({ getText: () => "" }), }, ], }), getKind: () => ts_morph_1.SyntaxKind.FunctionType, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("FunctionType"); }); it("returns the function name for a named function expression in default export", () => { const mockNode = { getExpression: () => ({ getName: () => "namedFunction", getKind: () => ts_morph_1.SyntaxKind.FunctionExpression, }), getKind: () => ts_morph_1.SyntaxKind.ExportAssignment, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("namedFunction"); }); it('returns "Unnamed" for a node with no name', () => { const mockNode = { getName: () => "", getKind: () => ts_morph_1.SyntaxKind.ClassDeclaration, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("Unnamed"); }); it('returns "Unnamed" for a variable statement with no declarations', () => { const mockNode = { getDeclarations: () => [], getKind: () => ts_morph_1.SyntaxKind.VariableStatement, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("Unnamed"); }); it('returns "Unnamed" for a function type with no call signatures', () => { const mockNode = { getType: () => ({ getCallSignatures: () => [], // No signatures }), getKind: () => ts_morph_1.SyntaxKind.FunctionType, }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("Unnamed"); }); it('returns "Unnamed" for an unhandled node kind', () => { const mockNode = { getKind: () => -1, // Unknown node type }; expect((0, astAnalysis_1.getFunctionName)(mockNode)).toBe("Unnamed"); }); });