This is an automated email from the ASF dual-hosted git repository.
sophieyou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to refs/heads/master by this push:
new fb1f2c4f18 fix: re-add missing code from PR #28132 (#29446)
fb1f2c4f18 is described below
commit fb1f2c4f188397470bdbfd562243cb876c95b529
Author: Joe Li <[email protected]>
AuthorDate: Tue Jul 2 16:58:55 2024 -0700
fix: re-add missing code from PR #28132 (#29446)
---
.../FormattingPopoverContent.test.tsx | 121 +++++++++++++++++++++
.../FormattingPopoverContent.tsx | 10 +-
2 files changed, 126 insertions(+), 5 deletions(-)
diff --git
a/superset-frontend/src/explore/components/controls/ConditionalFormattingControl/FormattingPopoverContent.test.tsx
b/superset-frontend/src/explore/components/controls/ConditionalFormattingControl/FormattingPopoverContent.test.tsx
new file mode 100644
index 0000000000..e99796c135
--- /dev/null
+++
b/superset-frontend/src/explore/components/controls/ConditionalFormattingControl/FormattingPopoverContent.test.tsx
@@ -0,0 +1,121 @@
+/**
+ * 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,
+ fireEvent,
+ waitFor,
+} from 'spec/helpers/testing-library';
+import { Comparator } from '@superset-ui/chart-controls';
+import { ColorSchemeEnum } from '@superset-ui/plugin-chart-table';
+import { FormattingPopoverContent } from './FormattingPopoverContent';
+
+const mockOnChange = jest.fn();
+
+const columns = [
+ { label: 'Column 1', value: 'column1' },
+ { label: 'Column 2', value: 'column2' },
+];
+
+const extraColorChoices = [
+ {
+ value: ColorSchemeEnum.Green,
+ label: 'Green for increase, red for decrease',
+ },
+ {
+ value: ColorSchemeEnum.Red,
+ label: 'Red for increase, green for decrease',
+ },
+];
+
+test('renders FormattingPopoverContent component', () => {
+ render(
+ <FormattingPopoverContent
+ onChange={mockOnChange}
+ columns={columns}
+ extraColorChoices={extraColorChoices}
+ />,
+ );
+
+ // Assert that the component renders correctly
+ expect(screen.getByLabelText('Column')).toBeInTheDocument();
+ expect(screen.getAllByLabelText('Color scheme')).toHaveLength(2);
+ expect(screen.getAllByLabelText('Operator')).toHaveLength(2);
+ expect(screen.queryByLabelText('Left value')).not.toBeInTheDocument();
+ expect(screen.queryByLabelText('Right value')).not.toBeInTheDocument();
+ expect(screen.getByText('Apply')).toBeInTheDocument();
+});
+
+test('calls onChange when Apply button is clicked', async () => {
+ render(
+ <FormattingPopoverContent
+ onChange={mockOnChange}
+ columns={columns}
+ extraColorChoices={extraColorChoices}
+ />,
+ );
+
+ // Simulate user interaction by clicking the Apply button
+ fireEvent.click(screen.getByText('Apply'));
+
+ // Assert that the onChange function is called with the correct config
+ await waitFor(() => {
+ expect(mockOnChange).toHaveBeenCalled();
+ });
+});
+
+test('renders the correct input fields based on the selected operator', async
() => {
+ render(
+ <FormattingPopoverContent
+ onChange={mockOnChange}
+ columns={columns}
+ extraColorChoices={extraColorChoices}
+ />,
+ );
+
+ // Select the 'Between' operator
+ fireEvent.change(screen.getAllByLabelText('Operator')[0], {
+ target: { value: Comparator.Between },
+ });
+ fireEvent.click(await screen.findByTitle('< x <'));
+
+ // Assert that the left and right value inputs are rendered
+ expect(await screen.findByLabelText('Left value')).toBeInTheDocument();
+ expect(await screen.findByLabelText('Right value')).toBeInTheDocument();
+});
+
+test('renders None for operator when Green for increase is selected', async ()
=> {
+ render(
+ <FormattingPopoverContent
+ onChange={mockOnChange}
+ columns={columns}
+ extraColorChoices={extraColorChoices}
+ />,
+ );
+
+ // Select the 'Green for increase' color scheme
+ fireEvent.change(screen.getAllByLabelText(/color scheme/i)[0], {
+ target: { value: ColorSchemeEnum.Green },
+ });
+
+ fireEvent.click(await screen.findByTitle(/green for increase/i));
+
+ // Assert that the operator is set to 'None'
+ expect(screen.getByText(/none/i)).toBeInTheDocument();
+});
diff --git
a/superset-frontend/src/explore/components/controls/ConditionalFormattingControl/FormattingPopoverContent.tsx
b/superset-frontend/src/explore/components/controls/ConditionalFormattingControl/FormattingPopoverContent.tsx
index b49fdbb023..79b0829dad 100644
---
a/superset-frontend/src/explore/components/controls/ConditionalFormattingControl/FormattingPopoverContent.tsx
+++
b/superset-frontend/src/explore/components/controls/ConditionalFormattingControl/FormattingPopoverContent.tsx
@@ -124,7 +124,7 @@ const shouldFormItemUpdate = (
isOperatorMultiValue(prevValues.operator) !==
isOperatorMultiValue(currentValues.operator);
-const operatorField = (showOnlyNone?: boolean) => (
+const renderOperator = ({ showOnlyNone }: { showOnlyNone?: boolean } = {}) => (
<FormItem
name="operator"
label={t('Operator')}
@@ -141,7 +141,7 @@ const operatorField = (showOnlyNone?: boolean) => (
const renderOperatorFields = ({ getFieldValue }: GetFieldValue) =>
isOperatorNone(getFieldValue('operator')) ? (
<Row gutter={12}>
- <Col span={6}>{operatorField()}</Col>
+ <Col span={6}>{renderOperator()}</Col>
</Row>
) : isOperatorMultiValue(getFieldValue('operator')) ? (
<Row gutter={12}>
@@ -157,7 +157,7 @@ const renderOperatorFields = ({ getFieldValue }:
GetFieldValue) =>
<FullWidthInputNumber />
</FormItem>
</Col>
- <Col span={6}>{operatorField}</Col>
+ <Col span={6}>{renderOperator()}</Col>
<Col span={9}>
<FormItem
name="targetValueRight"
@@ -173,7 +173,7 @@ const renderOperatorFields = ({ getFieldValue }:
GetFieldValue) =>
</Row>
) : (
<Row gutter={12}>
- <Col span={6}>{operatorField}</Col>
+ <Col span={6}>{renderOperator()}</Col>
<Col span={18}>
<FormItem
name="targetValue"
@@ -248,7 +248,7 @@ export const FormattingPopoverContent = ({
renderOperatorFields
) : (
<Row gutter={12}>
- <Col span={6}>{operatorField(true)}</Col>
+ <Col span={6}>{renderOperator({ showOnlyNone: true })}</Col>
</Row>
)}
</FormItem>