Skip to content

Commit

Permalink
test(plugin): Reinforce stanford test (#3384)
Browse files Browse the repository at this point in the history
Add test cases for timeseries and category type axis

Ref #3333
  • Loading branch information
netil authored Aug 29, 2023
1 parent d04173c commit 4d3764d
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/Plugin/stanford/Elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,17 @@ export default class Elements {
.select("line")
.transition()
.duration(duration)
.attr("x1", d => (isRotated ? yvCustom(d, "y1") : xvCustom(d, "x1")))
.attr("x1", d => {
const v = isRotated ? yvCustom(d, "y1") : xvCustom(d, "x1");

return v;
})
.attr("x2", d => (isRotated ? yvCustom(d, "y2") : xvCustom(d, "x2")))
.attr("y1", d => (isRotated ? xvCustom(d, "x1") : yvCustom(d, "y1")))
.attr("y1", d => {
const v = isRotated ? xvCustom(d, "x1") : yvCustom(d, "y1");

return v;
})
.attr("y2", d => (isRotated ? xvCustom(d, "x2") : yvCustom(d, "y2")))
.transition()
.style("opacity", null);
Expand Down
165 changes: 164 additions & 1 deletion test/plugin/stanford/stanford-elements-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("PLUGIN: STANFORD ELEMENTS", () => {
{x: 40, y: 15, content: "Hello World 2", class: "text2"},
{x: 5, y: 65, content: "Hello World 3"}
];
const args = {
let args: any = {
data: {
x: "x",
columns: [
Expand Down Expand Up @@ -86,4 +86,167 @@ describe("PLUGIN: STANFORD ELEMENTS", () => {
expect(chart.$.main.selectAll(`.${CLASS.stanfordRegions} .${CLASS.stanfordRegion} text`).size()).to.be.equal(3);
});
});

describe("timseries axis", () => {
before(() => {
args = {
data: {
x: "Datetime",
columns: [
[
"Datetime",
new Date("2023-08-25"),
new Date("2023-08-26"),
new Date("2023-08-27")
],
[
"Pressure",
1,
2.04,
2.96
],
],
type: "scatter"
},
axis: {
x: {
label: "Datetime",
tick: {
format: '%d/%m/%y'
},
type: "timeseries"
}
},
plugins: [
new Stanford({
epochs: [
34.794, 34.787, 34.791
],
scale: {
min: 34.79,
max: 35.139,
width: 10
},
lines: [
{
x1: new Date("2023-08-25"),
y1: 1,
x2: new Date("2023-08-27"),
y2: 3,
class: "line"
}
]
})
]
};
});

it("check line position", done => {
const {$el: {main}} = chart.internal;
const line = main.selectAll(".bb-stanford-line line");
const expected = [6, 543, 391, 30];

setTimeout(() => {
const pos = [
line.attr("x1"),
line.attr("x2"),
line.attr("y1"),
line.attr("y2")
].map(Number);

expect(line.size()).to.be.equal(1);
expect(pos).to.be.deep.equal(expected);

done();
}, 100);
});

it("set options: axis.rotated=true", () => {
args.axis.rotated = true;
});

it("check rotated axis line position", done => {
const {$el: {main}} = chart.internal;
const line = main.selectAll(".bb-stanford-line line");
const expected = [43, 476, 6, 421];

setTimeout(() => {
const pos = [
line.attr("x1"),
line.attr("x2"),
line.attr("y1"),
line.attr("y2")
].map(Number);

expect(line.size()).to.be.equal(1);
expect(pos).to.be.deep.equal(expected);

done();
}, 100);
});
});

describe("category axis", () => {
before(() => {
args = {
data: {
x: "x",
columns: [
["x", "a", "b", "c", "d"],
["Pressure", 1, 2.04, 2.96]
],
type: "scatter"
},
axis: {
_rotated: true,
x: {
label: "x",
type: "category"
}
},
plugins: [
new Stanford({
epochs: [
34.794, 34.787, 34.791
],
scale: {
min: 34.79,
max: 35.139,
width: 10
},
lines: [
{
x1: 0,
y1: 1,
x2: 10,
y2: 2,
class: "line"
}
]
})
]
};
});

it("", done => {
const {$el: {main}} = chart.internal;
const line = main.selectAll(".bb-stanford-line line");
const expected = [69, 1439, 391, 210];

setTimeout(() => {
const pos = [
line.attr("x1"),
line.attr("x2"),
line.attr("y1"),
line.attr("y2")
].map(Number);

expect(line.size()).to.be.equal(1);
expect(pos).to.be.deep.equal(expected);
expect(chart.categories()).to.be.deep.equal(["a", "b", "c", "d"]);

done();
}, 100);
});
});
});
2 changes: 1 addition & 1 deletion test/plugin/stanford/stanford-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,6 @@ describe("PLUGIN: STANFORD", () => {
const lines = chart.$.main.selectAll(".bb-stanford-lines line");

expect(lines.size()).to.be.equal(3);
})
});
});
});

0 comments on commit 4d3764d

Please sign in to comment.