mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-19 07:19:41 +02:00
convert stateDb to class, added test case.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import stateDb from '../stateDb.js';
|
import { StateDB } from '../stateDb.js';
|
||||||
import stateDiagram from './stateDiagram.jison';
|
import stateDiagram from './stateDiagram.jison';
|
||||||
import { setConfig } from '../../../config.js';
|
import { setConfig } from '../../../config.js';
|
||||||
|
|
||||||
@@ -7,7 +7,9 @@ setConfig({
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('state parser can parse...', () => {
|
describe('state parser can parse...', () => {
|
||||||
|
let stateDb;
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
stateDb = new StateDB();
|
||||||
stateDiagram.parser.yy = stateDb;
|
stateDiagram.parser.yy = stateDb;
|
||||||
stateDiagram.parser.yy.clear();
|
stateDiagram.parser.yy.clear();
|
||||||
});
|
});
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import stateDb from '../stateDb.js';
|
import { StateDB } from '../stateDb.js';
|
||||||
import stateDiagram from './stateDiagram.jison';
|
import stateDiagram from './stateDiagram.jison';
|
||||||
import { setConfig } from '../../../config.js';
|
import { setConfig } from '../../../config.js';
|
||||||
|
|
||||||
@@ -7,7 +7,9 @@ setConfig({
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('ClassDefs and classes when parsing a State diagram', () => {
|
describe('ClassDefs and classes when parsing a State diagram', () => {
|
||||||
|
let stateDb;
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
stateDb = new StateDB();
|
||||||
stateDiagram.parser.yy = stateDb;
|
stateDiagram.parser.yy = stateDb;
|
||||||
stateDiagram.parser.yy.clear();
|
stateDiagram.parser.yy.clear();
|
||||||
});
|
});
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { line, curveBasis } from 'd3';
|
import { line, curveBasis } from 'd3';
|
||||||
import idCache from './id-cache.js';
|
import idCache from './id-cache.js';
|
||||||
import stateDb from './stateDb.js';
|
import { StateDB } from './stateDb.js';
|
||||||
import utils from '../../utils.js';
|
import utils from '../../utils.js';
|
||||||
import common from '../common/common.js';
|
import common from '../common/common.js';
|
||||||
import { getConfig } from '../../diagram-api/diagramAPI.js';
|
import { getConfig } from '../../diagram-api/diagramAPI.js';
|
||||||
@@ -414,13 +414,13 @@ let edgeCount = 0;
|
|||||||
export const drawEdge = function (elem, path, relation) {
|
export const drawEdge = function (elem, path, relation) {
|
||||||
const getRelationType = function (type) {
|
const getRelationType = function (type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case stateDb.relationType.AGGREGATION:
|
case StateDB.relationType.AGGREGATION:
|
||||||
return 'aggregation';
|
return 'aggregation';
|
||||||
case stateDb.relationType.EXTENSION:
|
case StateDB.relationType.EXTENSION:
|
||||||
return 'extension';
|
return 'extension';
|
||||||
case stateDb.relationType.COMPOSITION:
|
case StateDB.relationType.COMPOSITION:
|
||||||
return 'composition';
|
return 'composition';
|
||||||
case stateDb.relationType.DEPENDENCY:
|
case StateDB.relationType.DEPENDENCY:
|
||||||
return 'dependency';
|
return 'dependency';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -459,7 +459,7 @@ export const drawEdge = function (elem, path, relation) {
|
|||||||
|
|
||||||
svgPath.attr(
|
svgPath.attr(
|
||||||
'marker-end',
|
'marker-end',
|
||||||
'url(' + url + '#' + getRelationType(stateDb.relationType.DEPENDENCY) + 'End' + ')'
|
'url(' + url + '#' + getRelationType(StateDB.relationType.DEPENDENCY) + 'End' + ')'
|
||||||
);
|
);
|
||||||
|
|
||||||
if (relation.title !== undefined) {
|
if (relation.title !== undefined) {
|
||||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,9 @@
|
|||||||
import stateDb from './stateDb.js';
|
import { StateDB } from './stateDb.js';
|
||||||
|
|
||||||
describe('State Diagram stateDb', () => {
|
describe('State Diagram stateDb', () => {
|
||||||
|
let stateDb;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
stateDb.clear();
|
stateDb = new StateDB();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('addStyleClass', () => {
|
describe('addStyleClass', () => {
|
||||||
@@ -20,8 +21,9 @@ describe('State Diagram stateDb', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('addDescription to a state', () => {
|
describe('addDescription to a state', () => {
|
||||||
|
let stateDb;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
stateDb.clear();
|
stateDb = new StateDB();
|
||||||
stateDb.addState('state1');
|
stateDb.addState('state1');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -73,3 +75,25 @@ describe('State Diagram stateDb', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('state db class', () => {
|
||||||
|
let stateDb;
|
||||||
|
beforeEach(() => {
|
||||||
|
stateDb = new StateDB();
|
||||||
|
});
|
||||||
|
// This is to ensure that functions used in state JISON are exposed as function from StateDb
|
||||||
|
it('should have functions used in flow JISON as own property', () => {
|
||||||
|
const functionsUsedInParser = [
|
||||||
|
'setRootDoc',
|
||||||
|
'trimColon',
|
||||||
|
'getDividerId',
|
||||||
|
'setAccTitle',
|
||||||
|
'setAccDescription',
|
||||||
|
'setDirection',
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const fun of functionsUsedInParser) {
|
||||||
|
expect(Object.hasOwn(stateDb, fun)).toBe(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
@@ -1,11 +1,13 @@
|
|||||||
import { parser } from './parser/stateDiagram.jison';
|
import { parser } from './parser/stateDiagram.jison';
|
||||||
import stateDb from './stateDb.js';
|
import { StateDB } from './stateDb.js';
|
||||||
import stateDiagram from './parser/stateDiagram.jison';
|
import stateDiagram from './parser/stateDiagram.jison';
|
||||||
|
|
||||||
describe('state diagram V2, ', function () {
|
describe('state diagram V2, ', function () {
|
||||||
// TODO - these examples should be put into ./parser/stateDiagram.spec.js
|
// TODO - these examples should be put into ./parser/stateDiagram.spec.js
|
||||||
describe('when parsing an info graph it', function () {
|
describe('when parsing an info graph it', function () {
|
||||||
|
let stateDb;
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
stateDb = new StateDB();
|
||||||
parser.yy = stateDb;
|
parser.yy = stateDb;
|
||||||
stateDiagram.parser.yy = stateDb;
|
stateDiagram.parser.yy = stateDb;
|
||||||
stateDiagram.parser.yy.clear();
|
stateDiagram.parser.yy.clear();
|
||||||
|
@@ -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/stateDiagram.jison';
|
import parser from './parser/stateDiagram.jison';
|
||||||
import db from './stateDb.js';
|
import { StateDB } from './stateDb.js';
|
||||||
import styles from './styles.js';
|
import styles from './styles.js';
|
||||||
import renderer from './stateRenderer-v3-unified.js';
|
import renderer from './stateRenderer-v3-unified.js';
|
||||||
|
|
||||||
export const diagram: DiagramDefinition = {
|
export const diagram: DiagramDefinition = {
|
||||||
parser,
|
parser,
|
||||||
db,
|
get db() {
|
||||||
|
return new StateDB();
|
||||||
|
},
|
||||||
renderer,
|
renderer,
|
||||||
styles,
|
styles,
|
||||||
init: (cnf) => {
|
init: (cnf) => {
|
||||||
|
@@ -1,9 +1,11 @@
|
|||||||
import { parser } from './parser/stateDiagram.jison';
|
import { parser } from './parser/stateDiagram.jison';
|
||||||
import stateDb from './stateDb.js';
|
import { StateDB } from './stateDb.js';
|
||||||
|
|
||||||
describe('state diagram, ', function () {
|
describe('state diagram, ', function () {
|
||||||
describe('when parsing an info graph it', function () {
|
describe('when parsing an info graph it', function () {
|
||||||
|
let stateDb;
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
stateDb = new StateDB();
|
||||||
parser.yy = stateDb;
|
parser.yy = stateDb;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -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/stateDiagram.jison';
|
import parser from './parser/stateDiagram.jison';
|
||||||
import db from './stateDb.js';
|
import { StateDB } from './stateDb.js';
|
||||||
import styles from './styles.js';
|
import styles from './styles.js';
|
||||||
import renderer from './stateRenderer.js';
|
import renderer from './stateRenderer.js';
|
||||||
|
|
||||||
export const diagram: DiagramDefinition = {
|
export const diagram: DiagramDefinition = {
|
||||||
parser,
|
parser,
|
||||||
db,
|
get db() {
|
||||||
|
return new StateDB();
|
||||||
|
},
|
||||||
renderer,
|
renderer,
|
||||||
styles,
|
styles,
|
||||||
init: (cnf) => {
|
init: (cnf) => {
|
||||||
|
@@ -71,6 +71,7 @@ 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';
|
import { ClassDB } from './diagrams/class/classDb.js';
|
||||||
import { FlowDB } from './diagrams/flowchart/flowDb.js';
|
import { FlowDB } from './diagrams/flowchart/flowDb.js';
|
||||||
|
import { StateDB } from './diagrams/state/stateDb.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
|
||||||
@@ -836,6 +837,31 @@ graph TD;A--x|text including URL space|B;`)
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should not modify db when rendering different diagrams', async () => {
|
it('should not modify db when rendering different diagrams', async () => {
|
||||||
|
const stateDiagram1 = await mermaidAPI.getDiagramFromText(
|
||||||
|
`stateDiagram
|
||||||
|
direction LR
|
||||||
|
[*] --> Still
|
||||||
|
Still --> [*]
|
||||||
|
Still --> Moving
|
||||||
|
Moving --> Still
|
||||||
|
Moving --> Crash
|
||||||
|
Crash --> [*]`
|
||||||
|
);
|
||||||
|
const stateDiagram2 = await mermaidAPI.getDiagramFromText(
|
||||||
|
`stateDiagram
|
||||||
|
direction TB
|
||||||
|
[*] --> Still
|
||||||
|
Still --> [*]
|
||||||
|
Still --> Moving
|
||||||
|
Moving --> Still
|
||||||
|
Moving --> Crash
|
||||||
|
Crash --> [*]`
|
||||||
|
);
|
||||||
|
expect(stateDiagram1.db).not.toBe(stateDiagram2.db);
|
||||||
|
assert(stateDiagram1.db instanceof StateDB);
|
||||||
|
assert(stateDiagram2.db instanceof StateDB);
|
||||||
|
expect(stateDiagram1.db.getDirection()).not.toEqual(stateDiagram2.db.getDirection());
|
||||||
|
|
||||||
const flowDiagram1 = await mermaidAPI.getDiagramFromText(
|
const flowDiagram1 = await mermaidAPI.getDiagramFromText(
|
||||||
`flowchart LR
|
`flowchart LR
|
||||||
A -- text --> B -- text2 --> C`
|
A -- text --> B -- text2 --> C`
|
||||||
|
Reference in New Issue
Block a user