Skip to content

Commit fcab713

Browse files
rusackasclaude
andcommitted
test(deck.gl): pin legend and point colors to the same slice_id key
Adds a regression test asserting that the legend path (getCategories) and the point-color path (addColorToFeatures) both key the categorical color scale on form_data's slice_id, and that legend swatches match the resolved point colors per category. Exports getCategories to make the legend path testable. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 3b96e90 commit fcab713

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { QueryFormData } from '@superset-ui/core';
20+
import { getCategories } from './CategoricalDeckGLContainer';
21+
import { addColorToFeatures } from './utils/addColor';
22+
import { COLOR_SCHEME_TYPES } from './utilities/utils';
23+
24+
// Record every (label, sliceId) pair the categorical color scale is asked to
25+
// resolve, so we can assert the legend and point-color paths key the scale on
26+
// the same slice id.
27+
const scaleCalls: [string, number | undefined][] = [];
28+
jest.mock('@superset-ui/core', () => {
29+
const actual = jest.requireActual('@superset-ui/core');
30+
return {
31+
...actual,
32+
CategoricalColorNamespace: {
33+
...actual.CategoricalColorNamespace,
34+
getScale: () => (value: string, sliceId?: number) => {
35+
scaleCalls.push([value, sliceId]);
36+
return value === 'A' ? '#ff0000' : '#00ff00';
37+
},
38+
},
39+
};
40+
});
41+
42+
test('legend and point colors resolve from the same slice_id', () => {
43+
const fd = {
44+
datasource: '1__table',
45+
viz_type: 'deck_scatter',
46+
color_scheme_type: COLOR_SCHEME_TYPES.categorical_palette,
47+
color_scheme: 'supersetColors',
48+
dimension: 'category',
49+
slice_id: 42,
50+
color_picker: { r: 0, g: 0, b: 0, a: 1 },
51+
} as unknown as QueryFormData;
52+
const data = [{ cat_color: 'A' }, { cat_color: 'B' }];
53+
54+
const categories = getCategories(fd, data);
55+
const features = addColorToFeatures(data, fd);
56+
57+
// Both the legend path (getCategories) and the point-color path
58+
// (addColorToFeatures) key the color scale on the same slice id.
59+
expect(scaleCalls.length).toBeGreaterThan(0);
60+
scaleCalls.forEach(([, sliceId]) => {
61+
expect(sliceId).toBe(42);
62+
});
63+
64+
// The legend swatch for each category matches the resolved point color.
65+
expect(categories.A.color).toEqual(features[0].color);
66+
expect(categories.B.color).toEqual(features[1].color);
67+
expect(features[0].color).not.toEqual(features[1].color);
68+
});

superset-frontend/plugins/preset-chart-deckgl/src/CategoricalDeckGLContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import { addColorToFeatures } from './utils/addColor';
5555

5656
const { getScale } = CategoricalColorNamespace;
5757

58-
function getCategories(fd: QueryFormData, data: JsonObject[]) {
58+
export function getCategories(fd: QueryFormData, data: JsonObject[]) {
5959
const c = fd.color_picker || { r: 0, g: 0, b: 0, a: 1 };
6060
const fixedColor = [c.r, c.g, c.b, 255 * c.a];
6161
const appliedScheme = fd.color_scheme;

0 commit comments

Comments
 (0)