Skip to content

Commit

Permalink
test(interaction): Increase test coverage for interactions (#3360)
Browse files Browse the repository at this point in the history
- Reinforce test coverage for interaction related codes
- Remove unused ‘else’ condition block from .generateTicks()
- Remove unused ‘jsonTreemp’ module

Ref #3333
  • Loading branch information
netil committed Aug 22, 2023
1 parent b897cbb commit 6333297
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 59 deletions.
8 changes: 0 additions & 8 deletions src/ChartInternal/Axis/AxisRendererHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,6 @@ export default class AxisRendererHelper {

return r;
});
} else {
for (let i = Math.ceil(start); i < end; i++) {
ticks.push(i);
}

if (ticks.length > 0 && ticks[0] > 0) {
ticks.unshift(ticks[0] - (ticks[1] - ticks[0]));
}
}

return ticks;
Expand Down
16 changes: 1 addition & 15 deletions src/ChartInternal/data/convert.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
tsvParseRows as d3TsvParseRows,
} from "d3-dsv";

export {columns, json, jsonTreemap, rows, url, csv, tsv};
export {columns, json, rows, url, csv, tsv};

/***** Functions to be executed on Web Worker *****
* NOTE: Don't allowed to use
Expand Down Expand Up @@ -146,20 +146,6 @@ function json(json, keysParam) {
return data;
}

function jsonTreemap(json) {
const convertKey = v => {
if (v.children) {
v.children.forEach(convertKey);
}

v.name = v.id;
}

json.forEach(convertKey);

return json;
}

/***** Functions can't be executed on Web Worker *****/
/**
* Convert URL data
Expand Down
1 change: 0 additions & 1 deletion src/ChartInternal/interactions/eventrect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ export default {

svg.on("touchstart", event => {
state.event = event;

const {target} = event;

if (target && target !== eventRect.node()) {
Expand Down
5 changes: 1 addition & 4 deletions src/ChartInternal/interactions/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ export default {
const xFunc = d => cx(d) - config.point_r;
const yFunc = d => cy(d) - config.point_r;

n.attr("x", xFunc)
.attr("y", yFunc)
.attr("cx", cx) // when pattern is used, it possibly contain 'circle' also.
.attr("cy", cy);
n.attr("x", xFunc).attr("y", yFunc);
}
} else if (v === "region.list") {
n.select("rect").filter($$.isRegionOnX)
Expand Down
12 changes: 9 additions & 3 deletions src/ChartInternal/interactions/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,15 @@ export default {
if (hasAxis && !hasRadar && !isMultipleX) {
const coords = eventReceiver.coords[index];

width = coords.w;
left += coords.x;
top += coords.y;
if (coords) {
width = coords.w;
left += coords.x;
top += coords.y;
} else {
width = 0;
left = 0;
top = 0;
}
}

const x = left + (mouse ? mouse[0] : 0) + (
Expand Down
12 changes: 10 additions & 2 deletions src/config/Options/data/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,11 @@ export default {
* {name: "www.site4.com", upload: 400, download: 100, total: 500}
* ],
* keys: {
* // x: "name", // it's possible to specify 'x' when category axis
* // case 1: specify 'x' key for category axis
* x: "name", // 'name' key will be used as category x axis values
* value: ["upload", "download"]
*
* // case 2: without 'x' key for non-category axis
* value: ["upload", "download"]
* }
* }
Expand Down Expand Up @@ -857,7 +861,11 @@ export default {
* {name: "www.site4.com", upload: 400, download: 100, total: 500}
* ],
* keys: {
* // x: "name", // it's possible to specify 'x' when category axis
* // case 1: specify 'x' key for category axis
* x: "name", // 'name' key will be used as category x axis values
* value: ["upload", "download"]
*
* // case 2: without 'x' key for non-category axis
* value: ["upload", "download"]
* }
* }
Expand Down
90 changes: 72 additions & 18 deletions test/api/category-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,51 @@ import {$AXIS} from "../../src/config/classes";

describe("API category", () => {
let chart;
let args;

beforeEach(() => {
chart = util.generate(args);
});

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

before(() => {
return new Promise((resolve) => {
chart = util.generate({
data: {
x: "x",
columns: [
["x", "a", "b", "c", "d", "e"],
["data1", 30, 200, 100, 400, 150],
["data2", 5000, 2000, 1000, 4000, 1500]
]
},
axis: {
x: {
type: "category"
}
},
onrendered: resolve
});
});
args = {
data: {
x: "x",
columns: [
["x", "a", "b", "c", "d", "e"],
["data1", 30, 200, 100, 400, 150],
["data2", 5000, 2000, 1000, 4000, 1500]
]
},
axis: {
x: {
type: "category"
}
}
};

// return new Promise((resolve) => {
// chart = util.generate({
// data: {
// x: "x",
// columns: [
// ["x", "a", "b", "c", "d", "e"],
// ["data1", 30, 200, 100, 400, 150],
// ["data2", 5000, 2000, 1000, 4000, 1500]
// ]
// },
// axis: {
// x: {
// type: "category"
// }
// },
// onrendered: resolve
// });
// });
});

it("should return category names", () => {
Expand Down Expand Up @@ -66,4 +91,33 @@ describe("API category", () => {
expect(d3Select(this).text()).to.be.equal(name[i]);
});
});

it("set options: set categories by axis.x.catgories option", () => {
args = {
data: {
columns: [
["download", 30, 200, 100, 400],
["loading", 90, 100, 140, 200]
],
type: "bar"
},
axis: {
x: {
type: "category",
categories: ["www.site1.com", "www.site2.com", "www.site3.com", "www.site4.com"],
}
}
};
});

it("should return categories correctly.", () => {
const categories = chart.categories();

expect(categories).to.deep.equal(args.axis.x.categories);

// when give out of range x axis value
chart.tooltip.show({x: 2000});

expect(chart.$.tooltip.html()).to.be.empty;
});
});
106 changes: 102 additions & 4 deletions test/api/flow-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {expect} from "chai";
import sinon from "sinon";
import util from "../assets/util";
import {$AXIS, $LINE} from "../../src/config/classes";
import {$AXIS, $GRID, $LINE, $TEXT} from "../../src/config/classes";
import {window} from "../../src/module/browser";

describe("API flow", () => {
Expand Down Expand Up @@ -318,13 +318,13 @@ describe("API flow", () => {
};
});

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

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

Expand All @@ -334,5 +334,103 @@ describe("API flow", () => {
}
});
});

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

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

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

expect(tick.text()).to.be.equal("2023-08-25");

done();
}
});
});

it("set options", () => {
args = {
data: {
columns: [
["data1", 10, 12, 11]
],
type: "line",
labels: true
},
grid: {
x: {
show: true,
lines: [
{value: 2, text: "Label on 2"},
]
}
},
regions: [
{
axis: "x",
start: 0,
end: 1,
class: "region-1-4"
}
]
};
});

it("grid & regions should flow", done => {
chart.flow({
columns: [
["data1", 200]

],
duration: 300,
done() {
const {$el} = this.internal;

// grids
const xgrids = $el.grid.main.selectAll(`.${$GRID.xgrids} line`);

expect(xgrids.size()).to.be.equal(2);

// region
const regionRect = $el.region.list.select("rect").node().getBoundingClientRect();

expect(regionRect.x).to.be.below(-240);

// data label text
const text = $el.main.selectAll(`.${$TEXT.chartText} text`);

text.each(function(d, i) {
expect(+this.textContent).to.be.equal(d.value);
});

done();
}
});

});
});
});
Loading

0 comments on commit 6333297

Please sign in to comment.