Skip to content

Commit

Permalink
Add @superset-ui/chart with extract modules and additional unit tests (
Browse files Browse the repository at this point in the history
…#23)

* initialize chart package

* add unit tests for metadata and chartprops

* fix lint

* add unit test for ChartPlugin

* update README
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 26, 2021
1 parent 537b6a5 commit ae47631
Show file tree
Hide file tree
Showing 15 changed files with 493 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ applications that leverage a Superset backend :chart_with_upwards_trend:

| Package | Version |
|--|--|
| [@superset-ui/chart](https://github.com/apache-superset/superset-ui/tree/master/packages/superset-ui-chart) | [![Version](https://img.shields.io/npm/v/@superset-ui/chart.svg?style=flat-square)](https://img.shields.io/npm/v/@superset-ui/chart.svg?style=flat-square) |
| [@superset-ui/color](https://github.com/apache-superset/superset-ui/tree/master/packages/superset-ui-color) | [![Version](https://img.shields.io/npm/v/@superset-ui/color.svg?style=flat-square)](https://img.shields.io/npm/v/@superset-ui/color.svg?style=flat-square) |
| [@superset-ui/connection](https://github.com/apache-superset/superset-ui/tree/master/packages/superset-ui-connection) | [![Version](https://img.shields.io/npm/v/@superset-ui/connection.svg?style=flat-square)](https://img.shields.io/npm/v/@superset-ui/connection.svg?style=flat-square) |
| [@superset-ui/core](https://github.com/apache-superset/superset-ui/tree/master/packages/superset-ui-core) | [![Version](https://img.shields.io/npm/v/@superset-ui/core.svg?style=flat-square)](https://img.shields.io/npm/v/@superset-ui/core.svg?style=flat-square) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## @superset-ui/chart

[![Version](https://img.shields.io/npm/v/@superset-ui/chart.svg?style=flat)](https://img.shields.io/npm/v/@superset-ui/chart.svg?style=flat)
[![David (path)](https://img.shields.io/david/apache-superset/superset-ui.svg?path=packages%2Fsuperset-ui-chart&style=flat-square)](https://david-dm.org/apache-superset/superset-ui?path=packages/superset-ui-chart)

Description

#### Example usage

```js
import { xxx } from '@superset-ui/chart';
```

#### API

`fn(args)`

- Do something

### Development

`@data-ui/build-config` is used to manage the build configuration for this package including babel
builds, jest testing, eslint, and prettier.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@superset-ui/chart",
"version": "0.0.0",
"description": "SuperChart and related modules",
"sideEffects": false,
"main": "lib/index.js",
"module": "esm/index.js",
"files": [
"esm",
"lib"
],
"repository": {
"type": "git",
"url": "git+https://github.com/apache-superset/superset-ui.git"
},
"keywords": [
"superset"
],
"author": "Superset",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/apache-superset/superset-ui/issues"
},
"homepage": "https://github.com/apache-superset/superset-ui#readme",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@superset-ui/core": "^0.3.0",
"reselect": "^4.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export { default as ChartMetadata } from './models/ChartMetadata';
export { default as ChartPlugin } from './models/ChartPlugin';
export { default as ChartProps } from './models/ChartProps';
export {
default as getChartBuildQueryRegistry,
} from './registries/ChartBuildQueryRegistrySingleton';
export { default as getChartComponentRegistry } from './registries/ChartComponentRegistrySingleton';
export { default as getChartMetadataRegistry } from './registries/ChartMetadataRegistrySingleton';
export {
default as getChartTransformPropsRegistry,
} from './registries/ChartTransformPropsRegistrySingleton';
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default class ChartMetadata {
constructor({
name,
credits = [],
description = '',
show = true,
canBeAnnotationTypes = [],
supportedAnnotationTypes = [],
thumbnail,
}) {
this.name = name;
this.credits = credits;
this.description = description;
this.show = show;
this.canBeAnnotationTypesLookup = canBeAnnotationTypes.reduce((prev, type) => {
const lookup = prev;
lookup[type] = true;

return lookup;
}, {});
this.supportedAnnotationTypes = supportedAnnotationTypes;
this.thumbnail = thumbnail;
}

canBeAnnotationType(type) {
return this.canBeAnnotationTypesLookup[type] || false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { isRequired, Plugin } from '@superset-ui/core';
import getChartMetadataRegistry from '../registries/ChartMetadataRegistrySingleton';
import getChartBuildQueryRegistry from '../registries/ChartBuildQueryRegistrySingleton';
import getChartComponentRegistry from '../registries/ChartComponentRegistrySingleton';
import getChartTransformPropsRegistry from '../registries/ChartTransformPropsRegistrySingleton';

const IDENTITY = x => x;

export default class ChartPlugin extends Plugin {
constructor({
metadata = isRequired('metadata'),

// use buildQuery for immediate value
buildQuery = IDENTITY,
// use loadBuildQuery for dynamic import (lazy-loading)
loadBuildQuery,

// use transformProps for immediate value
transformProps = IDENTITY,
// use loadTransformProps for dynamic import (lazy-loading)
loadTransformProps,

// use Chart for immediate value
Chart,
// use loadChart for dynamic import (lazy-loading)
loadChart,
} = {}) {
super();
this.metadata = metadata;
this.loadBuildQuery = loadBuildQuery || (() => buildQuery);
this.loadTransformProps = loadTransformProps || (() => transformProps);

if (loadChart) {
this.loadChart = loadChart;
} else if (Chart) {
this.loadChart = () => Chart;
} else {
throw new Error('Chart or loadChart is required');
}
}

register() {
const { key = isRequired('config.key') } = this.config;
getChartMetadataRegistry().registerValue(key, this.metadata);
getChartBuildQueryRegistry().registerLoader(key, this.loadBuildQuery);
getChartComponentRegistry().registerLoader(key, this.loadChart);
getChartTransformPropsRegistry().registerLoader(key, this.loadTransformProps);

return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { createSelector } from 'reselect';
import { convertKeysToCamelCase } from '@superset-ui/core';

export default class ChartProps {
constructor({
annotationData,
datasource,
filters,
formData,
height,
onAddFilter,
onError,
payload,
setControlValue,
setTooltip,
width,
}) {
this.width = width;
this.height = height;
this.annotationData = annotationData;
this.datasource = convertKeysToCamelCase(datasource);
this.rawDatasource = datasource;
this.filters = filters;
this.formData = convertKeysToCamelCase(formData);
this.rawFormData = formData;
this.onAddFilter = onAddFilter;
this.onError = onError;
this.payload = payload;
this.setControlValue = setControlValue;
this.setTooltip = setTooltip;
}
}

ChartProps.createSelector = function create() {
return createSelector(
input => input.annotationData,
input => input.datasource,
input => input.filters,
input => input.formData,
input => input.height,
input => input.onAddFilter,
input => input.onError,
input => input.payload,
input => input.setControlValue,
input => input.setTooltip,
input => input.width,
(
annotationData,
datasource,
filters,
formData,
height,
onAddFilter,
onError,
payload,
setControlValue,
setTooltip,
width,
) =>
new ChartProps({
annotationData,
datasource,
filters,
formData,
height,
onAddFilter,
onError,
payload,
setControlValue,
setTooltip,
width,
}),
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Registry, makeSingleton } from '@superset-ui/core';

class ChartBuildQueryRegistry extends Registry {
constructor() {
super('ChartBuildQuery');
}
}

const getInstance = makeSingleton(ChartBuildQueryRegistry);

export default getInstance;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Registry, makeSingleton } from '@superset-ui/core';

class ChartComponentRegistry extends Registry {
constructor() {
super('ChartComponent');
}
}

const getInstance = makeSingleton(ChartComponentRegistry);

export default getInstance;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Registry, makeSingleton } from '@superset-ui/core';

class ChartMetadataRegistry extends Registry {
constructor() {
super('ChartMetadata');
}
}

const getInstance = makeSingleton(ChartMetadataRegistry);

export default getInstance;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Registry, makeSingleton } from '@superset-ui/core';

class ChartTransformPropsRegistry extends Registry {
constructor() {
super('ChartTransformProps');
}
}

const getInstance = makeSingleton(ChartTransformPropsRegistry);

export default getInstance;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
ChartMetadata,
ChartPlugin,
ChartProps,
getChartBuildQueryRegistry,
getChartComponentRegistry,
getChartMetadataRegistry,
getChartTransformPropsRegistry,
} from '../src/index';

describe('index', () => {
it('exports modules', () => {
[
ChartMetadata,
ChartPlugin,
ChartProps,
getChartBuildQueryRegistry,
getChartComponentRegistry,
getChartMetadataRegistry,
getChartTransformPropsRegistry,
].forEach(x => expect(x).toBeDefined());
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import ChartMetadata from '../../src/models/ChartMetadata';

describe('ChartMetadata', () => {
it('exists', () => {
expect(ChartMetadata).toBeDefined();
});
describe('new ChartMetadata({})', () => {
it('creates new metadata instance', () => {
const metadata = new ChartMetadata({
name: 'test chart',
credits: [],
description: 'some kind of chart',
thumbnail: 'test.png',
});
expect(metadata).toBeInstanceOf(ChartMetadata);
});
});
describe('.canBeAnnotationType(type)', () => {
const metadata = new ChartMetadata({
name: 'test chart',
credits: [],
description: 'some kind of chart',
canBeAnnotationTypes: ['event'],
thumbnail: 'test.png',
});
it('returns true if can', () => {
expect(metadata.canBeAnnotationType('event')).toBeTruthy();
});
it('returns false otherwise', () => {
expect(metadata.canBeAnnotationType('invalid-type')).toBeFalsy();
});
});
});
Loading

0 comments on commit ae47631

Please sign in to comment.