mermaid/src/mermaid.spec.js

232 lines
7.9 KiB
JavaScript
Raw Normal View History

2019-09-12 21:59:13 +02:00
import mermaid from './mermaid';
import flowDb from './diagrams/flowchart/flowDb';
import flowParser from './diagrams/flowchart/parser/flow';
import flowRenderer from './diagrams/flowchart/flowRenderer';
2022-07-04 18:22:36 +02:00
import Diagram from './Diagram';
2019-09-12 21:59:13 +02:00
2021-07-15 10:14:35 +02:00
const spyOn = jest.spyOn;
describe('when using mermaid and ', function () {
describe('when detecting chart type ', function () {
it('should not start rendering with mermaid.startOnLoad set to false', function () {
2019-09-12 21:59:13 +02:00
mermaid.startOnLoad = false;
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>';
spyOn(mermaid, 'init');
mermaid.contentLoaded();
expect(mermaid.init).not.toHaveBeenCalled();
});
it('should start rendering with both startOnLoad set', function () {
2019-09-12 21:59:13 +02:00
mermaid.startOnLoad = true;
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>';
spyOn(mermaid, 'init');
mermaid.contentLoaded();
expect(mermaid.init).toHaveBeenCalled();
});
it('should start rendering with mermaid.startOnLoad', function () {
2019-09-12 21:59:13 +02:00
mermaid.startOnLoad = true;
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>';
spyOn(mermaid, 'init');
mermaid.contentLoaded();
expect(mermaid.init).toHaveBeenCalled();
});
it('should start rendering as a default with no changes performed', function () {
2019-09-12 21:59:13 +02:00
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>';
spyOn(mermaid, 'init');
mermaid.contentLoaded();
expect(mermaid.init).toHaveBeenCalled();
});
});
describe('when calling addEdges ', function () {
beforeEach(function () {
2019-09-12 21:59:13 +02:00
flowParser.parser.yy = flowDb;
flowDb.clear();
2020-09-16 20:33:04 +02:00
flowDb.setGen('gen-2');
2019-09-12 21:59:13 +02:00
});
it('it should handle edges with text', function () {
2022-07-04 18:22:36 +02:00
const diag = new Diagram('graph TD;A-->|text ex|B;');
diag.db.getVertices();
const edges = diag.db.getEdges();
2017-04-11 16:14:25 +02:00
2017-09-14 13:40:38 +02:00
const mockG = {
setEdge: function (start, end, options) {
2020-09-16 20:33:04 +02:00
expect(start).toContain('flowchart-A-');
expect(end).toContain('flowchart-B-');
2019-09-12 21:59:13 +02:00
expect(options.arrowhead).toBe('normal');
expect(options.label.match('text ex')).toBeTruthy();
},
2019-09-12 21:59:13 +02:00
};
2017-04-11 16:14:25 +02:00
2022-07-04 18:22:36 +02:00
flowRenderer.addEdges(edges, mockG, diag);
2019-09-12 21:59:13 +02:00
});
2017-04-11 16:14:25 +02:00
it('should handle edges without text', function () {
2022-07-04 18:22:36 +02:00
const diag = new Diagram('graph TD;A-->B;');
diag.db.getVertices();
const edges = diag.db.getEdges();
2017-04-11 16:14:25 +02:00
2017-09-14 13:40:38 +02:00
const mockG = {
setEdge: function (start, end, options) {
2020-09-16 20:33:04 +02:00
expect(start).toContain('flowchart-A-');
expect(end).toContain('flowchart-B-');
2019-09-12 21:59:13 +02:00
expect(options.arrowhead).toBe('normal');
},
2019-09-12 21:59:13 +02:00
};
2017-04-11 16:14:25 +02:00
2022-07-04 18:22:36 +02:00
flowRenderer.addEdges(edges, mockG, diag);
2019-09-12 21:59:13 +02:00
});
2017-04-11 16:14:25 +02:00
it('should handle open-ended edges', function () {
2022-07-04 18:22:36 +02:00
const diag = new Diagram('graph TD;A---B;');
diag.db.getVertices();
const edges = diag.db.getEdges();
2017-04-11 16:14:25 +02:00
2017-09-14 13:40:38 +02:00
const mockG = {
setEdge: function (start, end, options) {
2020-09-16 20:33:04 +02:00
expect(start).toContain('flowchart-A-');
expect(end).toContain('flowchart-B-');
2019-09-12 21:59:13 +02:00
expect(options.arrowhead).toBe('none');
},
2019-09-12 21:59:13 +02:00
};
2017-04-11 16:14:25 +02:00
2022-07-04 18:22:36 +02:00
flowRenderer.addEdges(edges, mockG, diag);
2019-09-12 21:59:13 +02:00
});
2017-04-11 16:14:25 +02:00
it('should handle edges with styles defined', function () {
2022-07-04 18:22:36 +02:00
const diag = new Diagram('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;');
diag.db.getVertices();
const edges = diag.db.getEdges();
2017-04-11 16:14:25 +02:00
2017-09-14 13:40:38 +02:00
const mockG = {
setEdge: function (start, end, options) {
2020-09-16 20:33:04 +02:00
expect(start).toContain('flowchart-A-');
expect(end).toContain('flowchart-B-');
2019-09-12 21:59:13 +02:00
expect(options.arrowhead).toBe('none');
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:none;');
},
2019-09-12 21:59:13 +02:00
};
2017-04-11 16:14:25 +02:00
2022-07-04 18:22:36 +02:00
flowRenderer.addEdges(edges, mockG, diag);
2019-09-12 21:59:13 +02:00
});
it('should handle edges with interpolation defined', function () {
2022-07-04 18:22:36 +02:00
const diag = new Diagram('graph TD;A---B; linkStyle 0 interpolate basis');
diag.db.getVertices();
const edges = diag.db.getEdges();
2017-04-11 16:14:25 +02:00
2017-09-14 13:40:38 +02:00
const mockG = {
setEdge: function (start, end, options) {
2020-09-16 20:33:04 +02:00
expect(start).toContain('flowchart-A-');
expect(end).toContain('flowchart-B-');
2019-09-12 21:59:13 +02:00
expect(options.arrowhead).toBe('none');
expect(options.curve).toBe('basis'); // mocked as string
},
2019-09-12 21:59:13 +02:00
};
2017-04-11 16:14:25 +02:00
2022-07-04 18:22:36 +02:00
flowRenderer.addEdges(edges, mockG, diag);
2019-09-12 21:59:13 +02:00
});
it('should handle edges with text and styles defined', function () {
2022-07-04 18:22:36 +02:00
const diag = new Diagram(
2019-09-12 21:59:13 +02:00
'graph TD;A---|the text|B; linkStyle 0 stroke:val1,stroke-width:val2;'
);
2022-07-04 18:22:36 +02:00
diag.db.getVertices();
const edges = diag.db.getEdges();
2017-04-11 16:14:25 +02:00
2017-09-14 13:40:38 +02:00
const mockG = {
setEdge: function (start, end, options) {
2020-09-16 20:33:04 +02:00
expect(start).toContain('flowchart-A-');
expect(end).toContain('flowchart-B-');
2019-09-12 21:59:13 +02:00
expect(options.arrowhead).toBe('none');
expect(options.label.match('the text')).toBeTruthy();
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:none;');
},
2019-09-12 21:59:13 +02:00
};
2017-04-11 16:14:25 +02:00
2022-07-04 18:22:36 +02:00
flowRenderer.addEdges(edges, mockG, diag);
2019-09-12 21:59:13 +02:00
});
2017-04-11 16:14:25 +02:00
it('should set fill to "none" by default when handling edges', function () {
2022-07-04 18:22:36 +02:00
const diag = new Diagram('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;');
diag.db.getVertices();
const edges = diag.db.getEdges();
2017-04-11 16:14:25 +02:00
2017-09-14 13:40:38 +02:00
const mockG = {
setEdge: function (start, end, options) {
2020-09-16 20:33:04 +02:00
expect(start).toContain('flowchart-A-');
expect(end).toContain('flowchart-B');
2019-09-12 21:59:13 +02:00
expect(options.arrowhead).toBe('none');
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:none;');
},
2019-09-12 21:59:13 +02:00
};
2017-04-11 16:14:25 +02:00
2022-07-04 18:22:36 +02:00
flowRenderer.addEdges(edges, mockG, diag);
2019-09-12 21:59:13 +02:00
});
2017-04-11 16:14:25 +02:00
it('should not set fill to none if fill is set in linkStyle', function () {
2022-07-04 18:22:36 +02:00
const diag = new Diagram(
2019-09-12 21:59:13 +02:00
'graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2,fill:blue;'
);
2022-07-04 18:22:36 +02:00
diag.db.getVertices();
const edges = diag.db.getEdges();
2017-09-14 13:40:38 +02:00
const mockG = {
setEdge: function (start, end, options) {
2020-09-16 20:33:04 +02:00
expect(start).toContain('flowchart-A-');
expect(end).toContain('flowchart-B-');
2019-09-12 21:59:13 +02:00
expect(options.arrowhead).toBe('none');
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:blue;');
},
2019-09-12 21:59:13 +02:00
};
2022-07-04 18:22:36 +02:00
flowRenderer.addEdges(edges, mockG, diag);
2019-09-12 21:59:13 +02:00
});
});
describe('checking validity of input ', function () {
beforeEach(function () {
flowParser.parser.yy = flowDb;
flowDb.clear();
2020-09-16 20:33:04 +02:00
flowDb.setGen('gen-2');
});
it('it should throw for an invalid definition', function () {
2019-09-12 21:59:13 +02:00
expect(() => mermaid.parse('this is not a mermaid diagram definition')).toThrow();
});
it('it should not throw for a valid flow definition', function () {
2019-09-12 21:59:13 +02:00
expect(() => mermaid.parse('graph TD;A--x|text including URL space|B;')).not.toThrow();
});
it('it should throw for an invalid flow definition', function () {
2019-09-12 21:59:13 +02:00
expect(() => mermaid.parse('graph TQ;A--x|text including URL space|B;')).toThrow();
});
it('it should not throw for a valid sequenceDiagram definition', function () {
2019-09-12 21:59:13 +02:00
const text =
'sequenceDiagram\n' +
2017-09-09 18:47:09 +02:00
'Alice->Bob: Hello Bob, how are you?\n\n' +
'%% Comment\n' +
'Note right of Bob: Bob thinks\n' +
'alt isWell\n\n' +
'Bob-->Alice: I am good thanks!\n' +
'else isSick\n' +
'Bob-->Alice: Feel sick...\n' +
2019-09-12 21:59:13 +02:00
'end';
expect(() => mermaid.parse(text)).not.toThrow();
});
it('it should throw for an invalid sequenceDiagram definition', function () {
2019-09-12 21:59:13 +02:00
const text =
'sequenceDiagram\n' +
2017-09-09 18:47:09 +02:00
'Alice:->Bob: Hello Bob, how are you?\n\n' +
'%% Comment\n' +
'Note right of Bob: Bob thinks\n' +
'alt isWell\n\n' +
'Bob-->Alice: I am good thanks!\n' +
'else isSick\n' +
'Bob-->Alice: Feel sick...\n' +
2019-09-12 21:59:13 +02:00
'end';
expect(() => mermaid.parse(text)).toThrow();
});
});
});