Skip to content

Commit

Permalink
fix(api): only remove created container (#5567)
Browse files Browse the repository at this point in the history
* fix(api): only remove created container

* fix: ci
  • Loading branch information
pearmini authored Sep 20, 2023
1 parent 763d611 commit 7be3ddf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
18 changes: 18 additions & 0 deletions __tests__/unit/api/chart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,24 @@ describe('Chart', () => {
expect(chart.getGroup()).toEqual(null);
});

it('chart.destroy() should remove created node.', () => {
const chart = new Chart({});
const container = chart.getContainer();
document.body.append(container);
expect(container.parentNode).not.toBe(null);
chart.destroy();
expect(container.parentNode).toBe(null);
});

it('chart.destroy() should not remove provided node.', () => {
const container = document.createElement('div');
document.body.append(container);
const chart = new Chart({ container });
expect(container.parentNode).not.toBe(null);
chart.destroy();
expect(container.parentNode).not.toBe(null);
});

it('chart.clear() should clear group.', async () => {
const chart = new Chart({});
chart.data([
Expand Down
3 changes: 2 additions & 1 deletion __tests__/unit/lib/threed.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { threedlib } from '../../../src/lib';
import { Cartesian3D } from '../../../src/coordinate';
import { AxisZ } from '../../../src/component';
import { Point3D, Line3D } from '../../../src/mark';
import { Point3D, Line3D, Interval3D } from '../../../src/mark';

describe('threedlib', () => {
it('threedlib() should returns expected threed components.', () => {
Expand All @@ -10,6 +10,7 @@ describe('threedlib', () => {
'component.axisZ': AxisZ,
'mark.point3D': Point3D,
'mark.line3D': Line3D,
'mark.interval3D': Interval3D,
});
});
});
3 changes: 2 additions & 1 deletion src/api/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
optionsOf,
updateRoot,
createEmptyPromise,
REMOVE_FLAG,
} from './utils';
import { CompositionNode } from './composition';
import { Node } from './node';
Expand Down Expand Up @@ -155,7 +156,7 @@ export class Runtime<Spec extends G2Spec = G2Spec> extends CompositionNode {
this._unbindAutoFit();
this._reset();
destroy(options, this._context, true);
removeContainer(this._container);
if (this._container[REMOVE_FLAG]) removeContainer(this._container);
this.emit(ChartEvent.AFTER_DESTROY);
}

Expand Down
8 changes: 7 additions & 1 deletion src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ export const VIEW_KEYS = [
'title',
];

export const REMOVE_FLAG = '__remove__';

export function normalizeContainer(
container: string | HTMLElement,
): HTMLElement {
if (container === undefined) return document.createElement('div');
if (container === undefined) {
const container = document.createElement('div');
container[REMOVE_FLAG] = true;
return container;
}
if (typeof container === 'string') {
const node = document.getElementById(container);
return node;
Expand Down

0 comments on commit 7be3ddf

Please sign in to comment.