convert flowDb to class and added test cases.

This commit is contained in:
saurabhg772244
2025-01-15 18:02:04 +05:30
parent bc2cc61240
commit 8fba9c1236
7 changed files with 748 additions and 669 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,11 @@
import { parser } from './parser/classDiagram.jison'; import { parser } from './parser/classDiagram.jison';
import classDb from './classDb.js'; import { ClassDB } from './classDb.js';
describe('class diagram, ', function () { describe('class diagram, ', function () {
describe('when parsing data from a classDiagram it', function () { describe('when parsing data from a classDiagram it', function () {
let classDb;
beforeEach(function () { beforeEach(function () {
classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
parser.yy.clear(); parser.yy.clear();
}); });

View File

@@ -1,13 +1,15 @@
import type { DiagramDefinition } from '../../diagram-api/types.js'; import type { DiagramDefinition } from '../../diagram-api/types.js';
// @ts-ignore: JISON doesn't support types // @ts-ignore: JISON doesn't support types
import parser from './parser/classDiagram.jison'; import parser from './parser/classDiagram.jison';
import db from './classDb.js'; import { ClassDB } from './classDb.js';
import styles from './styles.js'; import styles from './styles.js';
import renderer from './classRenderer-v3-unified.js'; import renderer from './classRenderer-v3-unified.js';
export const diagram: DiagramDefinition = { export const diagram: DiagramDefinition = {
parser, parser,
db, get db() {
return new ClassDB();
},
renderer, renderer,
styles, styles,
init: (cnf) => { init: (cnf) => {
@@ -15,6 +17,5 @@ export const diagram: DiagramDefinition = {
cnf.class = {}; cnf.class = {};
} }
cnf.class.arrowMarkerAbsolute = cnf.arrowMarkerAbsolute; cnf.class.arrowMarkerAbsolute = cnf.arrowMarkerAbsolute;
db.clear();
}, },
}; };

View File

@@ -1,6 +1,6 @@
// @ts-expect-error Jison doesn't export types // @ts-expect-error Jison doesn't export types
import { parser } from './parser/classDiagram.jison'; import { parser } from './parser/classDiagram.jison';
import classDb from './classDb.js'; import { ClassDB } from './classDb.js';
import { vi, describe, it, expect } from 'vitest'; import { vi, describe, it, expect } from 'vitest';
import type { ClassMap, NamespaceNode } from './classTypes.js'; import type { ClassMap, NamespaceNode } from './classTypes.js';
const spyOn = vi.spyOn; const spyOn = vi.spyOn;
@@ -10,8 +10,9 @@ const abstractCssStyle = 'font-style:italic;';
describe('given a basic class diagram, ', function () { describe('given a basic class diagram, ', function () {
describe('when parsing class definition', function () { describe('when parsing class definition', function () {
let classDb: ClassDB;
beforeEach(function () { beforeEach(function () {
classDb.clear(); classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
}); });
it('should handle classes within namespaces', () => { it('should handle classes within namespaces', () => {
@@ -564,8 +565,9 @@ class C13["With Città foreign language"]
}); });
describe('when parsing class defined in brackets', function () { describe('when parsing class defined in brackets', function () {
let classDb: ClassDB;
beforeEach(function () { beforeEach(function () {
classDb.clear(); classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
}); });
@@ -656,8 +658,9 @@ class C13["With Città foreign language"]
}); });
describe('when parsing comments', function () { describe('when parsing comments', function () {
let classDb: ClassDB;
beforeEach(function () { beforeEach(function () {
classDb.clear(); classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
}); });
@@ -746,8 +749,9 @@ foo()
}); });
describe('when parsing click statements', function () { describe('when parsing click statements', function () {
let classDb: ClassDB;
beforeEach(function () { beforeEach(function () {
classDb.clear(); classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
}); });
it('should handle href link', function () { it('should handle href link', function () {
@@ -857,8 +861,9 @@ foo()
}); });
describe('when parsing annotations', function () { describe('when parsing annotations', function () {
let classDb: ClassDB;
beforeEach(function () { beforeEach(function () {
classDb.clear(); classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
}); });
@@ -921,8 +926,9 @@ foo()
describe('given a class diagram with members and methods ', function () { describe('given a class diagram with members and methods ', function () {
describe('when parsing members', function () { describe('when parsing members', function () {
let classDb: ClassDB;
beforeEach(function () { beforeEach(function () {
classDb.clear(); classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
}); });
@@ -980,8 +986,9 @@ describe('given a class diagram with members and methods ', function () {
}); });
describe('when parsing method definition', function () { describe('when parsing method definition', function () {
let classDb: ClassDB;
beforeEach(function () { beforeEach(function () {
classDb.clear(); classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
}); });
@@ -1067,8 +1074,9 @@ describe('given a class diagram with members and methods ', function () {
describe('given a class diagram with generics, ', function () { describe('given a class diagram with generics, ', function () {
describe('when parsing valid generic classes', function () { describe('when parsing valid generic classes', function () {
let classDb: ClassDB;
beforeEach(function () { beforeEach(function () {
classDb.clear(); classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
}); });
@@ -1180,8 +1188,9 @@ namespace space {
describe('given a class diagram with relationships, ', function () { describe('given a class diagram with relationships, ', function () {
describe('when parsing basic relationships', function () { describe('when parsing basic relationships', function () {
let classDb: ClassDB;
beforeEach(function () { beforeEach(function () {
classDb.clear(); classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
}); });
@@ -1714,7 +1723,9 @@ class Class2
}); });
describe('when parsing classDiagram with text labels', () => { describe('when parsing classDiagram with text labels', () => {
let classDb: ClassDB;
beforeEach(function () { beforeEach(function () {
classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
parser.yy.clear(); parser.yy.clear();
}); });
@@ -1897,3 +1908,40 @@ class C13["With Città foreign language"]
}); });
}); });
}); });
describe('class db class', () => {
let classDb: ClassDB;
beforeEach(() => {
classDb = new ClassDB();
});
// This is to ensure that functions used in class JISON are exposed as function from ClassDB
it('should have functions used in class JISON as own property', () => {
const functionsUsedInParser = [
'addRelation',
'cleanupLabel',
'setAccTitle',
'setAccDescription',
'addClassesToNamespace',
'addNamespace',
'setCssClass',
'addMembers',
'addClass',
'setClassLabel',
'addAnnotation',
'addMember',
'addNote',
'defineClass',
'setDirection',
'relationType',
'lineType',
'setClickEvent',
'setTooltip',
'setLink',
'setCssStyle',
] as const satisfies (keyof ClassDB)[];
for (const fun of functionsUsedInParser) {
expect(Object.hasOwn(classDb, fun)).toBe(true);
}
});
});

View File

@@ -1,13 +1,15 @@
import type { DiagramDefinition } from '../../diagram-api/types.js'; import type { DiagramDefinition } from '../../diagram-api/types.js';
// @ts-ignore: JISON doesn't support types // @ts-ignore: JISON doesn't support types
import parser from './parser/classDiagram.jison'; import parser from './parser/classDiagram.jison';
import db from './classDb.js'; import { ClassDB } from './classDb.js';
import styles from './styles.js'; import styles from './styles.js';
import renderer from './classRenderer-v3-unified.js'; import renderer from './classRenderer-v3-unified.js';
export const diagram: DiagramDefinition = { export const diagram: DiagramDefinition = {
parser, parser,
db, get db() {
return new ClassDB();
},
renderer, renderer,
styles, styles,
init: (cnf) => { init: (cnf) => {
@@ -15,6 +17,5 @@ export const diagram: DiagramDefinition = {
cnf.class = {}; cnf.class = {};
} }
cnf.class.arrowMarkerAbsolute = cnf.arrowMarkerAbsolute; cnf.class.arrowMarkerAbsolute = cnf.arrowMarkerAbsolute;
db.clear();
}, },
}; };

View File

@@ -1,8 +1,10 @@
import { parser } from './classDiagram.jison'; import { parser } from './classDiagram.jison';
import classDb from '../classDb.js'; import { ClassDB } from '../classDb.js';
describe('class diagram', function () { describe('class diagram', function () {
let classDb;
beforeEach(function () { beforeEach(function () {
classDb = new ClassDB();
parser.yy = classDb; parser.yy = classDb;
parser.yy.clear(); parser.yy.clear();
}); });

View File

@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'; import { beforeEach, describe, expect, it, vi } from 'vitest';
import assert from 'node:assert';
// ------------------------------------- // -------------------------------------
// Mocks and mocking // Mocks and mocking
@@ -69,6 +70,7 @@ import { compile, serialize } from 'stylis';
import { Diagram } from './Diagram.js'; import { Diagram } from './Diagram.js';
import { decodeEntities, encodeEntities } from './utils.js'; import { decodeEntities, encodeEntities } from './utils.js';
import { toBase64 } from './utils/base64.js'; import { toBase64 } from './utils/base64.js';
import { ClassDB } from './diagrams/class/classDb.js';
/** /**
* @see https://vitest.dev/guide/mocking.html Mock part of a module * @see https://vitest.dev/guide/mocking.html Mock part of a module
@@ -833,4 +835,46 @@ graph TD;A--x|text including URL space|B;`)
expect(diagram.type).toBe('flowchart-v2'); expect(diagram.type).toBe('flowchart-v2');
}); });
}); });
it('should not modify db when rendering different diagrams', async () => {
const classDiagram1 = await mermaidAPI.getDiagramFromText(
`classDiagram
direction TB
class Student {
-idCard : IdCard
}
class IdCard{
-id : int
-name : string
}
class Bike{
-id : int
-name : string
}
Student "1" --o "1" IdCard : carries
Student "1" --o "1" Bike : rides`
);
const classDiagram2 = await mermaidAPI.getDiagramFromText(
`classDiagram
direction LR
class Student {
-idCard : IdCard
}
class IdCard{
-id : int
-name : string
}
class Bike{
-id : int
-name : string
}
Student "1" --o "1" IdCard : carries
Student "1" --o "1" Bike : rides`
);
// Since classDiagram will return new Db object each time, we can compare the db to be different.
expect(classDiagram1.db).not.toBe(classDiagram2.db);
assert(classDiagram1.db instanceof ClassDB);
assert(classDiagram2.db instanceof ClassDB);
expect(classDiagram1.db.getDirection()).not.toEqual(classDiagram2.db.getDirection());
});
}); });