Skip to content

Commit

Permalink
fix karma tests where text.textwrap is undefined; added textPlacement…
Browse files Browse the repository at this point in the history
… parameters to some test cases
  • Loading branch information
whyzdev committed Dec 8, 2016
1 parent db62447 commit 1424ff7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
19 changes: 15 additions & 4 deletions src/diagrams/sequenceDiagram/sequenceDiagram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ var sd = proxyquire('./sequenceRenderer', { '../../d3': d3 });
//
//var sd = require('./sequenceRenderer');

function addConf(conf, key, value) {
if (value !== undefined) {
conf[key]=value;
}
return conf;
}

var str;
describe('when parsing a sequenceDiagram',function() {
beforeEach(function () {
Expand Down Expand Up @@ -761,7 +768,9 @@ describe('when rendering a sequenceDiagram',function() {
//console.log(document.querySelector('#tst').getBBox());

});
it('it should handle one actor', function () {
['tspan','fo','old',undefined].forEach(function(textPlacement) {
it('it should handle one actor, when textPlacement is '+textPlacement, function () {
sd.setConf(addConf(conf, 'textPlacement', textPlacement));
sd.bounds.init();
var str = 'sequenceDiagram\n' +
'participant Alice';
Expand All @@ -774,7 +783,7 @@ describe('when rendering a sequenceDiagram',function() {
expect(bounds.starty).toBe(0);
expect(bounds.stopx ).toBe( conf.width);
expect(bounds.stopy ).toBe(conf.height);

});
});
it('it should handle one actor and a centered note', function () {
sd.bounds.init();
Expand Down Expand Up @@ -987,7 +996,9 @@ describe('when rendering a sequenceDiagram with actor mirror activated',function
};
sd.setConf(conf);
});
it('it should handle one actor', function () {
['tspan','fo','old',undefined].forEach(function(textPlacement) {
it('it should handle one actor, when textPlacement is'+textPlacement, function () {
sd.setConf(addConf(conf, 'textPlacement', textPlacement));
sd.bounds.init();
var str = 'sequenceDiagram\n' +
'participant Alice';
Expand All @@ -1000,6 +1011,6 @@ describe('when rendering a sequenceDiagram with actor mirror activated',function
expect(bounds.starty).toBe(0);
expect(bounds.stopx ).toBe( conf.width);
expect(bounds.stopy ).toBe(2*conf.height+2*conf.boxMargin);

});
});
});
2 changes: 1 addition & 1 deletion src/diagrams/sequenceDiagram/sequenceRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var conf = {
activationWidth:10,

//text placement as: tspan | fo | <else> only text as before
textPlacement: 'tspan',
textPlacement: 'fo',
};

exports.bounds = {
Expand Down
32 changes: 15 additions & 17 deletions src/diagrams/sequenceDiagram/svgDraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,25 +281,23 @@ var _drawTextCandidateFunc = (function() {
.attr('x', x + width / 2).attr('y', y)
.style('text-anchor', 'middle');
var tspan = text.append('tspan')
.attr('x', x + width / 2).attr('dy', '0') //.attr('y', y + height / 2)
.attr('x', x + width / 2).attr('dy', '0')
.text(content);

text.textwrap({ //d3textwrap
x: x + width / 2,
y: y,
width: width,
height: height
}, 0);

//vertical aligment after d3textwrap expans tspan to multiple tspans
var tspans = text.selectAll('tspan');
if (tspans.length > 0 && tspans[0].length > 0) {
tspans = tspans[0];
//set y of <text> to the mid y of the first line
text.attr('y', y + (height/2.- text[0][0].getBBox().height*(1 - 1.0/tspans.length)/2.))
.attr("dominant-baseline", "central")
.attr("alignment-baseline", "central")
}
if(typeof(text.textwrap) !== 'undefined'){
text.textwrap({ //d3textwrap
x: x + width / 2, y: y, width: width, height: height
}, 0);
//vertical aligment after d3textwrap expans tspan to multiple tspans
var tspans = text.selectAll('tspan');
if (tspans.length > 0 && tspans[0].length > 0) {
tspans = tspans[0];
//set y of <text> to the mid y of the first line
text.attr('y', y + (height/2.- text[0][0].getBBox().height*(1 - 1.0/tspans.length)/2.))
.attr("dominant-baseline", "central")
.attr("alignment-baseline", "central")
}
}

for (var key in textAttrs) {
text.attr(key, textAttrs[key]);
Expand Down

0 comments on commit 1424ff7

Please sign in to comment.