Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

feat: 🎸 add .clone() function to ChartMetadata #112

Merged
merged 3 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/superset-ui-chart/src/models/ChartMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface LookupTable {

export default class ChartMetadata {
name: string;
canBeAnnotationTypes?: string[];
canBeAnnotationTypesLookup: LookupTable;
credits: string[];
description: string;
Expand Down Expand Up @@ -37,6 +38,7 @@ export default class ChartMetadata {
this.credits = credits;
this.description = description;
this.show = show;
this.canBeAnnotationTypes = canBeAnnotationTypes;
this.canBeAnnotationTypesLookup = canBeAnnotationTypes.reduce(
(prev: LookupTable, type: string) => {
const lookup = prev;
Expand All @@ -54,4 +56,17 @@ export default class ChartMetadata {
canBeAnnotationType(type: string): boolean {
return this.canBeAnnotationTypesLookup[type] || false;
}

clone() {
return new ChartMetadata({
canBeAnnotationTypes: this.canBeAnnotationTypes,
credits: this.credits,
description: this.description,
name: this.name,
show: this.show,
supportedAnnotationTypes: this.supportedAnnotationTypes,
thumbnail: this.thumbnail,
useLegacyApi: this.useLegacyApi,
});
}
}
23 changes: 22 additions & 1 deletion packages/superset-ui-chart/test/models/ChartMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ describe('ChartMetadata', () => {
describe('.canBeAnnotationType(type)', () => {
const metadata = new ChartMetadata({
name: 'test chart',
canBeAnnotationTypes: ['event'],
credits: [],
description: 'some kind of chart',
canBeAnnotationTypes: ['event'],
thumbnail: 'test.png',
});
it('returns true if can', () => {
Expand All @@ -30,4 +30,25 @@ describe('ChartMetadata', () => {
expect(metadata.canBeAnnotationType('invalid-type')).toBeFalsy();
});
});
describe('.clone()', () => {
const metadata = new ChartMetadata({
name: 'test chart',
canBeAnnotationTypes: ['event'],
credits: [],
description: 'some kind of chart',
thumbnail: 'test.png',
});
const clone = metadata.clone();

it('returns a new instance', () => {
expect(metadata).not.toBe(clone);
});
it('returns a new instance with same field values', () => {
expect(metadata.name).toEqual(clone.name);
expect(metadata.credits).toEqual(clone.credits);
expect(metadata.description).toEqual(clone.description);
expect(metadata.canBeAnnotationTypes).toEqual(clone.canBeAnnotationTypes);
expect(metadata.thumbnail).toEqual(clone.thumbnail);
});
});
});