fix: add tests for deprecated flowchart.htmlLabels config option , update insertEdgeLabel to use new getEffectiveHtmlLabels helper

on-behalf-of: @Mermaid-Chart <hello@mermaidchart.com>
This commit is contained in:
darshanr0107
2025-09-25 14:07:50 +05:30
parent 1672edbd49
commit 0c659d486d
2 changed files with 63 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ import { assert, beforeEach, describe, expect, it, vi } from 'vitest';
import assignWithDepth from './assignWithDepth.js';
import type { MermaidConfig } from './config.type.js';
import { getEffectiveHtmlLabels } from './config.js';
import mermaid from './mermaid.js';
import mermaidAPI, {
appendDivSvgG,
@@ -358,10 +359,11 @@ describe('mermaidAPI', () => {
});
describe('no htmlLabels in the configuration', () => {
const mocked_config_no_htmlLabels = {
const mocked_config_no_htmlLabels: MermaidConfig = {
themeCSS: 'default',
fontFamily: 'serif',
altFontFamily: 'sans-serif',
htmlLabels: false, // Explicitly set to false
};
describe('creates styles for shape elements "rect", "polygon", "ellipse", and "circle"', () => {
@@ -1148,4 +1150,63 @@ flowchart TD
}
);
});
describe('flowchart.htmlLabels deprecation behavior', () => {
beforeEach(() => {
mermaidAPI.globalReset();
});
it('should use root-level htmlLabels when only root-level is set', () => {
const config: MermaidConfig = { htmlLabels: true };
expect(config.htmlLabels).toBe(true);
const config2: MermaidConfig = { htmlLabels: false };
expect(config2.htmlLabels).toBe(false);
});
it('should check config.htmlLabels value directly when set', () => {
const config1: MermaidConfig = { htmlLabels: true };
expect(config1.htmlLabels).toBe(true);
const config2: MermaidConfig = { htmlLabels: false };
expect(config2.htmlLabels).toBe(false);
const config3: MermaidConfig = { htmlLabels: undefined };
expect(config3.htmlLabels).toBeUndefined();
});
it('should use getEffectiveHtmlLabels only when fallback logic is needed', () => {
// Only call getEffectiveHtmlLabels when we need the fallback behavior
const configWithDeprecated: MermaidConfig = { flowchart: { htmlLabels: true } };
expect(getEffectiveHtmlLabels(configWithDeprecated)).toBe(true);
const configWithBoth: MermaidConfig = {
htmlLabels: false,
flowchart: { htmlLabels: true },
};
expect(getEffectiveHtmlLabels(configWithBoth)).toBe(false); // Root takes precedence
const configEmpty: MermaidConfig = {};
expect(getEffectiveHtmlLabels(configEmpty)).toBe(true); // Default to true
});
it('should verify the precedence logic: config.htmlLabels ?? config.flowchart?.htmlLabels ?? true', () => {
// Test the exact precedence chain
const config1: MermaidConfig = { htmlLabels: true };
const result1 = config1.htmlLabels ?? config1.flowchart?.htmlLabels ?? true;
expect(result1).toBe(true);
const config2: MermaidConfig = { htmlLabels: false };
const result2 = config2.htmlLabels ?? config2.flowchart?.htmlLabels ?? true;
expect(result2).toBe(false);
const config3: MermaidConfig = { flowchart: { htmlLabels: true } };
const result3 = config3.htmlLabels ?? config3.flowchart?.htmlLabels ?? true;
expect(result3).toBe(true);
const config4: MermaidConfig = {};
const result4 = config4.htmlLabels ?? config4.flowchart?.htmlLabels ?? true;
expect(result4).toBe(true);
});
});
});

View File

@@ -30,7 +30,6 @@ import rough from 'roughjs';
import createLabel from './createLabel.js';
import { addEdgeMarkers } from './edgeMarker.ts';
import { isLabelStyle, styles2String } from './shapes/handDrawnShapeStyles.js';
import { evaluate } from '../../diagrams/common/common.js';
export const edgeLabels = new Map();
export const terminalLabels = new Map();
@@ -46,7 +45,7 @@ export const getLabelStyles = (styleArray) => {
};
export const insertEdgeLabel = async (elem, edge) => {
let useHtmlLabels = evaluate(getConfig().flowchart.htmlLabels);
let useHtmlLabels = getEffectiveHtmlLabels(getConfig());
const { labelStyles } = styles2String(edge);
edge.labelStyle = labelStyles;