Skip to content

Commit

Permalink
adds tests for size and positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
davelandry committed Jul 21, 2016
1 parent 1e707df commit f6c7eba
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions test/textBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,68 @@ import {default as textBox} from "../src/textBox.js";

test("textBox", assert => {

const data = {text: "Hello D3plus, please wrap this sentence for me."};
const testBox = textBox().data([data]).fontFamily("Verdana").fontSize(14)();
const data = {text: "Hello D3plus, please wrap this sentence for me."},
height = 200,
width = 200,
x = 100,
y = 100;

const testBox = textBox()
.data([data])
.fontFamily("Verdana")
.fontSize(14)
.height(height)
.width(width)
.x(x)
.y(y)
();

assert.equal(document.getElementsByTagName("svg").length, 1, "automatically added <svg> element to page");
assert.equal(document.getElementsByTagName("text").length, 1, "created <text> container element");
assert.equal(document.getElementsByTagName("tspan").length, 2, "created 2 <tspan> elements");

let tspans = document.getElementsByTagName("tspan");
assert.true(tspans[0].textContent === "Hello D3plus, please wrap" &&
tspans[1].textContent === "this sentence for me.", "wrapping text");
tspans[1].textContent === "this sentence for me.", "wrapped text");

const elem = document.getElementById("d3plus-textBox-0");
let bbox = elem.getBBox();
assert.true(bbox.width <= width, "fit within width");
assert.true(bbox.height <= height, "fit within height");
assert.equal(Math.round(bbox.x), x, "x positioned correctly");

const yP = 1;
let y2 = y;
assert.true(y2 - yP <= bbox.y <= y + yP, "y positioned correctly (top)");

testBox.verticalAlign("middle")(() => {

bbox = elem.getBBox();
y2 = y + height / 2 - bbox.height / 2;
assert.true(y2 - yP <= bbox.y <= y + yP, "y positioned correctly (middle)");

testBox.fontResize(true)(() => {
testBox.verticalAlign("bottom")(() => {

tspans = document.getElementsByTagName("tspan");
assert.true(tspans[0].textContent === "Hello" &&
tspans[1].textContent === "D3plus," &&
tspans[2].textContent === "please" &&
tspans[3].textContent === "wrap this" &&
tspans[4].textContent === "sentence" &&
tspans[5].textContent === "for me.", "font resizing");
bbox = elem.getBBox();
y2 = y + height - bbox.height;
assert.true(y2 - yP <= bbox.y <= y + yP, "y positioned correctly (bottom)");

assert.end();
testBox.fontResize(true).verticalAlign("top")(() => {

tspans = document.getElementsByTagName("tspan");
assert.true(tspans[0].textContent === "Hello" &&
tspans[1].textContent === "D3plus," &&
tspans[2].textContent === "please" &&
tspans[3].textContent === "wrap this" &&
tspans[4].textContent === "sentence" &&
tspans[5].textContent === "for me.", "font resizing");

assert.end();

});

});

});

});

0 comments on commit f6c7eba

Please sign in to comment.