Skip to content

Commit

Permalink
test(api): Increase test coverage for APIs (#3350)
Browse files Browse the repository at this point in the history
Enforce test coverage under 90%

Ref #3333
  • Loading branch information
netil authored and netil committed Sep 5, 2023
1 parent cd001eb commit 5d9daa4
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/ChartInternal/data/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ export default {
* @private
*/
getMaxDataCount(): number {
return Math.max(...this.data.targets.map(t => t.values.length));
return Math.max(...this.data.targets.map(t => t.values.length), 0);
},

getMaxDataCountTarget() {
Expand Down
18 changes: 11 additions & 7 deletions test/api/export-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ describe("API export", () => {
data1: "bar",
data2: "area"
},
labels: true
labels: {
format: function(v, id, i, texts) {
return v > 4000 ? `${v}\nValue` : v;
}
}
},
grid: {
x: {
Expand Down Expand Up @@ -197,9 +201,9 @@ describe("API export", () => {

// pattern for local: preserveFontStyle=true
[
"bfCqV+haAfVT1S93d3ffkA02lUl9X1fP5JpDIpp6BkcA",
"CDa8p8AsA/UvgTAruS1ItHO9uWPO4PVwK+VUV4KMAVgKULt",
"Qnb7TAAxLMEdeC8tu4K/NMDE0VshaG1lzk/jI"
"RIYJUAB6NhIiLgAtL/ub4XqgdKfecox9OPcoQB0OTv0bSIB7e3qA0Y2gY",
"AgBISAEhIAQKGoCJp0CLmrQKTTueAADAfQE8BWAp93Lr/UvPgn5IaAPIOm3z",
"KxX72Ifp34GYBSAiQB6A9ArZNWuoLwQwFr3EfDLeRCA2wB4CMCu7srkyBgBqFcq"
],

// pattern for CI
Expand All @@ -211,9 +215,9 @@ describe("API export", () => {

// pattern for CI: preserveFontStyle=true
[
"YiAZcJUAC6nB361jECvOF1DD0HdpA",
"S6iAoAFtNmParEdAFM18H03Uf1P9ktbatrndeA",
"ZczoCtPNG8bDXXwJxMJ+4mpIHPBqrpwHz9sXAb"
"4U0OAAjDiVKvfuQsCnLC2WblfcoXb1MeG0MxJUOwFwMDoI1ghV8nCgt0UG83WRxx",
"AKOiTTutIKA9XRdAgo0QBNe1wn41m04KQJg3wOB86SvuWs1/1pMACaxNoF4Bu",
"H8wWyLdFr4gZWYBnbtoAO2SzOhhNr41l9l0NbArPEZDAiSQQAJmLWYTg"
]
];

Expand Down
139 changes: 132 additions & 7 deletions test/api/flow-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {window} from "../../src/module/browser";

describe("API flow", () => {
let chart;
let args = {
let args: any = {
data: {
x: "x",
columns: [
Expand All @@ -31,11 +31,15 @@ describe("API flow", () => {
}
};

describe("basic functionality", () => {
describe("basic functionality #1", () => {
before(()=> {
chart = util.generate(args);
});

after(() => {
chart.destroy();
});

it("should flow updating the data", done => {
chart.flow({
columns: [
Expand All @@ -44,7 +48,7 @@ describe("API flow", () => {
["data2", 100, 300],
["data3", 200, 120]
],
done: function () {
done() {
const lineSize = this.internal.$el.main.selectAll(`.${$LINE.chartLines} > g`).size();

expect(lineSize).to.be.equal(this.data().length);
Expand Down Expand Up @@ -101,7 +105,7 @@ describe("API flow", () => {
});
});

describe("basic functionality", () => {
describe("basic functionality #2", () => {
before(()=> {
args = {
data: {
Expand All @@ -127,8 +131,12 @@ describe("API flow", () => {
chart = util.generate(args);
});

after(() => {
chart.destroy();
});

it("ticks should translate", done => {
const moved = [];
const moved: number[] = [];
let interval;

chart.flow({
Expand Down Expand Up @@ -179,8 +187,10 @@ describe("API flow", () => {
columns: [
["data", 50, 60]
],
done: function() {
done() {
expect(true).to.be.true;
this.destroy();

done();
}
});
Expand All @@ -207,8 +217,123 @@ describe("API flow", () => {
["x", "d", "e"],
["data", 50, 60]
],
done: function() {
done() {
expect(true).to.be.true;
this.destroy();

done();
}
});
});
});

describe("check options", () => {
beforeEach(() => {
chart = util.generate(args);
});

afterEach(() => {
chart?.destroy();
});

before(() => {
args = {
data: {
x: "x",
columns: [
["x", "2017-01-11", "2017-01-21", "2017-01-25"],
["data1", 130, 140, 130],
["data2", 150, 160, 145]
],
type: "line"
},
axis: {
x: {
type: "timeseries",
tick: {
format: "%y/%m/%d"
}
}
}
};
});

it("should flow not surpassing indicated 'to' option value.", done => {
chart.flow({
columns: [
["x", '2017-02-01', '2017-02-10'],
["data1", 500, 200]

],
to: "2017-01-11",
duration: 500,
done() {
const tick = this.internal.$el.axis.x.select(".tick");

expect(tick.text()).to.be.equal("17/01/11");
expect(tick.attr("transform")).to.be.equal("translate(6,0)");

done();
}
})
});

it("set options", () => {
args = {
data: {
x: "x",
columns: [],
type: "line"
},
axis: {
x: {
type: "timeseries",
tick: {
format: "%Y-%m-%d"
}
}
}
};
});

it("when flows from timeseries x axis empty data", done => {
chart.flow({
columns: [
["x", '2017-02-01', '2017-02-10'],
["data1", 100, 200]

],
duration: 500,
done() {
const tick = this.internal.$el.axis.x.select(".tick");

expect(tick.text()).to.be.equal("2017-02-01");
done();
}
});
});

it("set options", () => {
args = {
data: {
columns: [],
type: "line"
}
};
});

it("when flows from indexed x axis empty data", done => {
chart.flow({
columns: [
["data1", 100, 200]

],
duration: 700,
done() {
const tick = this.internal.$el.axis.x.select(".tick");

expect(tick.text()).to.be.equal("0");

done();
}
});
Expand Down
78 changes: 59 additions & 19 deletions test/api/grid-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,70 @@ describe("API grid", function() {
y: {
lines: [{value: 1, class: "test"}]
}
},
transition: {
duration: 0
}
});
});

it("should update y grids", done => {
after(() => {
chart.destroy();
});

it("should update x grids", () => {
const gridData = {
value: 2, text: "grid text", position: "middle", class:"some-class"
};

chart.xgrids([gridData]);

setTimeout(() => {
const xgrid = chart.$.main.select(`.${$GRID.xgridLine}`);
const xgrid = chart.$.main.select(`.${$GRID.xgridLine}`);

expect(xgrid.classed(gridData.class)).to.be.true;
expect(xgrid.classed(gridData.class)).to.be.true;

const text = xgrid.select("text");
const text = xgrid.select("text");

expect(text.text()).to.be.equal(gridData.text);
expect(text.attr("text-anchor")).to.be.equal(gridData.position);
expect(text.text()).to.be.equal(gridData.text);
expect(text.attr("text-anchor")).to.be.equal(gridData.position);

done();
}, 500);
chart.xgrids.remove();
});

it("using .xgrids.add()", () => {
const {$el: {gridLines}, scale: {x}} = chart.internal;
const gridData = [
{value: 2, text: "Label 2"},
{value: 3, text: "Label 3"},
{value: 4, text: "Label 4"}
];

chart.xgrids.add(
gridData[0]
);

expect(gridLines.x.size()).to.be.equal(1);

gridLines.x.each(function(d) {
const x1 = +this.querySelector("line").getAttribute("x1");

expect(x1).to.be.equal(x(d.value));
expect(this.querySelector("text").textContent).to.be.equal(d.text);
});

// when adding some duplicated xgrids
chart.xgrids.add(gridData.slice(1));

expect(gridLines.x.size()).to.be.equal(3);

gridLines.x.each(function(d) {
const x1 = +this.querySelector("line").getAttribute("x1");

expect(x1).to.be.equal(x(d.value));
expect(this.querySelector("text").textContent).to.be.equal(d.text);
});

expect(chart.xgrids()).to.be.deep.equal(gridData);
});
});

Expand Down Expand Up @@ -209,29 +250,28 @@ describe("API grid", function() {
y: {
lines: [{value: 150, class: "test"}]
}
},
transition: {
duration: 0
}
});
});

it("should update y grids", done => {
it("should update y grids", () => {
const gridData = {
value: 250, text: "grid text", position: "start", class:"some-class"
};

chart.ygrids([gridData]);

setTimeout(() => {
const ygrid = chart.$.main.select(`.${$GRID.ygridLine}`);
const ygrid = chart.$.main.select(`.${$GRID.ygridLine}`);

expect(ygrid.classed(gridData.class)).to.be.true;
expect(ygrid.classed(gridData.class)).to.be.true;

const text = ygrid.select("text");
const text = ygrid.select("text");

expect(text.text()).to.be.equal(gridData.text);
expect(text.attr("text-anchor")).to.be.equal(gridData.position);

done();
}, 500);
expect(text.text()).to.be.equal(gridData.text);
expect(text.attr("text-anchor")).to.be.equal(gridData.position);
});
});
});
Loading

0 comments on commit 5d9daa4

Please sign in to comment.