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

* feat: 🎸 add .clone() function to ChartMetadata

* test: 💍 add unit tests

* fix: 🐛 lint
This commit is contained in:
Krist Wongsuphasawat 2019-02-28 10:36:45 -08:00 committed by Yongjie Zhao
parent e14d6567a9
commit 9454aed59f
2 changed files with 37 additions and 1 deletions

View File

@ -4,6 +4,7 @@ interface LookupTable {
export default class ChartMetadata {
name: string;
canBeAnnotationTypes?: string[];
canBeAnnotationTypesLookup: LookupTable;
credits: string[];
description: string;
@ -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;
@ -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,
});
}
}

View File

@ -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', () => {
@ -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);
});
});
});