Add test cases for utils.initIdGeneratior

This commit is contained in:
Julian Amelung 2020-11-23 23:34:47 +01:00
parent 35cd3918df
commit c472024921
No known key found for this signature in database
GPG Key ID: B907D4A49E3F7345
3 changed files with 44 additions and 8 deletions

View File

@ -78,7 +78,7 @@ const init = function() {
mermaidAPI.updateSiteConfig({ gantt: mermaid.ganttConfig }); mermaidAPI.updateSiteConfig({ gantt: mermaid.ganttConfig });
} }
const nextId = utils.initIdGeneratior(conf.deterministicIds, conf.deterministicIDSeed); const nextId = utils.initIdGeneratior(conf.deterministicIds, conf.deterministicIDSeed).next;
let txt; let txt;

View File

@ -791,12 +791,16 @@ export const configureSvgSize = function(svgElem, height, width, useMaxWidth) {
}; };
export const initIdGeneratior = function(deterministic, seed) { export const initIdGeneratior = function(deterministic, seed) {
if (!deterministic) return () => Date.now(); if (!deterministic) return { next: () => Date.now() };
const iterator = function() { class iterator {
return this.count++; constructor() {
}; return (this.count = seed ? seed.length : 0);
iterator.seed = seed ? seed.length : 0; }
return iterator; next() {
return this.count++;
}
}
return new iterator();
}; };
export default { export default {
@ -820,5 +824,6 @@ export default {
generateId, generateId,
random, random,
memoize, memoize,
runFunc runFunc,
initIdGeneratior
}; };

View File

@ -253,3 +253,34 @@ describe('when calculating SVG size', function() {
expect(attrs.get('width')).toEqual(200); expect(attrs.get('width')).toEqual(200);
}); });
}); });
describe('when initializing the id generator', function () {
it('should return a random number generator based on Date', function (done) {
const idGenerator = utils.initIdGeneratior(false)
expect(typeof idGenerator.next).toEqual('function')
const lastId = idGenerator.next()
setTimeout(() => {
expect(idGenerator.next() > lastId).toBe(true)
done()
}, 5)
});
it('should return a non random number generator', function () {
const idGenerator = utils.initIdGeneratior(true)
expect(typeof idGenerator.next).toEqual('function')
const start = 0
const lastId = idGenerator.next()
expect(start).toEqual(lastId)
expect(idGenerator.next()).toEqual(lastId +1)
});
it('should return a non random number generator based on seed', function () {
const idGenerator = utils.initIdGeneratior(true, 'thisIsASeed')
expect(typeof idGenerator.next).toEqual('function')
const start = 11
const lastId = idGenerator.next()
expect(start).toEqual(lastId)
expect(idGenerator.next()).toEqual(lastId +1)
});
})