Skip to content

Commit

Permalink
feat: 馃幐 add .clone() function to ChartMetadata (#112)
Browse files Browse the repository at this point in the history
* feat: 馃幐 add .clone() function to ChartMetadata

* test: 馃拲 add unit tests

* fix: 馃悰 lint
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 26, 2021
1 parent e14d656 commit 9454aed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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,
});
}
}
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);
});
});
});

0 comments on commit 9454aed

Please sign in to comment.