SequenceDiagram/scripts/interface/Interface_spec.js

92 lines
2.2 KiB
JavaScript

import Interface from './Interface.js';
import stubRequire from '../stubs/require.js';
describe('Interface', () => {
// Thanks, https://stackoverflow.com/a/23522755/1180785
const safari = (/^((?!chrome|android).)*safari/i).test(navigator.userAgent);
const defaultCode = 'my default code';
let sequenceDiagram = null;
let container = null;
let ui = null;
beforeEach(() => {
sequenceDiagram = jasmine.createSpyObj('sequenceDiagram', [
'dom',
'render',
'clone',
'getSize',
'process',
'getThemeNames',
'on',
'registerCodeMirrorMode',
'getSVGSynchronous',
]);
sequenceDiagram.process.and.returnValue({
agents: [],
meta: {},
stages: [],
});
sequenceDiagram.on.and.returnValue(sequenceDiagram);
sequenceDiagram.getSize.and.returnValue({height: 20, width: 10});
sequenceDiagram.dom.and.returnValue(document.createElement('svg'));
container = jasmine.createSpyObj('container', [
'insertBefore',
'addEventListener',
]);
ui = new Interface({
defaultCode,
require: stubRequire,
sequenceDiagram,
});
});
describe('build', () => {
it('adds elements to the given container', () => {
ui.build(container);
expect(container.insertBefore).toHaveBeenCalledWith(
jasmine.anything(),
null
);
});
it('creates a code mirror instance with the given code', (done) => {
ui.build(container);
const check = setInterval(() => {
const constructorArgs = ui.code.constructor;
if(!constructorArgs.options) {
return;
}
clearInterval(check);
expect(constructorArgs.options.value).toEqual(defaultCode);
done();
}, 50);
});
});
describe('download SVG', () => {
it('triggers a download of the current image in SVG format', () => {
sequenceDiagram.getSVGSynchronous.and.returnValue('mySVGURL');
ui.build(container);
const el = ui.downloadSVG.element;
expect(el.getAttribute('href')).toEqual('#');
if(safari) {
/*
* Safari actually starts a download if we do this, which
* doesn't seem to fit its usual security vibe
*/
return;
}
el.dispatchEvent(new Event('click'));
expect(el.getAttribute('href')).toEqual('mySVGURL');
});
});
});