forked from mermaid-js/mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmermaid.spec.js
248 lines (214 loc) · 8.45 KB
/
mermaid.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* eslint-env jasmine */
import mermaid from './mermaid'
import graphDb from './diagrams/flowchart/graphDb'
import flowParser from './diagrams/flowchart/parser/flow'
import flowRenderer from './diagrams/flowchart/flowRenderer'
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 () {
global.mermaid_config = { startOnLoad: false }
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
spyOn(mermaid, 'init')
mermaid.contentLoaded()
expect(mermaid.init).not.toHaveBeenCalled()
})
it('should not start rendering with mermaid.startOnLoad set to false', function () {
mermaid.startOnLoad = false
global.mermaid_config = { startOnLoad: true }
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 () {
mermaid.startOnLoad = true
global.mermaid_config = { 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 set and no mermaid_config defined', function () {
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 () {
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 () {
global.mermaid_config = { startOnLoad: false }
flowParser.parser.yy = graphDb
graphDb.clear()
})
it('it should handle edges with text', function () {
flowParser.parser.parse('graph TD;A-->|text ex|B;')
flowParser.parser.yy.getVertices()
const edges = flowParser.parser.yy.getEdges()
const 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()
}
}
flowRenderer.addEdges(edges, mockG)
})
it('should handle edges without text', function () {
flowParser.parser.parse('graph TD;A-->B;')
flowParser.parser.yy.getVertices()
const edges = flowParser.parser.yy.getEdges()
const mockG = {
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('normal')
}
}
flowRenderer.addEdges(edges, mockG)
})
it('should handle open-ended edges', function () {
flowParser.parser.parse('graph TD;A---B;')
flowParser.parser.yy.getVertices()
const edges = flowParser.parser.yy.getEdges()
const mockG = {
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('none')
}
}
flowRenderer.addEdges(edges, mockG)
})
it('should handle edges with styles defined', function () {
flowParser.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;')
flowParser.parser.yy.getVertices()
const edges = flowParser.parser.yy.getEdges()
const 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;')
}
}
flowRenderer.addEdges(edges, mockG)
})
it('should handle edges with interpolation defined', function () {
flowParser.parser.parse('graph TD;A---B; linkStyle 0 interpolate basis')
flowParser.parser.yy.getVertices()
const edges = flowParser.parser.yy.getEdges()
const mockG = {
setEdge: function (start, end, options) {
expect(start).toBe('A')
expect(end).toBe('B')
expect(options.arrowhead).toBe('none')
expect(options.lineInterpolate).toBe('basis')
}
}
flowRenderer.addEdges(edges, mockG)
})
it('should handle edges with text and styles defined', function () {
flowParser.parser.parse('graph TD;A---|the text|B; linkStyle 0 stroke:val1,stroke-width:val2;')
flowParser.parser.yy.getVertices()
const edges = flowParser.parser.yy.getEdges()
const 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;')
}
}
flowRenderer.addEdges(edges, mockG)
})
it('should set fill to "none" by default when handling edges', function () {
flowParser.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;')
flowParser.parser.yy.getVertices()
const edges = flowParser.parser.yy.getEdges()
const 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;')
}
}
flowRenderer.addEdges(edges, mockG)
})
it('should not set fill to none if fill is set in linkStyle', function () {
flowParser.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2,fill:blue;')
flowParser.parser.yy.getVertices()
const edges = flowParser.parser.yy.getEdges()
const 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;')
}
}
flowRenderer.addEdges(edges, mockG)
})
})
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()
})
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()
})
it('it should throw for an invalid flow definition', function () {
expect(() => mermaid.parse('graph TQ;A--x|text including URL space|B;')).toThrow()
})
it('it should not throw for a valid sequenceDiagram definition', function () {
const text = 'sequenceDiagram\n' +
'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()
})
it('it should throw for an invalid sequenceDiagram definition', function () {
const text = 'sequenceDiagram\n' +
'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()
})
it('it should not throw for a valid dot definition', function () {
const text = 'digraph\n' +
'{\n' +
' a -> b -> c -- d -> e;\n' +
' a -- e;\n' +
'}'
expect(() => mermaid.parse(text)).not.toThrow()
})
it('it should throw for an invalid dot definition', function () {
const text = 'digraph\n' +
'{\n' +
'a -:> b -> c -- d -> e;\n' +
'a -- e;\n' +
'}'
expect(() => mermaid.parse(text)).toThrow()
})
})
})