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.

173 lines (172 loc) 8.14 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const mdxContent_1 = require("./mdxContent"); const utils = __importStar(require("./utils")); // Mock the utils functions for isolated testing jest.mock("./utils", () => ({ formatJsDocComment: jest.fn((comment) => `Formatted: ${comment}`), removeCommentsFromCode: jest.fn((code) => `Cleaned: ${code}`), extractMethodFromCode: jest.fn((code, methodName) => `Extracted: ${methodName}`), })); describe("createMdxContent", () => { // Reset mock implementations before each test beforeEach(() => { jest.clearAllMocks(); }); it("should create MDX content from JsDoc comments", () => { // Mock data const jsDocComments = [ { name: "TestFunction", type: "Function", comment: "This is a test function", code: "function test() {} // This is a comment", }, ]; const pathName = "test/path"; const result = (0, mdxContent_1.createMdxContent)(jsDocComments, pathName); expect(result).toContain("TestFunction"); expect(result).toContain("Formatted: This is a test function"); expect(result).toContain("Cleaned: function test() {} // This is a comment"); expect(utils.formatJsDocComment).toHaveBeenCalledTimes(jsDocComments.length); expect(utils.removeCommentsFromCode).toHaveBeenCalledTimes(jsDocComments.length); expect(utils.extractMethodFromCode).not.toHaveBeenCalled(); }); it("should handle unnamed JsDoc comments correctly", () => { // Mock data with an unnamed comment const jsDocComments = [ { name: "Unnamed", type: "Variable", comment: "This is an unnamed variable", code: "let unnamedVar = true; // This is a comment", }, ]; const pathName = "unnamed/path"; const result = (0, mdxContent_1.createMdxContent)(jsDocComments, pathName); // Assertions to verify the content for unnamed JsDoc comment // No header for unnamed JsDoc comment expect(result).toContain("**AST Node Type:** *Variable*"); expect(result).toContain("Formatted: This is an unnamed variable"); expect(result).toContain("Cleaned: let unnamedVar = true; // This is a comment"); expect(result).not.toContain("# Unnamed"); // Ensure "Unnamed" is not included as a heading expect(utils.formatJsDocComment).toHaveBeenCalledTimes(jsDocComments.length); expect(utils.removeCommentsFromCode).toHaveBeenCalledTimes(jsDocComments.length); expect(utils.extractMethodFromCode).not.toHaveBeenCalled(); }); it("should use extractMethodFromCode for method declarations", () => { // Mock data with a method declaration const jsDocComments = [ { name: "testMethod", type: "MethodDeclaration", comment: "This is a test method", code: "class Test { testMethod() { return true; } }", }, ]; const pathName = "test/class"; const result = (0, mdxContent_1.createMdxContent)(jsDocComments, pathName); expect(result).toContain("# testMethod"); expect(result).toContain("Formatted: This is a test method"); expect(result).toContain("Extracted: testMethod"); expect(utils.formatJsDocComment).toHaveBeenCalledTimes(1); expect(utils.removeCommentsFromCode).not.toHaveBeenCalled(); expect(utils.extractMethodFromCode).toHaveBeenCalledTimes(1); expect(utils.extractMethodFromCode).toHaveBeenCalledWith("class Test { testMethod() { return true; } }", "testMethod"); }); it("should handle unnamed method declarations correctly", () => { // Mock data with an unnamed method declaration const jsDocComments = [ { name: "Unnamed", type: "MethodDeclaration", comment: "This is an unnamed method", code: "class Test { someMethod() { return false; } }", }, ]; const pathName = "test/class"; const result = (0, mdxContent_1.createMdxContent)(jsDocComments, pathName); expect(result).toContain("**AST Node Type:** *MethodDeclaration*"); expect(result).toContain("Formatted: This is an unnamed method"); expect(result).toContain("Extracted: Unnamed"); expect(result).not.toContain("# Unnamed"); expect(utils.formatJsDocComment).toHaveBeenCalledTimes(1); expect(utils.removeCommentsFromCode).not.toHaveBeenCalled(); expect(utils.extractMethodFromCode).toHaveBeenCalledTimes(1); expect(utils.extractMethodFromCode).toHaveBeenCalledWith("class Test { someMethod() { return false; } }", "Unnamed"); }); it("should format multiple JsDoc comments correctly", () => { // Mock data with multiple comments const jsDocComments = [ { name: "TestFunction", type: "Function", comment: "This is a test function", code: "function test() { return 'test'; }", }, { name: "testMethod", type: "MethodDeclaration", comment: "This is a test method", code: "class Test { testMethod() { return true; } }", }, { name: "Unnamed", type: "Variable", comment: "This is an unnamed variable", code: "let unnamedVar = true;", }, ]; const pathName = "test/combined"; const result = (0, mdxContent_1.createMdxContent)(jsDocComments, pathName); // Function assertions expect(result).toContain("# TestFunction"); expect(result).toContain("Formatted: This is a test function"); expect(result).toContain("Cleaned: function test() { return 'test'; }"); // Method assertions expect(result).toContain("# testMethod"); expect(result).toContain("Formatted: This is a test method"); expect(result).toContain("Extracted: testMethod"); // Unnamed variable assertions expect(result).toContain("**AST Node Type:** *Variable*"); expect(result).toContain("Formatted: This is an unnamed variable"); expect(result).toContain("Cleaned: let unnamedVar = true;"); // Call count assertions expect(utils.formatJsDocComment).toHaveBeenCalledTimes(3); expect(utils.removeCommentsFromCode).toHaveBeenCalledTimes(2); expect(utils.extractMethodFromCode).toHaveBeenCalledTimes(1); }); });