forked from mermaid-js/mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmermaidAPI.spec.js
124 lines (114 loc) · 4.29 KB
/
mermaidAPI.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
import mermaidAPI from './mermaidAPI';
import { assignWithDepth } from './utils';
describe('when using mermaidAPI and ', function () {
describe('doing initialize ', function () {
beforeEach(function () {
document.body.innerHTML = '';
mermaidAPI.globalReset();
});
it('should copy a literal into the configuration', function () {
const orgConfig = mermaidAPI.getConfig();
expect(orgConfig.testLiteral).toBe(undefined);
mermaidAPI.initialize({ testLiteral: true });
const config = mermaidAPI.getConfig();
expect(config.testLiteral).toBe(true);
});
it('should copy a an object into the configuration', function () {
const orgConfig = mermaidAPI.getConfig();
expect(orgConfig.testObject).toBe(undefined);
const object = {
test1: 1,
test2: false,
};
mermaidAPI.initialize({ testObject: object });
let config = mermaidAPI.getConfig();
expect(config.testObject.test1).toBe(1);
mermaidAPI.updateSiteConfig({ testObject: { test3: true } });
config = mermaidAPI.getConfig();
expect(config.testObject.test1).toBe(1);
expect(config.testObject.test2).toBe(false);
expect(config.testObject.test3).toBe(true);
});
it('should reset mermaid config to global defaults', function () {
let config = {
logLevel: 0,
securityLevel: 'loose',
};
mermaidAPI.initialize(config);
mermaidAPI.setConfig({ securityLevel: 'strict', logLevel: 1 });
expect(mermaidAPI.getConfig().logLevel).toBe(1);
expect(mermaidAPI.getConfig().securityLevel).toBe('strict');
mermaidAPI.globalReset();
expect(mermaidAPI.getConfig().logLevel).toBe(0);
expect(mermaidAPI.getConfig().securityLevel).toBe('loose');
});
it('should prevent changes to site defaults (sneaky)', function () {
let config = {
logLevel: 0,
};
mermaidAPI.initialize(config);
const siteConfig = mermaidAPI.getSiteConfig();
expect(mermaidAPI.getConfig().logLevel).toBe(0);
config.secure = {
toString: function () {
mermaidAPI.initialize({ securityLevel: 'loose' });
},
};
mermaidAPI.reinitialize(config);
expect(mermaidAPI.getConfig().secure).toEqual(mermaidAPI.getSiteConfig().secure);
expect(mermaidAPI.getConfig().securityLevel).toBe('strict');
mermaidAPI.reset();
expect(mermaidAPI.getSiteConfig()).toEqual(siteConfig);
expect(mermaidAPI.getConfig()).toEqual(siteConfig);
});
it('should prevent clobbering global defaults (direct)', function () {
let config = assignWithDepth({}, mermaidAPI.defaultConfig);
assignWithDepth(config, { logLevel: 0 });
let error = { message: '' };
try {
mermaidAPI['defaultConfig'] = config;
} catch (e) {
error = e;
}
expect(error.message).toBe(
"Cannot assign to read only property 'defaultConfig' of object '#<Object>'"
);
expect(mermaidAPI.defaultConfig['logLevel']).toBe(5);
});
it('should prevent changes to global defaults (direct)', function () {
let error = { message: '' };
try {
mermaidAPI.defaultConfig['logLevel'] = 0;
} catch (e) {
error = e;
}
expect(error.message).toBe(
"Cannot assign to read only property 'logLevel' of object '#<Object>'"
);
expect(mermaidAPI.defaultConfig['logLevel']).toBe(5);
});
it('should prevent sneaky changes to global defaults (assignWithDepth)', function () {
let config = {
logLevel: 0,
};
let error = { message: '' };
try {
assignWithDepth(mermaidAPI.defaultConfig, config);
} catch (e) {
error = e;
}
expect(error.message).toBe(
"Cannot assign to read only property 'logLevel' of object '#<Object>'"
);
expect(mermaidAPI.defaultConfig['logLevel']).toBe(5);
});
});
describe('checking validity of input ', function () {
it('it should throw for an invalid definiton', function () {
expect(() => mermaidAPI.parse('this is not a mermaid diagram definition')).toThrow();
});
it('it should not throw for a valid definiton', function () {
expect(() => mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).not.toThrow();
});
});
});