Skip to content

Commit

Permalink
Updated test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhg772244 committed Jan 7, 2025
1 parent 8dc016e commit 6836e9c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/mermaid/src/diagrams/flowchart/flowDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import type {
FlowVertexTypeParam,
} from './types.js';
import type { NodeMetaData } from '../../types.js';
import type { DiagramDB } from '../../diagram-api/types.js';

const MERMAID_DOM_ID_PREFIX = 'flowchart-';

export class FlowDb {
export class FlowDb implements DiagramDB {
private vertexCounter = 0;
private config = getConfig();
private vertices = new Map<string, FlowVertex>();
Expand Down
25 changes: 21 additions & 4 deletions packages/mermaid/src/mermaidAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { compile, serialize } from 'stylis';
import { Diagram } from './Diagram.js';
import { decodeEntities, encodeEntities } from './utils.js';
import { toBase64 } from './utils/base64.js';
import { FlowDb } from './diagrams/flowchart/flowDb.js';

/**
* @see https://vitest.dev/guide/mocking.html Mock part of a module
Expand Down Expand Up @@ -836,16 +837,17 @@ graph TD;A--x|text including URL space|B;`)
it('should not modify db when rendering different diagrams', async () => {
const flowDiagram1 = await mermaidAPI.getDiagramFromText(
`flowchart LR
%% This is a comment A -- text --> B{node}
A -- text --> B -- text2 --> C`
);
const flwoDiagram2 = await mermaidAPI.getDiagramFromText(
`flowchart LR
%% This is a comment A -- text --> B{node}
`flowchart TD
A -- text --> B -- text2 --> C`
);
// Since flowDiagram will return new Db object each time, we can compare the db to be different.
expect(flwoDiagram1.db).not.toBe(flwoDiagram2.db);
expect(flowDiagram1.db).not.toBe(flwoDiagram2.db);
assert(flowDiagram1.db instanceof FlowDb);
assert(flwoDiagram2.db instanceof FlowDb);
expect(flowDiagram1.db.getDirection()).not.toEqual(flwoDiagram2.db.getDirection());

const classDiagram1 = await mermaidAPI.getDiagramFromText(
`stateDiagram
Expand All @@ -869,4 +871,19 @@ graph TD;A--x|text including URL space|B;`)
expect(classDiagram1.db).toBe(classDiagram2.db);
});
});

// Sequence Diagram currently uses a singleton DB, so this test will fail
it.fails('should not modify db when rendering different sequence diagrams', async () => {
const sequenceDiagram1 = await mermaidAPI.getDiagramFromText(
`sequenceDiagram
Alice->>Bob: Hello Bob, how are you?
Bob-->>John: How about you John?`
);
const sequenceDiagram2 = await mermaidAPI.getDiagramFromText(
`sequenceDiagram
Alice->>Bob: Hello Bob, how are you?
Bob-->>John: How about you John?`
);
expect(sequenceDiagram1.db).not.toBe(sequenceDiagram2.db);
});
});

0 comments on commit 6836e9c

Please sign in to comment.