mermaid/src/mermaid.spec.js

218 lines
7.4 KiB
JavaScript
Raw Normal View History

2017-04-11 22:57:57 +08:00
/* eslint-env jasmine */
2017-09-09 21:47:21 +08:00
import mermaid from './mermaid'
2018-03-12 20:52:06 +08:00
import flowDb from './diagrams/flowchart/flowDb'
2017-09-10 21:23:04 +08:00
import flowParser from './diagrams/flowchart/parser/flow'
import flowRenderer from './diagrams/flowchart/flowRenderer'
2017-09-09 21:47:21 +08:00
2017-04-11 22:14:25 +08:00
describe('when using mermaid and ', function () {
describe('when detecting chart type ', function () {
it('should not start rendering with mermaid.startOnLoad set to false', function () {
2017-09-10 10:24:48 +08:00
mermaid.startOnLoad = false
2017-04-11 22:14:25 +08:00
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
2017-09-10 10:24:48 +08:00
spyOn(mermaid, 'init')
2017-04-11 22:14:25 +08:00
mermaid.contentLoaded()
2017-09-10 10:24:48 +08:00
expect(mermaid.init).not.toHaveBeenCalled()
2017-04-11 22:14:25 +08:00
})
it('should start rendering with both startOnLoad set', function () {
2017-09-10 10:24:48 +08:00
mermaid.startOnLoad = true
2017-04-11 22:14:25 +08:00
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
2017-09-10 10:24:48 +08:00
spyOn(mermaid, 'init')
2017-04-11 22:14:25 +08:00
mermaid.contentLoaded()
2017-09-10 10:24:48 +08:00
expect(mermaid.init).toHaveBeenCalled()
2017-04-11 22:14:25 +08:00
})
2018-03-09 15:13:05 +08:00
it('should start rendering with mermaid.startOnLoad', function () {
2017-09-10 10:24:48 +08:00
mermaid.startOnLoad = true
2017-04-11 22:14:25 +08:00
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
2017-09-10 10:24:48 +08:00
spyOn(mermaid, 'init')
2017-04-11 22:14:25 +08:00
mermaid.contentLoaded()
2017-09-10 10:24:48 +08:00
expect(mermaid.init).toHaveBeenCalled()
2017-04-11 22:14:25 +08:00
})
it('should start rendering as a default with no changes performed', function () {
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
2017-09-10 10:24:48 +08:00
spyOn(mermaid, 'init')
2017-04-11 22:14:25 +08:00
mermaid.contentLoaded()
2017-09-10 10:24:48 +08:00
expect(mermaid.init).toHaveBeenCalled()
2017-04-11 22:14:25 +08:00
})
})
describe('when calling addEdges ', function () {
beforeEach(function () {
2018-03-12 20:52:06 +08:00
flowParser.parser.yy = flowDb
flowDb.clear()
2017-04-11 22:14:25 +08:00
})
it('it should handle edges with text', function () {
2017-09-10 21:23:04 +08:00
flowParser.parser.parse('graph TD;A-->|text ex|B;')
flowParser.parser.yy.getVertices()
2017-09-14 19:40:38 +08:00
const edges = flowParser.parser.yy.getEdges()
2017-04-11 22:14:25 +08:00
2017-09-14 19:40:38 +08:00
const mockG = {
2017-04-11 22:14:25 +08:00
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('normal')
expect(options.label.match('text ex')).toBeTruthy()
}
}
2017-09-10 21:23:04 +08:00
flowRenderer.addEdges(edges, mockG)
2017-04-11 22:14:25 +08:00
})
it('should handle edges without text', function () {
2017-09-10 21:23:04 +08:00
flowParser.parser.parse('graph TD;A-->B;')
flowParser.parser.yy.getVertices()
2017-09-14 19:40:38 +08:00
const edges = flowParser.parser.yy.getEdges()
2017-04-11 22:14:25 +08:00
2017-09-14 19:40:38 +08:00
const mockG = {
2017-04-11 22:14:25 +08:00
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('normal')
}
}
2017-09-10 21:23:04 +08:00
flowRenderer.addEdges(edges, mockG)
2017-04-11 22:14:25 +08:00
})
it('should handle open-ended edges', function () {
2017-09-10 21:23:04 +08:00
flowParser.parser.parse('graph TD;A---B;')
flowParser.parser.yy.getVertices()
2017-09-14 19:40:38 +08:00
const edges = flowParser.parser.yy.getEdges()
2017-04-11 22:14:25 +08:00
2017-09-14 19:40:38 +08:00
const mockG = {
2017-04-11 22:14:25 +08:00
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('none')
}
}
2017-09-10 21:23:04 +08:00
flowRenderer.addEdges(edges, mockG)
2017-04-11 22:14:25 +08:00
})
it('should handle edges with styles defined', function () {
2017-09-10 21:23:04 +08:00
flowParser.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;')
flowParser.parser.yy.getVertices()
2017-09-14 19:40:38 +08:00
const edges = flowParser.parser.yy.getEdges()
2017-04-11 22:14:25 +08:00
2017-09-14 19:40:38 +08:00
const mockG = {
2017-04-11 22:14:25 +08:00
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('none')
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:none;')
}
}
2017-09-10 21:23:04 +08:00
flowRenderer.addEdges(edges, mockG)
2017-04-11 22:14:25 +08:00
})
it('should handle edges with interpolation defined', function () {
2017-09-10 21:23:04 +08:00
flowParser.parser.parse('graph TD;A---B; linkStyle 0 interpolate basis')
flowParser.parser.yy.getVertices()
2017-09-14 19:40:38 +08:00
const edges = flowParser.parser.yy.getEdges()
2017-04-11 22:14:25 +08:00
2017-09-14 19:40:38 +08:00
const mockG = {
2017-04-11 22:14:25 +08:00
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('none')
2018-03-17 23:19:13 +08:00
expect(options.curve).toBe('basis') // mocked as string
2017-04-11 22:14:25 +08:00
}
}
2017-09-10 21:23:04 +08:00
flowRenderer.addEdges(edges, mockG)
2017-04-11 22:14:25 +08:00
})
it('should handle edges with text and styles defined', function () {
2017-09-10 21:23:04 +08:00
flowParser.parser.parse('graph TD;A---|the text|B; linkStyle 0 stroke:val1,stroke-width:val2;')
flowParser.parser.yy.getVertices()
2017-09-14 19:40:38 +08:00
const edges = flowParser.parser.yy.getEdges()
2017-04-11 22:14:25 +08:00
2017-09-14 19:40:38 +08:00
const mockG = {
2017-04-11 22:14:25 +08:00
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('none')
expect(options.label.match('the text')).toBeTruthy()
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:none;')
}
}
2017-09-10 21:23:04 +08:00
flowRenderer.addEdges(edges, mockG)
2017-04-11 22:14:25 +08:00
})
it('should set fill to "none" by default when handling edges', function () {
2017-09-10 21:23:04 +08:00
flowParser.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;')
flowParser.parser.yy.getVertices()
2017-09-14 19:40:38 +08:00
const edges = flowParser.parser.yy.getEdges()
2017-04-11 22:14:25 +08:00
2017-09-14 19:40:38 +08:00
const mockG = {
2017-04-11 22:14:25 +08:00
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('none')
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:none;')
}
}
2017-09-10 21:23:04 +08:00
flowRenderer.addEdges(edges, mockG)
2017-04-11 22:14:25 +08:00
})
it('should not set fill to none if fill is set in linkStyle', function () {
2017-09-10 21:23:04 +08:00
flowParser.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2,fill:blue;')
flowParser.parser.yy.getVertices()
2017-09-14 19:40:38 +08:00
const edges = flowParser.parser.yy.getEdges()
const mockG = {
2017-04-11 22:14:25 +08:00
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('none')
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:blue;')
}
}
2017-09-10 21:23:04 +08:00
flowRenderer.addEdges(edges, mockG)
2017-04-11 22:14:25 +08:00
})
})
describe('checking validity of input ', function () {
it('it should throw for an invalid definiton', function () {
expect(() => mermaid.parse('this is not a mermaid diagram definition')).toThrow()
2017-04-11 22:14:25 +08:00
})
it('it should not throw for a valid flow definition', function () {
expect(() => mermaid.parse('graph TD;A--x|text including URL space|B;')).not.toThrow()
2017-04-11 22:14:25 +08:00
})
it('it should throw for an invalid flow definition', function () {
expect(() => mermaid.parse('graph TQ;A--x|text including URL space|B;')).toThrow()
2017-04-11 22:14:25 +08:00
})
it('it should not throw for a valid sequenceDiagram definition', function () {
2017-09-14 19:40:38 +08:00
const text = 'sequenceDiagram\n' +
2017-09-10 00:47:09 +08: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' +
'end'
expect(() => mermaid.parse(text)).not.toThrow()
2017-04-11 22:14:25 +08:00
})
it('it should throw for an invalid sequenceDiagram definition', function () {
2017-09-14 19:40:38 +08:00
const text = 'sequenceDiagram\n' +
2017-09-10 00:47:09 +08: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' +
'end'
expect(() => mermaid.parse(text)).toThrow()
2017-04-11 22:14:25 +08:00
})
})
})