-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add @superset-ui/chart with extract modules and additional unit tests (…
…#23) * initialize chart package * add unit tests for metadata and chartprops * fix lint * add unit test for ChartPlugin * update README
- Loading branch information
1 parent
f58b2a9
commit 287b4b8
Showing
15 changed files
with
493 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...frontend/temporary_superset_ui/superset-ui/packages/superset-ui-chart/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
32 changes: 32 additions & 0 deletions
32
superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-chart/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-chart/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
28 changes: 28 additions & 0 deletions
28
.../temporary_superset_ui/superset-ui/packages/superset-ui-chart/src/models/ChartMetadata.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...nd/temporary_superset_ui/superset-ui/packages/superset-ui-chart/src/models/ChartPlugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...end/temporary_superset_ui/superset-ui/packages/superset-ui-chart/src/models/ChartProps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}), | ||
); | ||
}; |
11 changes: 11 additions & 0 deletions
11
...superset-ui/packages/superset-ui-chart/src/registries/ChartBuildQueryRegistrySingleton.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
11 changes: 11 additions & 0 deletions
11
.../superset-ui/packages/superset-ui-chart/src/registries/ChartComponentRegistrySingleton.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
11 changes: 11 additions & 0 deletions
11
...i/superset-ui/packages/superset-ui-chart/src/registries/ChartMetadataRegistrySingleton.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
11 changes: 11 additions & 0 deletions
11
...rset-ui/packages/superset-ui-chart/src/registries/ChartTransformPropsRegistrySingleton.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
23 changes: 23 additions & 0 deletions
23
...-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-chart/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
...rary_superset_ui/superset-ui/packages/superset-ui-chart/test/models/ChartMetadata.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.