fix: failing unit tests

Some optional description over here if you need to add more info

on-behalf-of: @Mermaid-Chart <hello@mermaidchart.com>
This commit is contained in:
darshanr0107
2025-08-06 16:35:54 +05:30
parent 074f18dfb8
commit b61780f735
8 changed files with 45 additions and 40 deletions

View File

@@ -5,6 +5,6 @@
"outDir": "./dist", "outDir": "./dist",
"types": ["vitest/importMeta", "vitest/globals"] "types": ["vitest/importMeta", "vitest/globals"]
}, },
"include": ["./src/**/*.ts"], "include": ["./src/**/*.ts", "./src/**/*.d.ts"],
"typeRoots": ["./src/types"] "typeRoots": ["./src/types"]
} }

View File

@@ -5,6 +5,6 @@
"outDir": "./dist", "outDir": "./dist",
"types": ["vitest/importMeta", "vitest/globals"] "types": ["vitest/importMeta", "vitest/globals"]
}, },
"include": ["./src/**/.ts", "./src/**/*.d.ts"], "include": ["./src/**/*.ts", "./src/**/*.d.ts"],
"typeRoots": ["./src/types"] "typeRoots": ["./src/types"]
} }

View File

@@ -171,7 +171,9 @@ This Markdown should be kept.
expect(buildShapeDoc()).toMatchInlineSnapshot(` expect(buildShapeDoc()).toMatchInlineSnapshot(`
"| **Semantic Name** | **Shape Name** | **Short Name** | **Description** | **Alias Supported** | "| **Semantic Name** | **Shape Name** | **Short Name** | **Description** | **Alias Supported** |
| --------------------------------- | ---------------------- | -------------- | ------------------------------ | ---------------------------------------------------------------- | | --------------------------------- | ---------------------- | -------------- | ------------------------------ | ---------------------------------------------------------------- |
| Bang | Bang | \`bang\` | Bang | \`bang\` |
| Card | Notched Rectangle | \`notch-rect\` | Represents a card | \`card\`, \`notched-rectangle\` | | Card | Notched Rectangle | \`notch-rect\` | Represents a card | \`card\`, \`notched-rectangle\` |
| Cloud | Cloud | \`cloud\` | cloud | \`cloud\` |
| Collate | Hourglass | \`hourglass\` | Represents a collate operation | \`collate\`, \`hourglass\` | | Collate | Hourglass | \`hourglass\` | Represents a collate operation | \`collate\`, \`hourglass\` |
| Com Link | Lightning Bolt | \`bolt\` | Communication link | \`com-link\`, \`lightning-bolt\` | | Com Link | Lightning Bolt | \`bolt\` | Communication link | \`com-link\`, \`lightning-bolt\` |
| Comment | Curly Brace | \`brace\` | Adds a comment | \`brace-l\`, \`comment\` | | Comment | Curly Brace | \`brace\` | Adds a comment | \`brace-l\`, \`comment\` |

View File

@@ -1,5 +1,3 @@
// tests to check that comments are removed
import { cleanupComments } from './comments.js'; import { cleanupComments } from './comments.js';
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from 'vitest';
@@ -10,12 +8,12 @@ describe('comments', () => {
%% This is a comment %% This is a comment
%% This is another comment %% This is another comment
graph TD graph TD
A-->B A-->B
%% This is a comment %% This is a comment
`; `;
expect(cleanupComments(text)).toMatchInlineSnapshot(` expect(cleanupComments(text)).toMatchInlineSnapshot(`
"graph TD "graph TD
A-->B A-->B
" "
`); `);
}); });
@@ -29,9 +27,9 @@ graph TD
%%{ init: {'theme': 'space before init'}}%% %%{ init: {'theme': 'space before init'}}%%
%%{init: {'theme': 'space after ending'}}%% %%{init: {'theme': 'space after ending'}}%%
graph TD graph TD
A-->B A-->B
B-->C B-->C
%% This is a comment %% This is a comment
`; `;
expect(cleanupComments(text)).toMatchInlineSnapshot(` expect(cleanupComments(text)).toMatchInlineSnapshot(`
@@ -39,9 +37,9 @@ graph TD
%%{ init: {'theme': 'space before init'}}%% %%{ init: {'theme': 'space before init'}}%%
%%{init: {'theme': 'space after ending'}}%% %%{init: {'theme': 'space after ending'}}%%
graph TD graph TD
A-->B A-->B
B-->C B-->C
" "
`); `);
}); });
@@ -50,14 +48,14 @@ graph TD
const text = ` const text = `
%% This is a comment %% This is a comment
graph TD graph TD
A-->B A-->B
%% This is a comment %% This is a comment
C-->D C-->D
`; `;
expect(cleanupComments(text)).toMatchInlineSnapshot(` expect(cleanupComments(text)).toMatchInlineSnapshot(`
"graph TD "graph TD
A-->B A-->B
C-->D C-->D
" "
`); `);
}); });
@@ -70,11 +68,11 @@ graph TD
%% This is a comment %% This is a comment
graph TD graph TD
A-->B A-->B
`; `;
expect(cleanupComments(text)).toMatchInlineSnapshot(` expect(cleanupComments(text)).toMatchInlineSnapshot(`
"graph TD "graph TD
A-->B A-->B
" "
`); `);
}); });
@@ -82,12 +80,12 @@ graph TD
it('should remove comments at end of text with no newline', () => { it('should remove comments at end of text with no newline', () => {
const text = ` const text = `
graph TD graph TD
A-->B A-->B
%% This is a comment`; %% This is a comment`;
expect(cleanupComments(text)).toMatchInlineSnapshot(` expect(cleanupComments(text)).toMatchInlineSnapshot(`
"graph TD "graph TD
A-->B A-->B
" "
`); `);
}); });

View File

@@ -4,5 +4,6 @@
* @returns cleaned text * @returns cleaned text
*/ */
export const cleanupComments = (text: string): string => { export const cleanupComments = (text: string): string => {
return text.replace(/^\s*%%(?!{)[^\n]+\n?/gm, '').trimStart(); const cleaned = text.replace(/^\s*%%(?!{)[^\n]+\n?/gm, '');
return cleaned.trimStart();
}; };

View File

@@ -1,7 +1,7 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'; import { describe, it, expect, beforeEach, vi } from 'vitest';
import { MindmapDB } from './mindmapDb.js'; import { MindmapDB } from './mindmapDb.js';
import type { MindmapLayoutNode, MindmapLayoutEdge } from './mindmapDb.js'; import type { MindmapLayoutNode, MindmapLayoutEdge } from './mindmapDb.js';
import { Edge } from '../../rendering-util/types.js'; import type { Edge } from '../../rendering-util/types.js';
// Mock the getConfig function // Mock the getConfig function
vi.mock('../../diagram-api/diagramAPI.js', () => ({ vi.mock('../../diagram-api/diagramAPI.js', () => ({

View File

@@ -178,9 +178,13 @@ export class MindmapDB {
* @param sectionNumber - The section number to assign (undefined for root) * @param sectionNumber - The section number to assign (undefined for root)
*/ */
public assignSections(node: MindmapNode, sectionNumber?: number): void { public assignSections(node: MindmapNode, sectionNumber?: number): void {
// Assign section number to the current node // For root node, section should be undefined (not -1)
node.section = sectionNumber; if (node.level === 0) {
node.section = undefined;
} else {
// For non-root nodes, assign the section number
node.section = sectionNumber;
}
// For root node's children, assign section numbers based on their index // For root node's children, assign section numbers based on their index
// For other nodes, inherit parent's section number // For other nodes, inherit parent's section number
if (node.children) { if (node.children) {
@@ -198,22 +202,24 @@ export class MindmapDB {
*/ */
public flattenNodes(node: MindmapNode, processedNodes: MindmapLayoutNode[]): void { public flattenNodes(node: MindmapNode, processedNodes: MindmapLayoutNode[]): void {
// Build CSS classes for the node // Build CSS classes for the node
let cssClasses = 'mindmap-node'; const cssClasses = ['mindmap-node'];
// Add section-specific classes // Add section-specific classes
if (node.level === 0) { if (node.level === 0) {
// Root node gets special classes // Root node gets special classes
cssClasses += ' section-root section--1'; cssClasses.push('section-root', 'section--1');
} else if (node.section !== undefined) { } else if (node.section !== undefined) {
// Child nodes get section class based on their section number // Child nodes get section class based on their section number
cssClasses += ` section-${node.section}`; cssClasses.push(`section-${node.section}`);
} }
// Add any custom classes from the node // Add any custom classes from the node
if (node.class) { if (node.class) {
cssClasses += ` ${node.class}`; cssClasses.push(node.class);
} }
const classes = cssClasses.join(' ');
// Map mindmap node type to valid shape name // Map mindmap node type to valid shape name
const getShapeFromType = (type: number) => { const getShapeFromType = (type: number) => {
switch (type) { switch (type) {
@@ -237,7 +243,7 @@ export class MindmapDB {
}; };
const processedNode: MindmapLayoutNode = { const processedNode: MindmapLayoutNode = {
id: 'node_' + node.id.toString(), id: node.id.toString(),
domId: 'node_' + node.id.toString(), domId: 'node_' + node.id.toString(),
label: node.descr, label: node.descr,
isGroup: false, isGroup: false,
@@ -245,7 +251,7 @@ export class MindmapDB {
width: node.width, width: node.width,
height: node.height ?? 0, height: node.height ?? 0,
padding: node.padding, padding: node.padding,
cssClasses: cssClasses, cssClasses: classes,
cssStyles: [], cssStyles: [],
look: 'default', look: 'default',
icon: node.icon, icon: node.icon,
@@ -292,8 +298,8 @@ export class MindmapDB {
const edge: MindmapLayoutEdge = { const edge: MindmapLayoutEdge = {
id: `edge_${node.id}_${child.id}`, id: `edge_${node.id}_${child.id}`,
start: 'node_' + node.id.toString(), start: node.id.toString(),
end: 'node_' + child.id.toString(), end: child.id.toString(),
type: 'normal', type: 'normal',
curve: 'basis', curve: 'basis',
thickness: 'normal', thickness: 'normal',

View File

@@ -25,14 +25,8 @@ export async function executeCoseBilkentLayout(
log.debug('Starting cose-bilkent layout algorithm'); log.debug('Starting cose-bilkent layout algorithm');
try { try {
// Validate input data // Validate layout data structure
if (!data.nodes || !Array.isArray(data.nodes)) { validateLayoutData(data);
throw new Error('No nodes found in layout data');
}
if (!data.edges || !Array.isArray(data.edges)) {
throw new Error('No edges found in layout data');
}
// Create and configure cytoscape instance // Create and configure cytoscape instance
const cy = await createCytoscapeInstance(data); const cy = await createCytoscapeInstance(data);
@@ -67,6 +61,10 @@ export function validateLayoutData(data: LayoutData): boolean {
throw new Error('Configuration is required in layout data'); throw new Error('Configuration is required in layout data');
} }
if (!data.rootNode) {
throw new Error('Root node is required');
}
if (!Array.isArray(data.nodes)) { if (!Array.isArray(data.nodes)) {
throw new Error('Nodes array is required in layout data'); throw new Error('Nodes array is required in layout data');
} }