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


##########
superset-frontend/src/explore/components/controls/ColumnConfigControl/ControlForm/ControlForm.test.tsx:
##########
@@ -0,0 +1,108 @@
+/**
+ * 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,
+  within,
+  waitFor,
+} from 'spec/helpers/testing-library';
+import ControlForm, { ControlFormItem, ControlFormRow } from '.';
+
+const noDigits = (value: unknown) =>
+  /\d/.test(String(value)) ? 'Digits are not allowed' : false;
+
+const renderForm = (
+  props: {
+    value?: Record<string, string>;
+    validators?: ((value: unknown) => string | false)[];
+    debounceDelay?: number;
+  } = {},
+) => {
+  const onChange = jest.fn();
+  render(
+    <ControlForm onChange={onChange} value={props.value}>
+      <ControlFormRow>
+        <ControlFormItem
+          name="title"
+          controlType="Input"
+          label="Title"
+          description="Title description"
+          validators={props.validators}
+          debounceDelay={props.debounceDelay}
+        />
+        <ControlFormItem
+          name="note"
+          controlType="Input"
+          label="Note"
+          description="Note description"
+        />
+      </ControlFormRow>
+    </ControlForm>,
+  );
+  return { onChange };
+};
+
+test('seeds each field from the matching key of the form value', () => {
+  renderForm({ value: { title: 'Hello', note: 'World' } });
+  expect(
+    screen
+      .getAllByRole('textbox')
+      .map(input => (input as HTMLInputElement).value),
+  ).toEqual(['Hello', 'World']);
+});
+
+test('propagates an edit to the form merged with the existing value', async () 
=> {
+  const { onChange } = renderForm({ value: { note: 'World' } });
+  await userEvent.type(screen.getAllByRole('textbox')[0], 'Hi');
+
+  await waitFor(() =>
+    expect(onChange).toHaveBeenLastCalledWith({ note: 'World', title: 'Hi' }),
+  );
+});

Review Comment:
   Added a test with a stateful parent (the debounced form change is fed back 
as the value) that edits two fields in sequence after the first change flushes, 
and asserts both values persist. It fails if the form payload stops merging the 
existing value. Editing two fields inside the 250ms debounce window does drop 
the first edit: the shared debounced handler only fires the last call, built 
from the stale `value`. That is a component bug, so it isn't pinned in this 
test-only change and is tracked separately.



##########
superset-frontend/src/explore/components/controls/ConditionalFormattingControl/ConditionalFormattingControl.test.tsx:
##########
@@ -101,3 +108,210 @@ test('renders verbose column name when available', () => {
   );
   expect(screen.getByText('My Column is false')).toBeInTheDocument();
 });
+
+const numericColumns = [
+  { label: 'Column A', value: 'col_a', dataType: GenericDataType.Numeric },
+  { label: 'Column B', value: 'col_b', dataType: GenericDataType.Numeric },
+];
+
+const greaterThanA: ConditionalFormattingConfig = {
+  column: 'col_a',
+  operator: Comparator.GreaterThan,
+  targetValue: 1,
+  colorScheme: 'colorSuccess',
+};
+
+const lessThanB: ConditionalFormattingConfig = {
+  column: 'col_b',
+  operator: Comparator.LessThan,
+  targetValue: 2,
+  colorScheme: 'colorSuccess',
+};
+
+const renderCrud = (
+  props: Partial<ComponentProps<typeof ConditionalFormattingControl>> = {},
+) => {
+  const onChange = jest.fn();
+  const utils = render(
+    <ConditionalFormattingControl
+      {...defaultProps}
+      columnOptions={numericColumns}
+      onChange={onChange}
+      {...props}
+    />,
+  );
+  return { onChange, ...utils };
+};
+
+const lastChange = (onChange: jest.Mock) =>
+  onChange.mock.calls[onChange.mock.calls.length - 1][0];
+
+// The popover Selects render their options inline next to the trigger, so
+// scope the option lookup to the Select that was opened.
+const pickColumn = async (optionLabel: string) => {
+  const trigger = await screen.findByRole('combobox', {
+    name: 'Select column',
+  });
+  const container = trigger.closest('.ant-select')!
+    .parentElement as HTMLElement;
+  await userEvent.click(trigger);
+  await userEvent.click(await within(container).findByText(optionLabel));
+};
+
+test('opens the add formatter popover from the add label', async () => {
+  renderCrud();
+  expect(screen.queryByText('Add new formatter')).not.toBeInTheDocument();
+
+  await userEvent.click(screen.getByText('Add new color formatter'));
+
+  expect(await screen.findByText('Add new formatter')).toBeInTheDocument();
+  expect(screen.getByText('Apply')).toBeInTheDocument();
+});
+
+test('adds a formatter and reports it through onChange when the popover is 
applied', async () => {
+  const { onChange } = renderCrud();
+
+  await userEvent.click(screen.getByText('Add new color formatter'));
+  await pickColumn('Column B');
+  await userEvent.click(await screen.findByText('Apply'));
+
+  await waitFor(() =>
+    expect(lastChange(onChange)).toEqual([
+      expect.objectContaining({ column: 'col_b' }),
+    ]),
+  );
+  expect(screen.getByText('col_b')).toBeInTheDocument();
+  await waitFor(() =>
+    expect(screen.queryByText('Add new formatter')).not.toBeInTheDocument(),
+  );
+});
+
+test('appends a new formatter after the existing ones', async () => {
+  const { onChange } = renderCrud({ value: [greaterThanA] });
+
+  await userEvent.click(screen.getByText('Add new color formatter'));
+  await pickColumn('Column B');
+  await userEvent.click(await screen.findByText('Apply'));
+
+  await waitFor(() => expect(lastChange(onChange)).toHaveLength(2));
+  expect(lastChange(onChange)[0]).toEqual(greaterThanA);
+  expect(lastChange(onChange)[1]).toEqual(
+    expect.objectContaining({ column: 'col_b' }),
+  );
+});
+
+test('opens the edit popover prefilled from the clicked formatter', async () 
=> {
+  renderCrud({ value: [greaterThanA, lessThanB] });
+
+  await userEvent.click(screen.getByText('col_b < 2'));
+
+  expect(await screen.findByText('Edit formatter')).toBeInTheDocument();
+  expect(screen.getByLabelText('Target value')).toHaveValue('2');
+});
+
+test('replaces the edited formatter at its index and keeps the others', async 
() => {
+  const { onChange } = renderCrud({ value: [greaterThanA, lessThanB] });
+
+  await userEvent.click(screen.getByText('col_b < 2'));
+  await screen.findByText('Edit formatter');
+  const targetValue = screen.getByLabelText('Target value');
+  await userEvent.clear(targetValue);
+  await userEvent.type(targetValue, '7');
+  await userEvent.click(screen.getByText('Apply'));
+
+  await waitFor(() =>
+    expect(lastChange(onChange)).toEqual([
+      greaterThanA,
+      expect.objectContaining({
+        column: 'col_b',
+        operator: Comparator.LessThan,
+        targetValue: 7,
+      }),
+    ]),
+  );
+  expect(screen.getByText('col_b < 7')).toBeInTheDocument();
+  expect(screen.queryByText('col_b < 2')).not.toBeInTheDocument();

Review Comment:
   Added a delete-then-edit test (the remaining formatter is edited at its 
shifted index) and a test that edits two formatters one after the other and 
asserts both persist. Both fail if `onEdit` builds from stale props instead of 
component state. Truly concurrent edits can't occur, since only one popover is 
open at a time and its handler is rebuilt on every state change.



##########
superset-frontend/src/explore/components/controls/ContourControl/ContourOption.test.tsx:
##########
@@ -0,0 +1,111 @@
+/**
+ * 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 } from 'spec/helpers/testing-library';
+import ContourOption from './ContourOption';
+import { ContourOptionProps, ContourType } from './types';
+
+const color = { r: 10, g: 20, b: 30, a: 100 };
+
+const isoline: ContourType = {
+  lowerThreshold: 4,
+  color,
+  strokeWidth: 3,
+  zIndex: 10,
+};
+
+const isoband: ContourType = {
+  lowerThreshold: 6,
+  upperThreshold: 10,
+  color,
+  zIndex: 20,
+};
+
+const renderOption = (props: Partial<ContourOptionProps> = {}) => {
+  const handlers = {
+    saveContour: jest.fn(),
+    onClose: jest.fn(),
+    onShift: jest.fn(),
+  };
+  render(
+    <ContourOption contour={isoline} index={0} {...handlers} {...props} />,
+    { useDnd: true },
+  );
+  return handlers;
+};
+
+test('labels an isoline with its threshold, color and stroke width', () => {
+  renderOption({ contour: isoline });
+  expect(
+    screen.getByText(
+      'Threshold: 4, color: rgba(10, 20, 30, 1), stroke width: 3',
+    ),
+  ).toBeInTheDocument();
+});
+
+test('labels an isoband with its threshold range and color but no stroke 
width', () => {
+  renderOption({ contour: isoband });
+  expect(
+    screen.getByText('Threshold: [6, 10], color: rgba(10, 20, 30, 1)'),
+  ).toBeInTheDocument();
+  expect(screen.queryByText(/stroke width/)).not.toBeInTheDocument();
+});
+
+test('treats a contour with an upper threshold as an isoband', () => {
+  renderOption({ contour: { ...isoline, upperThreshold: 8 } });
+  expect(screen.getByText(/Threshold: \[4, 8\]/)).toBeInTheDocument();
+});
+
+test('treats a contour without an upper threshold as an isoline', () => {
+  renderOption({
+    contour: { ...isoband, upperThreshold: undefined, strokeWidth: 2 },
+  });
+  expect(screen.getByText(/stroke width: 2/)).toBeInTheDocument();
+});
+
+test('calls onClose with the option index when removed', async () => {
+  const { onClose } = renderOption({ index: 2 });
+
+  await userEvent.click(screen.getByTestId('remove-control-button'));
+
+  expect(onClose).toHaveBeenCalledWith(2);
+});
+
+test('opens the contour popover prefilled from the contour when clicked', 
async () => {
+  renderOption({ contour: isoband });
+
+  await userEvent.click(screen.getByTestId('option-label'));
+
+  expect(await screen.findByRole('tab', { name: 'Isoband' })).toHaveAttribute(
+    'aria-selected',
+    'true',
+  );
+  const inputs = screen.getAllByTestId('inline-name') as HTMLInputElement[];
+  expect(inputs.map(input => input.value)).toEqual(['6', '10']);
+});
+
+test('saves an edited contour through saveContour', async () => {

Review Comment:
   Renamed to "saves the prefilled contour values unchanged through 
saveContour", which matches what it does. Editing a field is covered by "edits 
a contour in place through its popover" in `index.test.tsx`.



-- 
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