Skip to content

Commit

Permalink
Merge pull request #5065 from mermaid-js/5064_EdgeOffsetEdgeCase
Browse files Browse the repository at this point in the history
fix: #5064 Handle case when line has only one point
  • Loading branch information
sidharthv96 authored Nov 24, 2023
2 parents 8f733c6 + 7b0f6c1 commit 61747b6
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 161 deletions.
3 changes: 3 additions & 0 deletions .vite/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ export const getBuildConfig = ({ minify, core, watch, entryName }: BuildOptions)
output,
},
},
define: {
'import.meta.vitest': 'undefined',
},
resolve: {
extensions: [],
},
Expand Down
12 changes: 12 additions & 0 deletions cypress/integration/rendering/flowchart-v2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,18 @@ A ~~~ B
{}
);
});

it('5064: Should render when subgraph child has links to outside node and subgraph', () => {
imgSnapshotTest(
`flowchart TB
Out --> In
subgraph Sub
In
end
Sub --> In`
);
});

describe('Markdown strings flowchart (#4220)', () => {
describe('html labels', () => {
it('With styling and classes', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/mermaid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mermaid from './mermaid.js';
import { mermaidAPI } from './mermaidAPI.js';
import './diagram-api/diagram-orchestration.js';
import { addDiagrams } from './diagram-api/diagram-orchestration.js';
import { beforeAll, describe, it, expect, vi } from 'vitest';
import { beforeAll, describe, it, expect, vi, afterEach } from 'vitest';
import type { DiagramDefinition } from './diagram-api/types.js';

beforeAll(async () => {
Expand Down
48 changes: 46 additions & 2 deletions packages/mermaid/src/utils/lineWithOffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ const markerOffsets = {
* @returns The angle, deltaX and deltaY
*/
function calculateDeltaAndAngle(
point1: Point | [number, number],
point2: Point | [number, number]
point1?: Point | [number, number],
point2?: Point | [number, number]
): { angle: number; deltaX: number; deltaY: number } {
if (point1 === undefined || point2 === undefined) {
return { angle: 0, deltaX: 0, deltaY: 0 };
}
point1 = pointTransformer(point1);
point2 = pointTransformer(point2);
const [x1, y1] = [point1.x, point1.y];
Expand Down Expand Up @@ -90,3 +93,44 @@ export const getLineFunctionsWithOffset = (
},
};
};

if (import.meta.vitest) {
const { it, expect, describe } = import.meta.vitest;
describe('calculateDeltaAndAngle', () => {
it('should calculate the angle and deltas between two points', () => {
expect(calculateDeltaAndAngle([0, 0], [0, 1])).toStrictEqual({
angle: 1.5707963267948966,
deltaX: 0,
deltaY: 1,
});
expect(calculateDeltaAndAngle([1, 0], [0, -1])).toStrictEqual({
angle: 0.7853981633974483,
deltaX: -1,
deltaY: -1,
});
expect(calculateDeltaAndAngle({ x: 1, y: 0 }, [0, -1])).toStrictEqual({
angle: 0.7853981633974483,
deltaX: -1,
deltaY: -1,
});
expect(calculateDeltaAndAngle({ x: 1, y: 0 }, { x: 1, y: 0 })).toStrictEqual({
angle: NaN,
deltaX: 0,
deltaY: 0,
});
});

it('should calculate the angle and deltas if one point in undefined', () => {
expect(calculateDeltaAndAngle(undefined, [0, 1])).toStrictEqual({
angle: 0,
deltaX: 0,
deltaY: 0,
});
expect(calculateDeltaAndAngle([0, 1], undefined)).toStrictEqual({
angle: 0,
deltaX: 0,
deltaY: 0,
});
});
});
}
3 changes: 2 additions & 1 deletion packages/mermaid/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist"
"outDir": "./dist",
"types": ["vitest/importMeta", "vitest/globals"]
},
"include": ["./src/**/*.ts", "./package.json"]
}
Loading

0 comments on commit 61747b6

Please sign in to comment.