You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2024/04/18 21:27:55 UTC
(superset) branch elizabeth/fix-color-comparison-bug created (now 93a36193fd)
This is an automated email from the ASF dual-hosted git repository.
elizabeth pushed a change to branch elizabeth/fix-color-comparison-bug
in repository https://gitbox.apache.org/repos/asf/superset.git
at 93a36193fd fix render operator bug
This branch includes the following new commits:
new 93a36193fd fix render operator bug
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(superset) 01/01: fix render operator bug
Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
elizabeth pushed a commit to branch elizabeth/fix-color-comparison-bug
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 93a36193fd09d44cbe771041016bd78b828b67fa
Author: Elizabeth Thompson <es...@gmail.com>
AuthorDate: Thu Apr 18 14:27:34 2024 -0700
fix render operator bug
---
.../FormattingPopoverContent.test.tsx | 104 +++++++++++++++++++++
.../FormattingPopoverContent.tsx | 10 +-
2 files changed, 109 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..84d903d92c
--- /dev/null
+++ b/superset-frontend/src/explore/components/controls/ConditionalFormattingControl/FormattingPopoverContent.test.tsx
@@ -0,0 +1,104 @@
+import React from 'react';
+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 1f17019d8c..0a42b99c1c 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>