Skip to content

Commit

Permalink
test(plugin): Add tests for plugins (#3376)
Browse files Browse the repository at this point in the history
- Remove duplicated code from stanford plugin
- Reinforce test codes for plugins

Ref #3333
  • Loading branch information
netil authored Aug 28, 2023
1 parent 22a77eb commit 72aabde
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 65 deletions.
25 changes: 1 addition & 24 deletions src/Plugin/stanford/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Plugin from "../Plugin";
import Options from "./Options";
import Elements from "./Elements";
import ColorScale from "./ColorScale";
import {compareEpochs, isEmpty, isFunction, isString, parseDate, pointInRegion} from "./util";
import {compareEpochs, isEmpty, isFunction, pointInRegion} from "./util";

/**
* Stanford diagram plugin
Expand Down Expand Up @@ -173,29 +173,6 @@ export default class Stanford extends Plugin {
});
}

xvCustom(d, xyValue): number {
const $$ = this;
const {axis, config} = $$;
let value = xyValue ? d[xyValue] : $$.getBaseValue(d);

if (axis.isTimeSeries()) {
value = parseDate.call($$, value);
} else if (axis.isCategorized() && isString(value)) {
value = config.axis_x_categories.indexOf(d.value);
}

return Math.ceil($$.scale.x(value));
}

yvCustom(d, xyValue): number {
const $$ = this;
const {scale} = $$;
const yScale = d.axis && d.axis === "y2" ? scale.y2 : scale.y;
const value = xyValue ? d[xyValue] : $$.getBaseValue(d);

return Math.ceil(yScale(value));
}

initStanfordData(): void {
const {config} = this;
const target = this.$$.data.targets[0];
Expand Down
53 changes: 28 additions & 25 deletions test/internals/text-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ describe("TEXT", () => {
chart = util.generate(args);
});

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

const checkXY = function(x, y, prefix = "c", delta: any = {x: 1, y: 1}) {
if (isNumber(delta)) {
delta = {x: delta, y: delta};
Expand Down Expand Up @@ -1238,6 +1234,9 @@ describe("TEXT", () => {
["data2", 130, 100, 140]
],
labels: true
},
transition: {
duration: 200
}
};
});
Expand All @@ -1247,30 +1246,34 @@ describe("TEXT", () => {
const pos: number[] = [];
let text;
let interval;
let cnt = 0;

chart.load({
columns: [
["data2", 44, 134, 98, 170]
],
done: function () {
setTimeout(() => {
interval && clearInterval(interval);
const currPos = +text.attr("x");

expect(Math.round(pos[0])).to.not.equal(0);
expect(pos.every(v => v === currPos)).to.be.true;

done();
}, 500);
}
});

setTimeout(() => {
interval = setInterval(() => {
text = main.select(`.${$TEXT.texts}-data2 .${$TEXT.text}-3`);
pos.push(+text.attr("x"));
}, 100);
interval = setInterval(() => {
text = main.select(`.${$TEXT.texts}-data2 .${$TEXT.text}-3`);

chart.load({
columns: [
["data2", 44, 134, 98, 170]
],
done: function () {
setTimeout(() => {
clearInterval(interval);
const currPos = +text.attr("x");

expect(Math.round(pos[0])).to.not.equal(0);
expect(pos.every(v => v === currPos)).to.be.true;
if (text.size()) {
pos.push(+text.attr("x"));
clearInterval(interval);
}
}, 80);

done();
}, 500);
}
});
}, 500);
});
});

Expand Down
132 changes: 131 additions & 1 deletion test/plugin/sparkline/sparkline-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import util from "../../assets/util";

describe("PLUGIN: SPARKLINE", () => {
let chart;
const selector = ".sparkline";
let selector = ".sparkline";
let args: any = {
size: {
width: 150,
Expand All @@ -30,6 +30,11 @@ describe("PLUGIN: SPARKLINE", () => {
tooltip: {
show: true
},
point: {
focus: {
only: true
}
},
plugins: [
new Sparkline({
selector
Expand Down Expand Up @@ -60,10 +65,22 @@ describe("PLUGIN: SPARKLINE", () => {
});

it("check for tooltip interaction", () => {
const {state: {eventReceiver}} = chart.internal;
const el = chart.plugins[0].element[0];
const {tooltip} = chart.$;
const svg = el.querySelector("svg");

const eventWidth = eventReceiver.rect.width;

// when
util.fireEvent(svg, "mouseover", {
clientX: 10,
clientY: 10
}, chart);

expect(eventReceiver !== eventReceiver.rect.width).to.be.true;


// hover 1st chart element
util.fireEvent(svg, "mousemove", {
clientX: 10,
Expand Down Expand Up @@ -105,6 +122,119 @@ describe("PLUGIN: SPARKLINE", () => {
// tooltip element shouldn't be added to the DOM
expect(chart.$.tooltip).to.be.null;
});

it("set options: axis padding", () => {
args = {
data: {
columns: [
["data1", 30, 20, 50],
["data2", 200, 130, 90],
["data3", 300, 200, 160]
],
types: {
data2: "bar",
data3: "area"
}
},
padding: {},
tooltip: {
show: true
},
point: {
focus: {
only: true
}
},
axis: {
x: {
padding: {
left: 10,
right: 20
}
},
y: {
padding: {
top: 10
}
}
},
plugins: [
new Sparkline({
selector
})
]
}
});

it("padding value should be overriden", () => {
expect(chart.config("axis.x.padding")).to.be.deep.equal({
left: 15,
right: 15,
unit: "px"
});

expect(chart.config("axis.y.padding")).to.be.deep.equal(5);
});
});

describe("check initialization", () => {
it("when wrong holder selector is given", () => {
try {
util.generate({
data: {
columns: [
["data1", 30, 20, 50],
["data2", 200, 130, 90],
["data3", 300, 200, 160]
],
types: {
data3: "area"
}
},
padding: {},
tooltip: {
show: true
},
plugins: [
new Sparkline({
selector: "#no-chart"
})
]
});
} catch(e) {
expect(e.message.indexOf("No holder elements found") >-1).to.be.true;
}
});

it("when scatter type is found", () => {
try {
util.generate({
data: {
columns: [
["data1", 30, 20, 50],
["data2", 200, 130, 90],
["data3", 300, 200, 160]
],
types: {
data2: "bar",
data3: "scatter"
}
},
padding: {},
tooltip: {
show: true
},
plugins: [
new Sparkline({
selector
})
]
});
} catch(e) {
expect(e.message.indexOf("Contains non supported chart types") >-1).to.be.true;
}
});

});

describe("point option", () => {
Expand Down
Loading

0 comments on commit 72aabde

Please sign in to comment.