Refactor testing code for parseError

This commit is contained in:
Tyler Long
2017-09-10 00:47:09 +08:00
parent ab69dacc6d
commit bb534225b3
3 changed files with 78 additions and 108 deletions

View File

@@ -9,10 +9,30 @@ import mermaid from './mermaid'
global.mermaid = mermaid global.mermaid = mermaid
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()
}
}
describe('when using mermaid and ', function () { describe('when using mermaid and ', function () {
describe('when detecting chart type ', function () { describe('when detecting chart type ', function () {
it('should not start rendering with mermaid_config.startOnLoad set to false', function () { it('should not start rendering with mermaid_config.startOnLoad set to false', function () {
global.mermaid_config = {startOnLoad: false} global.mermaid_config = { startOnLoad: false }
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>' document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
spyOn(global.mermaid, 'init') spyOn(global.mermaid, 'init')
@@ -22,7 +42,7 @@ describe('when using mermaid and ', function () {
it('should not start rendering with mermaid.startOnLoad set to false', function () { it('should not start rendering with mermaid.startOnLoad set to false', function () {
global.mermaid.startOnLoad = false global.mermaid.startOnLoad = false
global.mermaid_config = {startOnLoad: true} global.mermaid_config = { startOnLoad: true }
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>' document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
spyOn(global.mermaid, 'init') spyOn(global.mermaid, 'init')
@@ -32,7 +52,7 @@ describe('when using mermaid and ', function () {
it('should start rendering with both startOnLoad set', function () { it('should start rendering with both startOnLoad set', function () {
global.mermaid.startOnLoad = true global.mermaid.startOnLoad = true
global.mermaid_config = {startOnLoad: true} global.mermaid_config = { startOnLoad: true }
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>' document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
spyOn(global.mermaid, 'init') spyOn(global.mermaid, 'init')
mermaid.contentLoaded() mermaid.contentLoaded()
@@ -61,7 +81,7 @@ describe('when using mermaid and ', function () {
var flowRend = require('./diagrams/flowchart/flowRenderer') var flowRend = require('./diagrams/flowchart/flowRenderer')
beforeEach(function () { beforeEach(function () {
global.mermaid_config = {startOnLoad: false} global.mermaid_config = { startOnLoad: false }
flow.parser.yy = graph flow.parser.yy = graph
graph.clear() graph.clear()
}) })
@@ -200,112 +220,56 @@ describe('when using mermaid and ', function () {
describe('checking validity of input ', function () { describe('checking validity of input ', function () {
it('it should return false for an invalid definiton', function () { it('it should return false for an invalid definiton', function () {
const foo = { validateDefinition('this is not a mermaid diagram definition', false)
parseError: () => {
}
}
spyOn(foo, 'parseError')
mermaid.eventEmitter.on('parseError', (err, hash) => {
foo.parseError(err)
})
var res = mermaid.parse('this is not a mermaid diagram definition')
expect(res).toBe(false)
expect(foo.parseError).toHaveBeenCalled()
}) })
it('it should return true for a valid flow definition', function () { it('it should return true for a valid flow definition', function () {
spyOn(global.mermaid, 'parseError') validateDefinition('graph TD;A--x|text including URL space|B;', true)
var res = mermaid.parse('graph TD;A--x|text including URL space|B;')
expect(res).toBe(true)
expect(global.mermaid.parseError).not.toHaveBeenCalled()
}) })
it('it should return false for an invalid flow definition', function () { it('it should return false for an invalid flow definition', function () {
const foo = { validateDefinition('graph TQ;A--x|text including URL space|B;', false)
parseError: () => {
}
}
spyOn(foo, 'parseError')
mermaid.eventEmitter.on('parseError', (err, hash) => {
foo.parseError(err)
})
var res = mermaid.parse('graph TQ;A--x|text including URL space|B;')
expect(res).toBe(false)
expect(foo.parseError).toHaveBeenCalled()
}) })
it('it should return true for a valid sequenceDiagram definition', function () { it('it should return true for a valid sequenceDiagram definition', function () {
spyOn(global.mermaid, 'parseError')
var str = 'sequenceDiagram\n' + var str = 'sequenceDiagram\n' +
'Alice->Bob: Hello Bob, how are you?\n\n' + 'Alice->Bob: Hello Bob, how are you?\n\n' +
'%% Comment\n' + '%% Comment\n' +
'Note right of Bob: Bob thinks\n' + 'Note right of Bob: Bob thinks\n' +
'alt isWell\n\n' + 'alt isWell\n\n' +
'Bob-->Alice: I am good thanks!\n' + 'Bob-->Alice: I am good thanks!\n' +
'else isSick\n' + 'else isSick\n' +
'Bob-->Alice: Feel sick...\n' + 'Bob-->Alice: Feel sick...\n' +
'end' 'end'
var res = mermaid.parse(str) validateDefinition(str, true)
expect(res).toBe(true)
expect(global.mermaid.parseError).not.toHaveBeenCalled()
}) })
it('it should return false for an invalid sequenceDiagram definition', function () { it('it should return false for an invalid sequenceDiagram definition', function () {
const foo = {
parseError: () => {
}
}
spyOn(foo, 'parseError')
mermaid.eventEmitter.on('parseError', (err, hash) => {
foo.parseError(err)
})
var str = 'sequenceDiagram\n' + var str = 'sequenceDiagram\n' +
'Alice:->Bob: Hello Bob, how are you?\n\n' + 'Alice:->Bob: Hello Bob, how are you?\n\n' +
'%% Comment\n' + '%% Comment\n' +
'Note right of Bob: Bob thinks\n' + 'Note right of Bob: Bob thinks\n' +
'alt isWell\n\n' + 'alt isWell\n\n' +
'Bob-->Alice: I am good thanks!\n' + 'Bob-->Alice: I am good thanks!\n' +
'else isSick\n' + 'else isSick\n' +
'Bob-->Alice: Feel sick...\n' + 'Bob-->Alice: Feel sick...\n' +
'end' 'end'
var res = mermaid.parse(str) validateDefinition(str, false)
expect(res).toBe(false)
expect(foo.parseError).toHaveBeenCalled()
}) })
it('it should return true for a valid dot definition', function () { it('it should return true for a valid dot definition', function () {
spyOn(global.mermaid, 'parseError') validateDefinition('digraph\n' +
var res = mermaid.parse('digraph\n' + '{\n' +
'{\n' + ' a -> b -> c -- d -> e;\n' +
' a -> b -> c -- d -> e;\n' + ' a -- e;\n' +
' a -- e;\n' + '}', true)
'}')
expect(res).toBe(true)
expect(global.mermaid.parseError).not.toHaveBeenCalled()
}) })
it('it should return false for an invalid dot definition', function () { it('it should return false for an invalid dot definition', function () {
const foo = { validateDefinition('digraph\n' +
parseError: () => { '{\n' +
} 'a -:> b -> c -- d -> e;\n' +
} 'a -- e;\n' +
spyOn(foo, 'parseError') '}', false)
mermaid.eventEmitter.on('parseError', (err, hash) => {
foo.parseError(err)
})
var res = mermaid.parse('digraph\n' +
'{\n' +
'a -:> b -> c -- d -> e;\n' +
'a -- e;\n' +
'}')
expect(res).toBe(false)
expect(foo.parseError).toHaveBeenCalled()
}) })
}) })
}) })

View File

@@ -7,6 +7,26 @@
*/ */
var api = require('./mermaidAPI.js') var api = require('./mermaidAPI.js')
const validateDefinition = (text, valid) => {
const foo = {
onError: () => {
}
}
spyOn(foo, 'onError')
global.mermaidAPI.eventEmitter.on('parseError', (err, hash) => {
foo.onError(err)
})
var res = api.parse(text)
if (valid) {
expect(res).toBe(true)
expect(foo.onError).not.toHaveBeenCalled()
} else {
expect(res).toBe(false)
expect(foo.onError).toHaveBeenCalled()
}
}
describe('when using mermaidAPI and ', function () { describe('when using mermaidAPI and ', function () {
describe('doing initialize ', function () { describe('doing initialize ', function () {
beforeEach(function () { beforeEach(function () {
@@ -44,25 +64,10 @@ describe('when using mermaidAPI and ', function () {
}) })
describe('checking validity of input ', function () { describe('checking validity of input ', function () {
it('it should return false for an invalid definiton', function () { it('it should return false for an invalid definiton', function () {
const foo = { validateDefinition('this is not a mermaid diagram definition', false)
parseError: () => {
}
}
spyOn(foo, 'parseError')
global.mermaidAPI.eventEmitter.on('parseError', (err, hash) => {
foo.parseError(err)
})
var res = api.parse('this is not a mermaid diagram definition')
expect(res).toBe(false)
expect(foo.parseError).toHaveBeenCalled()
}) })
it('it should return true for a valid definiton', function () { it('it should return true for a valid definiton', function () {
spyOn(global.mermaidAPI, 'parseError') validateDefinition('graph TD;A--x|text including URL space|B;', true)
var res = api.parse('graph TD;A--x|text including URL space|B;')
expect(res).toBe(true)
expect(global.mermaidAPI.parseError).not.toHaveBeenCalled()
}) })
}) })
}) })

View File

@@ -18,3 +18,4 @@
- Replace karma-webpack with karma-babel - Replace karma-webpack with karma-babel
- Webpack generate mermaid.js & mermaid.core.js - Webpack generate mermaid.js & mermaid.core.js
- Stops working for mermaid-live-editor since 7.0.9 - Stops working for mermaid-live-editor since 7.0.9
- Change emit parseError to throw parseError