Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lily Kuang committed May 13, 2023
1 parent 14636cd commit f978b1f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ class CategoricalColorScale extends ExtensibleFunction {
this.multiple = 0;
}

removeSharedLabelColorFromRange(
sharedColorMap: Map<string, string>,
cleanedValue: string,
) {
if (isFeatureEnabled(FeatureFlag.USE_ANALAGOUS_COLORS)) {
return;
}

const updatedRange = [...this.originColors];
// remove the color option from shared color
sharedColorMap.forEach((value: string, key: string) => {
if (key !== cleanedValue) {
updatedRange.splice(updatedRange.indexOf(value), 1);
}
});
this.range(updatedRange.length > 0 ? updatedRange : this.originColors);
}

getColor(value?: string, sliceId?: number) {
const cleanedValue = stringifyAndTrim(value);
const sharedLabelColor = getSharedLabelColor();
Expand All @@ -99,19 +117,8 @@ class CategoricalColorScale extends ExtensibleFunction {
if (!color) {
color = newColor;
// make sure we don't overwrite the origin colors
if (!isFeatureEnabled(FeatureFlag.USE_ANALAGOUS_COLORS)) {
const updatedRange = [...this.originColors];
// remove the color option from shared color
sharedColorMap.forEach((value, key) => {
if (key !== cleanedValue) {
const index = updatedRange.indexOf(value);
updatedRange.splice(index, 1);
}
});

this.range(updatedRange.length > 0 ? updatedRange : this.originColors);
color = this.scale(cleanedValue);
}
this.removeSharedLabelColorFromRange(sharedColorMap, cleanedValue);
color = this.scale(cleanedValue);
}

sharedLabelColor.addSlice(cleanedValue, color, sliceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
*/

import { ScaleOrdinal } from 'd3-scale';
import { CategoricalColorScale, FeatureFlag } from '@superset-ui/core';
import {
CategoricalColorScale,
FeatureFlag,
getSharedLabelColor,
} from '@superset-ui/core';

describe('CategoricalColorScale', () => {
it('exists', () => {
Expand Down Expand Up @@ -222,4 +226,53 @@ describe('CategoricalColorScale', () => {
expect(scale('pig')).toBe('blue');
});
});

describe('.removeSharedLabelColorFromRange(colorMap, cleanedValue)', () => {
it('range should be the same if analogous colors enabled', () => {
window.featureFlags = {
[FeatureFlag.USE_ANALAGOUS_COLORS]: true,
};
const colors = ['blue', 'green', 'red'];
const scale = new CategoricalColorScale(colors);
expect(scale.range()).toEqual(colors);

const sharedLabelColor = getSharedLabelColor();
const colorMap = sharedLabelColor.getColorMap();
sharedLabelColor.addSlice('cow', 'blue', 1);
sharedLabelColor.addSlice('goat', 'green', 1);
scale.removeSharedLabelColorFromRange(colorMap, 'pig');
expect(scale.range()).toEqual(colors);
sharedLabelColor.clear();
});

it('should remove shared color from range', () => {
window.featureFlags = {
[FeatureFlag.USE_ANALAGOUS_COLORS]: false,
};
const scale = new CategoricalColorScale(['blue', 'green', 'red']);
expect(scale.range()).toEqual(['blue', 'green', 'red']);

const sharedLabelColor = getSharedLabelColor();
const colorMap = sharedLabelColor.getColorMap();
sharedLabelColor.addSlice('cow', 'blue', 1);
scale.removeSharedLabelColorFromRange(colorMap, 'pig');
expect(scale.range()).toEqual(['green', 'red']);
scale.removeSharedLabelColorFromRange(colorMap, 'cow');
expect(scale.range()).toEqual(['blue', 'green', 'red']);
sharedLabelColor.clear();
});

it('recycles colors when all colors are in sharedLabelColor', () => {
const scale = new CategoricalColorScale(['blue', 'green', 'red']);
expect(scale.range()).toEqual(['blue', 'green', 'red']);
const sharedLabelColor = getSharedLabelColor();
const colorMap = sharedLabelColor.getColorMap();
sharedLabelColor.addSlice('cow', 'blue', 1);
sharedLabelColor.addSlice('pig', 'red', 1);
sharedLabelColor.addSlice('horse', 'green', 1);
scale.removeSharedLabelColorFromRange(colorMap, 'goat');
expect(scale.range()).toEqual(['blue', 'green', 'red']);
sharedLabelColor.clear();
});
});
});

0 comments on commit f978b1f

Please sign in to comment.