fix: unitTests after tripleParsing removal

This commit is contained in:
Sidharth Vinod 2023-08-03 23:21:49 +05:30
parent 10d5b5d1d6
commit 95efc1786f
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
3 changed files with 16 additions and 24 deletions

View File

@ -4,8 +4,9 @@ import { getDiagram, registerDiagram } from './diagram-api/diagramAPI.js';
import { detectType, getDiagramLoader } from './diagram-api/detectType.js'; import { detectType, getDiagramLoader } from './diagram-api/detectType.js';
import { extractFrontMatter } from './diagram-api/frontmatter.js'; import { extractFrontMatter } from './diagram-api/frontmatter.js';
import { UnknownDiagramError } from './errors.js'; import { UnknownDiagramError } from './errors.js';
import { DetailedError } from './utils.js';
import { cleanupComments } from './diagram-api/comments.js'; import { cleanupComments } from './diagram-api/comments.js';
import type { DetailedError } from './utils.js';
import type { MermaidConfig } from './config.type.js';
export type ParseErrorFunction = (err: string | DetailedError | unknown, hash?: any) => void; export type ParseErrorFunction = (err: string | DetailedError | unknown, hash?: any) => void;
@ -18,6 +19,7 @@ export class Diagram {
parser; parser;
renderer; renderer;
db; db;
private init?: (config: MermaidConfig) => void;
private detectError?: UnknownDiagramError; private detectError?: UnknownDiagramError;
constructor(public text: string) { constructor(public text: string) {
this.text += '\n'; this.text += '\n';
@ -48,11 +50,7 @@ export class Diagram {
originalParse(cleanupComments(extractFrontMatter(text, this.db))); originalParse(cleanupComments(extractFrontMatter(text, this.db)));
this.parser.parser.yy = this.db; this.parser.parser.yy = this.db;
this.db.clear?.(); this.init = diagram.init;
if (diagram.init) {
diagram.init(cnf);
log.info('Initialized diagram ' + this.type, cnf);
}
this.parse(); this.parse();
} }
@ -60,6 +58,12 @@ export class Diagram {
if (this.detectError) { if (this.detectError) {
throw this.detectError; throw this.detectError;
} }
this.db.clear?.();
if (this.init) {
const config = configApi.getConfig();
this.init(config);
log.info('Initialized diagram ' + this.type, config);
}
this.parser.parse(this.text); this.parser.parse(this.text);
} }

View File

@ -173,13 +173,12 @@ describe('more than one sequence diagram', () => {
describe('when parsing a sequenceDiagram', function () { describe('when parsing a sequenceDiagram', function () {
beforeEach(function () { beforeEach(function () {
// diagram.db = sequenceDb; // diagram.db = sequenceDb;
// diagram.db.clear(); //
diagram = new Diagram(` diagram = new Diagram(`
sequenceDiagram sequenceDiagram
Alice->Bob:Hello Bob, how are you? Alice->Bob:Hello Bob, how are you?
Note right of Bob: Bob thinks Note right of Bob: Bob thinks
Bob-->Alice: I am good thanks!`); Bob-->Alice: I am good thanks!`);
diagram.db.clear();
}); });
it('should handle a sequenceDiagram definition', async function () { it('should handle a sequenceDiagram definition', async function () {
const str = ` const str = `
@ -1482,8 +1481,6 @@ describe('when checking the bounds in a sequenceDiagram', function () {
let conf; let conf;
beforeEach(function () { beforeEach(function () {
mermaidAPI.reset(); mermaidAPI.reset();
// diagram.db = sequenceDb;
// diagram.db.clear();
diagram.renderer.bounds.init(); diagram.renderer.bounds.init();
conf = diagram.db.getConfig(); conf = diagram.db.getConfig();
}); });
@ -1635,7 +1632,6 @@ sequenceDiagram
Alice->Bob:Hello Bob, how are you? Alice->Bob:Hello Bob, how are you?
Note right of Bob: Bob thinks Note right of Bob: Bob thinks
Bob-->Alice: I am good thanks!`); Bob-->Alice: I am good thanks!`);
diagram.db.clear();
}); });
['tspan', 'fo', 'old', undefined].forEach(function (textPlacement) { ['tspan', 'fo', 'old', undefined].forEach(function (textPlacement) {
it(` it(`
@ -2009,8 +2005,6 @@ describe('when rendering a sequenceDiagram with actor mirror activated', () => {
let conf; let conf;
beforeEach(function () { beforeEach(function () {
mermaidAPI.reset(); mermaidAPI.reset();
// diagram.db = sequenceDb;
diagram.db.clear();
conf = diagram.db.getConfig(); conf = diagram.db.getConfig();
diagram.renderer.bounds.init(); diagram.renderer.bounds.init();
}); });
@ -2052,12 +2046,9 @@ describe('when rendering a sequenceDiagram with directives', () => {
mermaidAPI.initialize({ sequence: conf }); mermaidAPI.initialize({ sequence: conf });
}); });
let conf;
beforeEach(function () { beforeEach(function () {
mermaidAPI.reset(); mermaidAPI.reset();
// diagram.db = sequenceDb; // diagram.db = sequenceDb;
diagram.db.clear();
conf = diagram.db.getConfig();
diagram.renderer.bounds.init(); diagram.renderer.bounds.init();
}); });
@ -2069,10 +2060,7 @@ sequenceDiagram
participant Alice participant Alice
`; `;
diagram = new Diagram(str); diagram = new Diagram(str);
diagram.renderer.bounds.init(); diagram.renderer.bounds.init();
await mermaidAPI.parse(str);
diagram.renderer.draw(str, 'tst', '1.2.3', diagram); diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
const { bounds, models } = diagram.renderer.bounds.getBounds(); const { bounds, models } = diagram.renderer.bounds.getBounds();
@ -2093,7 +2081,7 @@ sequenceDiagram
participant Alice participant Alice
`; `;
diagram.parse(str); diagram = new Diagram(str);
diagram.renderer.draw(str, 'tst', '1.2.3', diagram); diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
const { bounds, models } = diagram.renderer.bounds.getBounds(); const { bounds, models } = diagram.renderer.bounds.getBounds();
@ -2114,7 +2102,7 @@ Alice->Bob:Hello Bob, how are you?
Note right of Bob: Bob thinks Note right of Bob: Bob thinks
Bob-->Alice: I am good thanks!`; Bob-->Alice: I am good thanks!`;
await mermaidAPI.parse(str1); diagram = new Diagram(str1);
diagram.renderer.draw(str1, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers diagram.renderer.draw(str1, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers
expect(diagram.db.showSequenceNumbers()).toBe(true); expect(diagram.db.showSequenceNumbers()).toBe(true);
@ -2124,7 +2112,7 @@ Alice->Bob:Hello Bob, how are you?
Note right of Bob: Bob thinks Note right of Bob: Bob thinks
Bob-->Alice: I am good thanks!`; Bob-->Alice: I am good thanks!`;
await mermaidAPI.parse(str2); diagram = new Diagram(str2);
diagram.renderer.draw(str2, 'tst', '1.2.3', diagram); diagram.renderer.draw(str2, 'tst', '1.2.3', diagram);
expect(diagram.db.showSequenceNumbers()).toBe(false); expect(diagram.db.showSequenceNumbers()).toBe(false);
}); });

View File

@ -108,8 +108,8 @@ export interface RenderResult {
async function parse(text: string, parseOptions?: ParseOptions): Promise<boolean> { async function parse(text: string, parseOptions?: ParseOptions): Promise<boolean> {
addDiagrams(); addDiagrams();
try { try {
const diagram = await getDiagramFromText(text); await getDiagramFromText(text);
diagram.parse(); // diagram.parse();
} catch (error) { } catch (error) {
if (parseOptions?.suppressErrors) { if (parseOptions?.suppressErrors) {
return false; return false;