Skip to content

Commit

Permalink
fix(api): prevent TypeError when chart already destroyed
Browse files Browse the repository at this point in the history
Add release of prototype chain methods.
Assign emtpy function to prevent any possible error throws.

Close #1613
  • Loading branch information
alanhamlett authored and netil committed Aug 19, 2020
1 parent 6e06629 commit ce42768
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Chart/api/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default {
window.removeEventListener("resize", $$.resizeFunction);
chart.classed("bb", false).html("");

// releasing references
// releasing own references
Object.keys(this).forEach(key => {
key === "internal" && Object.keys($$).forEach(k => {
$$[k] = null;
Expand All @@ -110,6 +110,11 @@ export default {
this[key] = null;
delete this[key];
});

// release prototype chains
for (const key in this) {
this[key] = () => {};
}
}

return null;
Expand Down
13 changes: 12 additions & 1 deletion test/api/chart-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ describe("API chart", () => {
chart.destroy();

expect(d3Select("#chart svg").empty()).to.be.true;
expect(Object.keys(chart).length).to.be.equal(0);

// all methods should be ressetted
Object.keys(chart).forEach(key => {
expect(chart[key]()).to.be.undefined;
expect(/^function()/.test(chart[key].toString())).to.be.true;
});

expect(bb.instance.indexOf(chart) === -1).to.be.true;
});

Expand All @@ -103,6 +109,11 @@ describe("API chart", () => {
setTimeout(done, 500);
}, 500);
});

it("should not throw error when already destroyed", () => {
chart.destroy();
chart.destroy();
});
});

describe("config()", () => {
Expand Down

0 comments on commit ce42768

Please sign in to comment.