sadpandajoe commented on code in PR #42910:
URL: https://github.com/apache/superset/pull/42910#discussion_r4373200437


##########
superset-frontend/src/explore/components/controls/IntervalColorsControl/IntervalColorsControl.test.tsx:
##########
@@ -0,0 +1,350 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import {
+  render,
+  screen,
+  userEvent,
+  waitFor,
+} from 'spec/helpers/testing-library';
+import {
+  CategoricalScheme,
+  getCategoricalSchemeRegistry,
+} from '@superset-ui/core';
+import IntervalColorsControl from 
'src/explore/components/controls/IntervalColorsControl';
+
+beforeAll(() => {
+  getCategoricalSchemeRegistry().registerValue(
+    'testScheme',
+    new CategoricalScheme({
+      id: 'testScheme',
+      colors: ['#1f77b4', '#ff7f0e', '#2ca02c'],
+    }),
+  );
+});
+
+beforeEach(() => {
+  jest.clearAllMocks();
+});
+
+test('renders a hint and no rows when no interval bounds are configured', () 
=> {
+  render(<IntervalColorsControl onChange={jest.fn()} />);
+
+  expect(
+    screen.getByText('Add interval bounds above to configure colors here.'),
+  ).toBeInTheDocument();
+  expect(document.querySelectorAll('.ant-color-picker-trigger')).toHaveLength(
+    0,
+  );
+});
+
+test('renders one color picker row per parsed interval bound', () => {
+  render(
+    <IntervalColorsControl
+      onChange={jest.fn()}
+      intervals="20,60,100"
+      colorScheme="testScheme"
+    />,
+  );
+
+  expect(screen.getByText(/Up to 20/)).toBeInTheDocument();
+  expect(screen.getByText(/Up to 60/)).toBeInTheDocument();
+  expect(screen.getByText(/Up to 100/)).toBeInTheDocument();
+  expect(document.querySelectorAll('.ant-color-picker-trigger')).toHaveLength(
+    3,
+  );
+});
+
+test('falls back to the categorical scheme when there is no explicit value or 
legacy data', () => {
+  render(
+    <IntervalColorsControl
+      onChange={jest.fn()}
+      intervals="20,60"
+      colorScheme="testScheme"
+    />,
+  );
+
+  expect(screen.getByText('#1F77B4')).toBeInTheDocument();
+  expect(screen.getByText('#FF7F0E')).toBeInTheDocument();
+});
+
+test('resolves legacy interval_color_indices against the color scheme for 
display', () => {
+  render(
+    <IntervalColorsControl
+      onChange={jest.fn()}
+      intervals="20,60"
+      legacyIntervalColorIndices="2,3"
+      colorScheme="testScheme"
+    />,
+  );
+
+  // 1-indexed: index 2 -> '#ff7f0e', index 3 -> '#2ca02c'
+  expect(screen.getByText('#FF7F0E')).toBeInTheDocument();
+  expect(screen.getByText('#2CA02C')).toBeInTheDocument();
+});
+
+test('disables editing while intervals has a blank token between numbers', 
async () => {
+  const onChange = jest.fn();
+  render(
+    <IntervalColorsControl
+      onChange={onChange}
+      intervals="20,,60"
+      colorScheme="testScheme"
+    />,
+  );
+
+  // The blank middle token means the list is still being edited; don't
+  // allow commits that would misalign colors once the blank is filled in.
+  // Pin the row count first so the per-row assertions can't pass vacuously.
+  const triggers = document.querySelectorAll('.ant-color-picker-trigger');
+  expect(triggers).toHaveLength(2);
+  triggers.forEach(trigger => {
+    expect(trigger).toHaveClass('ant-color-picker-trigger-disabled');
+  });
+
+  // A change attempt on a disabled picker must neither open it nor commit.
+  await userEvent.click(triggers[0]);
+  expect(
+    document.querySelector('.ant-color-picker-presets-color'),
+  ).not.toBeInTheDocument();
+  expect(onChange).not.toHaveBeenCalled();
+});
+
+test('explicit value takes precedence over legacy indices', () => {
+  render(
+    <IntervalColorsControl
+      onChange={jest.fn()}
+      intervals="20,60"
+      value={['#ff0000', '#00ff00']}
+      legacyIntervalColorIndices="2,3"
+      colorScheme="testScheme"
+    />,
+  );
+
+  expect(screen.getByText('#FF0000')).toBeInTheDocument();
+  expect(screen.getByText('#00FF00')).toBeInTheDocument();
+});
+
+test('a non-positive legacy index falls back to the positional scheme color 
like the renderer', () => {
+  // The renderer computes `colors[(ind - 1) % n]`, which is `undefined` for
+  // index 0, so that band uses `colors[idx]`; it must not wrap to the last
+  // scheme color.
+  render(
+    <IntervalColorsControl
+      onChange={jest.fn()}
+      intervals="20,60"
+      legacyIntervalColorIndices="0,2"
+      colorScheme="testScheme"
+    />,
+  );
+
+  expect(screen.getByText('#1F77B4')).toBeInTheDocument();
+  expect(screen.getByText('#FF7F0E')).toBeInTheDocument();
+  expect(screen.queryByText('#2CA02C')).not.toBeInTheDocument();
+});
+
+test('stays read-only when legacy indices cannot be resolved so untouched 
bands are never rewritten', async () => {

Review Comment:
   The `!hasExplicitColors` clause in `legacyUnresolved` is what keeps the 
editor writable once explicit colors exist, but nothing pins it: this test uses 
no `value`, and the explicit-color tests all use in-palette indices. If that 
clause were dropped, a chart with `interval_colors: ['#ff0000']` and legacy 
indices `10,3` would turn read-only and no test would fail. Could you add a 
case with `intervals="20,60"`, `value={['#ff0000']}`, 
`legacyIntervalColorIndices="10,3"`, then pick a preset on row 0 and assert 
`onChange` is called?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to