Add test cases for utils.initIdGeneratior
This commit is contained in:
parent
35cd3918df
commit
c472024921
|
@ -78,7 +78,7 @@ const init = function() {
|
|||
mermaidAPI.updateSiteConfig({ gantt: mermaid.ganttConfig });
|
||||
}
|
||||
|
||||
const nextId = utils.initIdGeneratior(conf.deterministicIds, conf.deterministicIDSeed);
|
||||
const nextId = utils.initIdGeneratior(conf.deterministicIds, conf.deterministicIDSeed).next;
|
||||
|
||||
let txt;
|
||||
|
||||
|
|
19
src/utils.js
19
src/utils.js
|
@ -791,12 +791,16 @@ export const configureSvgSize = function(svgElem, height, width, useMaxWidth) {
|
|||
};
|
||||
|
||||
export const initIdGeneratior = function(deterministic, seed) {
|
||||
if (!deterministic) return () => Date.now();
|
||||
const iterator = function() {
|
||||
return this.count++;
|
||||
};
|
||||
iterator.seed = seed ? seed.length : 0;
|
||||
return iterator;
|
||||
if (!deterministic) return { next: () => Date.now() };
|
||||
class iterator {
|
||||
constructor() {
|
||||
return (this.count = seed ? seed.length : 0);
|
||||
}
|
||||
next() {
|
||||
return this.count++;
|
||||
}
|
||||
}
|
||||
return new iterator();
|
||||
};
|
||||
|
||||
export default {
|
||||
|
@ -820,5 +824,6 @@ export default {
|
|||
generateId,
|
||||
random,
|
||||
memoize,
|
||||
runFunc
|
||||
runFunc,
|
||||
initIdGeneratior
|
||||
};
|
||||
|
|
|
@ -253,3 +253,34 @@ describe('when calculating SVG size', function() {
|
|||
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)
|
||||
});
|
||||
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue