Add data structures for chart plugin system (#6028)

* add unit tests

* add test structure

* add unit tests for Registry

* add LoaderRegistry unit test

* add unit test for makeSingleton

* add type check

* add plugin data structures

* simplify API

* add preset tests

* update test message

* fix lint

* update makeSingleton

* update Plugin, Preset and unit test

* revise Registry code

* update unit test, add remove function

* update test

* update unit test

* update plugin unit test

* add .keys(), .entries() and .entriesAsPromise()

* update test description
This commit is contained in:
Krist Wongsuphasawat
2018-10-09 11:47:16 -07:00
committed by Chris Williams
parent 395359f5ad
commit cd2c46a5ed
16 changed files with 602 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import ChartPlugin from '../../../../src/visualizations/core/models/ChartPlugin';
import ChartMetadata from '../../../../src/visualizations/core/models/ChartMetadata';
describe('ChartPlugin', () => {
const metadata = new ChartMetadata({});
it('exists', () => {
expect(ChartPlugin).to.not.equal(undefined);
});
describe('new ChartPlugin()', () => {
it('creates a new plugin', () => {
const plugin = new ChartPlugin({
metadata,
Chart() {},
});
expect(plugin).to.be.instanceof(ChartPlugin);
});
it('throws an error if metadata is not specified', () => {
expect(() => new ChartPlugin()).to.throw(Error);
});
it('throws an error if none of Chart or loadChart is specified', () => {
expect(() => new ChartPlugin({ metadata })).to.throw(Error);
});
});
describe('.register(key)', () => {
const plugin = new ChartPlugin({
metadata,
Chart() {},
});
it('throws an error if key is not provided', () => {
expect(() => plugin.register()).to.throw(Error);
expect(() => plugin.configure({ key: 'abc' }).register()).to.not.throw(Error);
});
it('returns itself', () => {
expect(plugin.configure({ key: 'abc' }).register()).to.equal(plugin);
});
});
});

View File

@@ -0,0 +1,48 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import Plugin from '../../../../src/visualizations/core/models/Plugin';
describe('Plugin', () => {
it('exists', () => {
expect(Plugin).to.not.equal(undefined);
});
describe('new Plugin()', () => {
it('creates a new plugin', () => {
const plugin = new Plugin();
expect(plugin).to.be.instanceof(Plugin);
});
});
describe('.configure(config, replace)', () => {
it('extends the default config with given config when replace is not set or false', () => {
const plugin = new Plugin();
plugin.configure({ key: 'abc', foo: 'bar' });
plugin.configure({ key: 'def' });
expect(plugin.config).to.deep.equal({ key: 'def', foo: 'bar' });
});
it('replaces the default config with given config when replace is true', () => {
const plugin = new Plugin();
plugin.configure({ key: 'abc', foo: 'bar' });
plugin.configure({ key: 'def' }, true);
expect(plugin.config).to.deep.equal({ key: 'def' });
});
it('returns the plugin itself', () => {
const plugin = new Plugin();
expect(plugin.configure({ key: 'abc' })).to.equal(plugin);
});
});
describe('.resetConfig()', () => {
it('resets config back to default', () => {
const plugin = new Plugin();
plugin.configure({ key: 'abc', foo: 'bar' });
plugin.resetConfig();
expect(plugin.config).to.deep.equal({});
});
it('returns the plugin itself', () => {
const plugin = new Plugin();
expect(plugin.resetConfig()).to.equal(plugin);
});
});
});

View File

@@ -0,0 +1,65 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import Preset from '../../../../src/visualizations/core/models/Preset';
import Plugin from '../../../../src/visualizations/core/models/Plugin';
describe('Preset', () => {
it('exists', () => {
expect(Preset).to.not.equal(undefined);
});
describe('new Preset()', () => {
it('creates new preset', () => {
const preset = new Preset();
expect(preset).to.be.instanceOf(Preset);
});
});
describe('.register()', () => {
it('register all listed presets then plugins', () => {
const values = [];
class Plugin1 extends Plugin {
register() {
values.push(1);
}
}
class Plugin2 extends Plugin {
register() {
values.push(2);
}
}
class Plugin3 extends Plugin {
register() {
values.push(3);
}
}
class Plugin4 extends Plugin {
register() {
const { key } = this.config;
values.push(key);
}
}
const preset1 = new Preset({
plugins: [new Plugin1()],
});
const preset2 = new Preset({
plugins: [new Plugin2()],
});
const preset3 = new Preset({
presets: [preset1, preset2],
plugins: [
new Plugin3(),
new Plugin4().configure({ key: 'abc' }),
],
});
preset3.register();
expect(values).to.deep.equal([1, 2, 3, 'abc']);
});
it('returns itself', () => {
const preset = new Preset();
expect(preset.register()).to.equal(preset);
});
});
});