diff --git a/src/ChartInternal/data/data.ts b/src/ChartInternal/data/data.ts index 02e2cba4a..f24c83f9c 100644 --- a/src/ChartInternal/data/data.ts +++ b/src/ChartInternal/data/data.ts @@ -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() { diff --git a/test/api/export-spec.ts b/test/api/export-spec.ts index 3fc3bef62..5f979b5c6 100644 --- a/test/api/export-spec.ts +++ b/test/api/export-spec.ts @@ -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: { @@ -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 @@ -211,9 +215,9 @@ describe("API export", () => { // pattern for CI: preserveFontStyle=true [ - "YiAZcJUAC6nB361jECvOF1DD0HdpA", - "S6iAoAFtNmParEdAFM18H03Uf1P9ktbatrndeA", - "ZczoCtPNG8bDXXwJxMJ+4mpIHPBqrpwHz9sXAb" + "4U0OAAjDiVKvfuQsCnLC2WblfcoXb1MeG0MxJUOwFwMDoI1ghV8nCgt0UG83WRxx", + "AKOiTTutIKA9XRdAgo0QBNe1wn41m04KQJg3wOB86SvuWs1/1pMACaxNoF4Bu", + "H8wWyLdFr4gZWYBnbtoAO2SzOhhNr41l9l0NbArPEZDAiSQQAJmLWYTg" ] ]; diff --git a/test/api/flow-spec.ts b/test/api/flow-spec.ts index a0dd8a34f..fd153848f 100644 --- a/test/api/flow-spec.ts +++ b/test/api/flow-spec.ts @@ -11,7 +11,7 @@ import {window} from "../../src/module/browser"; describe("API flow", () => { let chart; - let args = { + let args: any = { data: { x: "x", columns: [ @@ -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: [ @@ -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); @@ -101,7 +105,7 @@ describe("API flow", () => { }); }); - describe("basic functionality", () => { + describe("basic functionality #2", () => { before(()=> { args = { data: { @@ -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({ @@ -179,8 +187,10 @@ describe("API flow", () => { columns: [ ["data", 50, 60] ], - done: function() { + done() { expect(true).to.be.true; + this.destroy(); + done(); } }); @@ -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(); } }); diff --git a/test/api/grid-spec.ts b/test/api/grid-spec.ts index 904ea8778..3ac5a8c72 100644 --- a/test/api/grid-spec.ts +++ b/test/api/grid-spec.ts @@ -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); }); }); @@ -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); }); }); }); diff --git a/test/api/selection-spec.ts b/test/api/selection-spec.ts index 603df7a15..9223b3246 100644 --- a/test/api/selection-spec.ts +++ b/test/api/selection-spec.ts @@ -19,6 +19,9 @@ describe("API select", () => { selection: { enabled: true } + }, + transition: { + duration: 0 } }; @@ -28,6 +31,10 @@ describe("API select", () => { main = chart.$.main; }); + after(() => { + chart.destroy(); + }); + it("should select all data points", () => { chart.select(); @@ -37,48 +44,58 @@ describe("API select", () => { expect(selected.size()).to.be.equal(dataLen); }); - it("should unselect indice '1' data point", done => { + it("should unselect indice '1' data point", () => { const indice = 1; chart.unselect(["data1", "data2"], [indice]); - setTimeout(() => { - const unselected = main.selectAll(`.${$SELECT.selectedCircle}`) - .filter(`.${$SELECT.selectedCircle}-${indice}`); + const unselected = main.selectAll(`.${$SELECT.selectedCircle}`) + .filter(`.${$SELECT.selectedCircle}-${indice}`); - expect(unselected.empty()).to.be.ok; - - done(); - }, 500); + expect(unselected.empty()).to.be.ok; + }); + + it("should select some portion of data points", () => { + const indice = [1, 3]; + + chart.select("data1", indice, true); + + const selected = chart.selected(); + + main.selectAll(`.${$SELECT.selectedCircles}-data1 circle`).each((v, i) => { + expect(v).to.be.equal(selected[i]); + expect(v.index).to.be.equal(indice[i]); + }); }); - it("should unselect all data points", done => { + it("should unselect all data points", () => { chart.unselect(); - setTimeout(() => { - const unselected = main.selectAll(`.${$SELECT.selectedCircle}`); - - expect(unselected.empty()).to.be.ok; + const unselected = main.selectAll(`.${$SELECT.selectedCircle}`); - done(); - }, 500); + expect(unselected.empty()).to.be.ok; }); - it("should select some portion of data points", done => { - const indice = [1, 3]; + it("with reset option", () => { + const target = [1, 3]; - chart.select("data1", indice, true); + // when + chart.select("data1", target); - const selected = chart.selected(); + let selected = main.selectAll(`.${$SELECT.selectedCircle}`); + + expect(selected.size()).to.be.equal(2); - setTimeout(() => { - main.selectAll(`.${$SELECT.selectedCircles}-data1 circle`).each((v, i) => { - expect(v).to.be.equal(selected[i]); - expect(v.index).to.be.equal(indice[i]); - }); + selected.each(function(d, i) { + expect(d.index).to.be.equal(target[i]); + }); - done(); - }, 500); + // when select again with reset option, should be unselected + chart.select("data1", target, true); + + selected = main.selectAll(`.${$SELECT.selectedCircle}`); + + expect(selected.size()).to.be.equal(0); }); }); @@ -89,6 +106,10 @@ describe("API select", () => { main = chart.$.main; }); + after(() => { + chart.destroy(); + }); + it("should select all data points", () => { chart.select(); @@ -98,34 +119,26 @@ describe("API select", () => { expect(selected.size()).to.be.equal(dataLen); }); - it("should unselect indice '1' data point", done => { + it("should unselect indice '1' data point", () => { const indice = 1; chart.unselect(["data1", "data2"], [indice]); - setTimeout(() => { - const unselected = main.selectAll(`.${$SELECT.SELECTED}`) - .filter(`.${$BAR.bar}-${indice}`); + const unselected = main.selectAll(`.${$SELECT.SELECTED}`) + .filter(`.${$BAR.bar}-${indice}`); - expect(unselected.empty()).to.be.ok; - - done(); - }, 500); + expect(unselected.empty()).to.be.ok; }); - it("should unselect all data points", done => { + it("should unselect all data points", () => { chart.unselect(); - setTimeout(() => { - const unselected = main.selectAll(`.${$SELECT.SELECTED}`); - - expect(unselected.empty()).to.be.ok; + const unselected = main.selectAll(`.${$SELECT.SELECTED}`); - done(); - }, 500); + expect(unselected.empty()).to.be.ok; }); - it("should select some portion of data points", done => { + it("should select some portion of data points", () => { const indice = [1, 3]; const color = chart.color("data1"); @@ -133,17 +146,34 @@ describe("API select", () => { const selected = chart.selected(); - setTimeout(() => { - main.selectAll(`.${$SHAPE.shapes}-data1 path.${$SELECT.SELECTED}`).each(function(v, i) { - expect(v).to.be.equal(selected[i]); - expect(v.index).to.be.equal(indice[i]); + main.selectAll(`.${$SHAPE.shapes}-data1 path.${$SELECT.SELECTED}`).each(function(v, i) { + expect(v).to.be.equal(selected[i]); + expect(v.index).to.be.equal(indice[i]); + + // check for the selected color + expect(this.style.filter).to.be.equal("brightness(1.25)"); + }); + }); + }); - // check for the selected color - expect(this.style.filter).to.be.equal("brightness(1.25)"); - }); + describe("when selection is disabled", () => { + before(() => { + args.data.selection = false; + chart = util.generate(args); + main = chart.$.main; + }); + + after(() => { + chart.destroy(); + }); + + it("the call of .select() should not select.", () => { + // when + chart.select(); + + const selected = main.selectAll(`.${$SELECT.SELECTED}`); - done(); - }, 500); + expect(selected.size()).to.be.equal(0); }); }); }); diff --git a/test/api/tooltip-spec.ts b/test/api/tooltip-spec.ts index c4757127c..a8423d20e 100644 --- a/test/api/tooltip-spec.ts +++ b/test/api/tooltip-spec.ts @@ -312,11 +312,14 @@ describe("API tooltip", () => { ["data4", 20] ], type: "pie", + }, + transition: { + duration: 0 } }; }); - it("should show tooltip correctly", done => { + it("should show tooltip correctly using 'index'?", done => { setTimeout(() => { const {tooltip} = chart.$; @@ -330,6 +333,20 @@ describe("API tooltip", () => { }, 500); }); + it("should show tooltip correctly using 'id'?", done => { + setTimeout(() => { + const {tooltip} = chart.$; + + // when + chart.tooltip.show({data: {id: "data3"}}); + + expect(tooltip.select(".name").text()).to.be.equal("data3"); + expect(tooltip.select(".value").text()).to.be.equal("19.2%"); + + done(); + }, 500); + }); + it("set options tooltip.init", () => { args.tooltip = { init: {