mermaid/src/mermaid.spec.js

276 lines
8.7 KiB
JavaScript
Raw Normal View History

2017-04-11 22:57:57 +08:00
/* eslint-env jasmine */
/**
* Created by knut on 14-11-26.
*/
/**
* Created by knut on 14-11-23.
*/
2017-09-09 21:47:21 +08:00
import mermaid from './mermaid'
global.mermaid = mermaid
2017-04-11 22:14:25 +08:00
2017-09-10 00:47:09 +08:00
const validateDefinition = (text, valid) => {
const foo = {
onError: () => {
}
}
spyOn(foo, 'onError')
mermaid.eventEmitter.on('parseError', (err, hash) => {
foo.onError(err)
})
var res = mermaid.parse(text)
if (valid) {
expect(res).toBe(true)
expect(foo.onError).not.toHaveBeenCalled()
} else {
expect(res).toBe(false)
expect(foo.onError).toHaveBeenCalled()
}
}
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_config.startOnLoad set to false', function () {
2017-09-10 00:47:09 +08:00
global.mermaid_config = { startOnLoad: false }
2017-04-11 22:14:25 +08:00
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
spyOn(global.mermaid, 'init')
mermaid.contentLoaded()
expect(global.mermaid.init).not.toHaveBeenCalled()
})
it('should not start rendering with mermaid.startOnLoad set to false', function () {
global.mermaid.startOnLoad = false
2017-09-10 00:47:09 +08:00
global.mermaid_config = { startOnLoad: true }
2017-04-11 22:14:25 +08:00
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
spyOn(global.mermaid, 'init')
mermaid.contentLoaded()
expect(global.mermaid.init).not.toHaveBeenCalled()
})
it('should start rendering with both startOnLoad set', function () {
global.mermaid.startOnLoad = true
2017-09-10 00:47:09 +08:00
global.mermaid_config = { startOnLoad: true }
2017-04-11 22:14:25 +08:00
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
spyOn(global.mermaid, 'init')
mermaid.contentLoaded()
expect(global.mermaid.init).toHaveBeenCalled()
})
it('should start rendering with mermaid.startOnLoad set and no mermaid_config defined', function () {
global.mermaid.startOnLoad = true
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
spyOn(global.mermaid, 'init')
mermaid.contentLoaded()
expect(global.mermaid.init).toHaveBeenCalled()
})
it('should start rendering as a default with no changes performed', function () {
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
spyOn(global.mermaid, 'init')
mermaid.contentLoaded()
expect(global.mermaid.init).toHaveBeenCalled()
})
})
describe('when calling addEdges ', function () {
var graph = require('./diagrams/flowchart/graphDb')
var flow = require('./diagrams/flowchart/parser/flow')
var flowRend = require('./diagrams/flowchart/flowRenderer')
beforeEach(function () {
2017-09-10 00:47:09 +08:00
global.mermaid_config = { startOnLoad: false }
2017-04-11 22:14:25 +08:00
flow.parser.yy = graph
graph.clear()
})
it('it should handle edges with text', function () {
flow.parser.parse('graph TD;A-->|text ex|B;')
flow.parser.yy.getVertices()
var edges = flow.parser.yy.getEdges()
var mockG = {
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()
}
}
flowRend.addEdges(edges, mockG)
})
it('should handle edges without text', function () {
flow.parser.parse('graph TD;A-->B;')
flow.parser.yy.getVertices()
var edges = flow.parser.yy.getEdges()
var mockG = {
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('normal')
}
}
flowRend.addEdges(edges, mockG)
})
it('should handle open-ended edges', function () {
flow.parser.parse('graph TD;A---B;')
flow.parser.yy.getVertices()
var edges = flow.parser.yy.getEdges()
var mockG = {
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('none')
}
}
flowRend.addEdges(edges, mockG)
})
it('should handle edges with styles defined', function () {
flow.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;')
flow.parser.yy.getVertices()
var edges = flow.parser.yy.getEdges()
var mockG = {
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;')
}
}
flowRend.addEdges(edges, mockG)
})
it('should handle edges with interpolation defined', function () {
flow.parser.parse('graph TD;A---B; linkStyle 0 interpolate basis')
flow.parser.yy.getVertices()
var edges = flow.parser.yy.getEdges()
var mockG = {
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('none')
expect(options.lineInterpolate).toBe('basis')
}
}
flowRend.addEdges(edges, mockG)
})
it('should handle edges with text and styles defined', function () {
flow.parser.parse('graph TD;A---|the text|B; linkStyle 0 stroke:val1,stroke-width:val2;')
flow.parser.yy.getVertices()
var edges = flow.parser.yy.getEdges()
var mockG = {
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;')
}
}
flowRend.addEdges(edges, mockG)
})
it('should set fill to "none" by default when handling edges', function () {
flow.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;')
flow.parser.yy.getVertices()
var edges = flow.parser.yy.getEdges()
var mockG = {
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;')
}
}
flowRend.addEdges(edges, mockG)
})
it('should not set fill to none if fill is set in linkStyle', function () {
flow.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2,fill:blue;')
flow.parser.yy.getVertices()
var edges = flow.parser.yy.getEdges()
var mockG = {
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;')
}
}
flowRend.addEdges(edges, mockG)
})
})
describe('checking validity of input ', function () {
it('it should return false for an invalid definiton', function () {
2017-09-10 00:47:09 +08:00
validateDefinition('this is not a mermaid diagram definition', false)
2017-04-11 22:14:25 +08:00
})
it('it should return true for a valid flow definition', function () {
2017-09-10 00:47:09 +08:00
validateDefinition('graph TD;A--x|text including URL space|B;', true)
2017-04-11 22:14:25 +08:00
})
it('it should return false for an invalid flow definition', function () {
2017-09-10 00:47:09 +08:00
validateDefinition('graph TQ;A--x|text including URL space|B;', false)
2017-04-11 22:14:25 +08:00
})
it('it should return true for a valid sequenceDiagram definition', function () {
var str = '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'
validateDefinition(str, true)
2017-04-11 22:14:25 +08:00
})
2017-04-11 22:14:25 +08:00
it('it should return false for an invalid sequenceDiagram definition', function () {
var str = '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'
validateDefinition(str, false)
2017-04-11 22:14:25 +08:00
})
2017-04-11 22:14:25 +08:00
it('it should return true for a valid dot definition', function () {
2017-09-10 00:47:09 +08:00
validateDefinition('digraph\n' +
'{\n' +
' a -> b -> c -- d -> e;\n' +
' a -- e;\n' +
'}', true)
2017-04-11 22:14:25 +08:00
})
2017-09-10 00:31:29 +08:00
it('it should return false for an invalid dot definition', function () {
2017-09-10 00:47:09 +08:00
validateDefinition('digraph\n' +
'{\n' +
'a -:> b -> c -- d -> e;\n' +
'a -- e;\n' +
'}', false)
2017-09-10 00:31:29 +08:00
})
2017-04-11 22:14:25 +08:00
})
})