geido commented on code in PR #32980: URL: https://github.com/apache/superset/pull/32980#discussion_r2026845241
########## superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/CollapsibleControl.test.tsx: ########## @@ -0,0 +1,144 @@ +/** + * 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 React from 'react'; +import { render, fireEvent } from '@testing-library/react'; Review Comment: Please use 'spec/helpers/testing-library' instead ########## superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/CollapsibleControl.test.tsx: ########## @@ -0,0 +1,144 @@ +/** + * 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 React from 'react'; +import { render, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { ThemeProvider, supersetTheme } from '@superset-ui/core'; +import { CollapsibleControl } from './CollapsibleControl'; + +// Mock the InfoTooltipWithTrigger component +jest.mock('@superset-ui/chart-controls', () => ({ + InfoTooltipWithTrigger: ({ tooltip }: { tooltip: string }) => ( + <span data-test="info-tooltip" data-tooltip={tooltip} /> + ), +})); + +const defaultProps = { + title: 'Test Control', + children: <div data-test="child-content">Child Content</div>, +}; + +const renderCollapsibleControl = (props = {}) => + render( + <ThemeProvider theme={supersetTheme}> + <CollapsibleControl {...defaultProps} {...props} /> + </ThemeProvider> + ); + +describe('CollapsibleControl', () => { + it('renders title correctly', () => { + const { getByText } = renderCollapsibleControl(); + expect(getByText('Test Control')).toBeInTheDocument(); + }); + + it('renders tooltip when provided', () => { + const tooltipText = 'Test tooltip'; + const { container } = renderCollapsibleControl({ tooltip: tooltipText }); + const tooltip = container.querySelector('[data-test="info-tooltip"]'); + expect(tooltip).toBeInTheDocument(); + expect(tooltip).toHaveAttribute('data-tooltip', tooltipText); + }); + + it('starts collapsed when initialValue is false', () => { + const { container } = renderCollapsibleControl({ initialValue: false }); + const childContent = container.querySelector('[data-test="child-content"]'); + expect(childContent).not.toBeInTheDocument(); + }); + + it('starts expanded when initialValue is true', () => { + const { container } = renderCollapsibleControl({ initialValue: true }); + const childContent = container.querySelector('[data-test="child-content"]'); + expect(childContent).toBeInTheDocument(); + }); + + it('toggles content when clicked', () => { + const { container, getByText } = renderCollapsibleControl(); + const checkbox = getByText('Test Control').closest('label'); + + // Initially collapsed + expect(container.querySelector('[data-test="child-content"]')).not.toBeInTheDocument(); + + // Expand + if (checkbox) { + fireEvent.click(checkbox); Review Comment: Let's use `userEvent` from `'spec/helpers/testing-library'` ########## superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/CollapsibleControl.test.tsx: ########## @@ -0,0 +1,144 @@ +/** + * 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 React from 'react'; +import { render, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { ThemeProvider, supersetTheme } from '@superset-ui/core'; +import { CollapsibleControl } from './CollapsibleControl'; + +// Mock the InfoTooltipWithTrigger component +jest.mock('@superset-ui/chart-controls', () => ({ + InfoTooltipWithTrigger: ({ tooltip }: { tooltip: string }) => ( + <span data-test="info-tooltip" data-tooltip={tooltip} /> + ), +})); + +const defaultProps = { + title: 'Test Control', + children: <div data-test="child-content">Child Content</div>, +}; + +const renderCollapsibleControl = (props = {}) => + render( + <ThemeProvider theme={supersetTheme}> + <CollapsibleControl {...defaultProps} {...props} /> + </ThemeProvider> + ); + +describe('CollapsibleControl', () => { + it('renders title correctly', () => { + const { getByText } = renderCollapsibleControl(); + expect(getByText('Test Control')).toBeInTheDocument(); + }); + + it('renders tooltip when provided', () => { + const tooltipText = 'Test tooltip'; + const { container } = renderCollapsibleControl({ tooltip: tooltipText }); + const tooltip = container.querySelector('[data-test="info-tooltip"]'); Review Comment: Can we use `getByTestId` instead? ########## superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/CollapsibleControl.test.tsx: ########## @@ -0,0 +1,144 @@ +/** + * 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 React from 'react'; +import { render, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { ThemeProvider, supersetTheme } from '@superset-ui/core'; +import { CollapsibleControl } from './CollapsibleControl'; + +// Mock the InfoTooltipWithTrigger component +jest.mock('@superset-ui/chart-controls', () => ({ + InfoTooltipWithTrigger: ({ tooltip }: { tooltip: string }) => ( + <span data-test="info-tooltip" data-tooltip={tooltip} /> + ), +})); + +const defaultProps = { + title: 'Test Control', + children: <div data-test="child-content">Child Content</div>, +}; + +const renderCollapsibleControl = (props = {}) => + render( + <ThemeProvider theme={supersetTheme}> Review Comment: By importing `render` from 'spec/helpers/testing-library' you will have a `ThemeProvider` included and you can remove it from here. ########## superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/CollapsibleControl.test.tsx: ########## @@ -0,0 +1,144 @@ +/** + * 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 React from 'react'; +import { render, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { ThemeProvider, supersetTheme } from '@superset-ui/core'; +import { CollapsibleControl } from './CollapsibleControl'; + +// Mock the InfoTooltipWithTrigger component +jest.mock('@superset-ui/chart-controls', () => ({ + InfoTooltipWithTrigger: ({ tooltip }: { tooltip: string }) => ( + <span data-test="info-tooltip" data-tooltip={tooltip} /> + ), +})); + +const defaultProps = { + title: 'Test Control', + children: <div data-test="child-content">Child Content</div>, +}; + +const renderCollapsibleControl = (props = {}) => + render( + <ThemeProvider theme={supersetTheme}> + <CollapsibleControl {...defaultProps} {...props} /> + </ThemeProvider> + ); + +describe('CollapsibleControl', () => { + it('renders title correctly', () => { + const { getByText } = renderCollapsibleControl(); + expect(getByText('Test Control')).toBeInTheDocument(); + }); + + it('renders tooltip when provided', () => { + const tooltipText = 'Test tooltip'; + const { container } = renderCollapsibleControl({ tooltip: tooltipText }); + const tooltip = container.querySelector('[data-test="info-tooltip"]'); + expect(tooltip).toBeInTheDocument(); + expect(tooltip).toHaveAttribute('data-tooltip', tooltipText); + }); + + it('starts collapsed when initialValue is false', () => { + const { container } = renderCollapsibleControl({ initialValue: false }); + const childContent = container.querySelector('[data-test="child-content"]'); + expect(childContent).not.toBeInTheDocument(); + }); + + it('starts expanded when initialValue is true', () => { + const { container } = renderCollapsibleControl({ initialValue: true }); + const childContent = container.querySelector('[data-test="child-content"]'); + expect(childContent).toBeInTheDocument(); + }); + + it('toggles content when clicked', () => { + const { container, getByText } = renderCollapsibleControl(); + const checkbox = getByText('Test Control').closest('label'); Review Comment: Can we pick elements by the role when possible and avoid navigating through DOM structures? A few best practices are described [here](https://github.com/apache/superset/wiki/Testing-Guidelines-and-Best-Practices) ########## superset-frontend/src/components/IndeterminateCheckbox/index.tsx: ########## @@ -24,9 +24,7 @@ import { useRef, useEffect, } from 'react'; - -import { styled } from '@superset-ui/core'; -import Icons from 'src/components/Icons'; +import { Checkbox } from 'antd-v5'; Review Comment: Should this import from `src/components/Checkbox` so we only import the Ant Design Checkbox in one place? Also, would it make sense to move the whole IndeterminateCheckbox under `src/components/Checkbox/IndeterminateCheckbox.tsx`? ########## superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/CollapsibleControl.test.tsx: ########## @@ -0,0 +1,144 @@ +/** + * 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 React from 'react'; +import { render, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { ThemeProvider, supersetTheme } from '@superset-ui/core'; +import { CollapsibleControl } from './CollapsibleControl'; + +// Mock the InfoTooltipWithTrigger component +jest.mock('@superset-ui/chart-controls', () => ({ + InfoTooltipWithTrigger: ({ tooltip }: { tooltip: string }) => ( + <span data-test="info-tooltip" data-tooltip={tooltip} /> + ), +})); + +const defaultProps = { + title: 'Test Control', + children: <div data-test="child-content">Child Content</div>, +}; + +const renderCollapsibleControl = (props = {}) => + render( + <ThemeProvider theme={supersetTheme}> + <CollapsibleControl {...defaultProps} {...props} /> + </ThemeProvider> + ); + +describe('CollapsibleControl', () => { + it('renders title correctly', () => { + const { getByText } = renderCollapsibleControl(); + expect(getByText('Test Control')).toBeInTheDocument(); + }); + + it('renders tooltip when provided', () => { + const tooltipText = 'Test tooltip'; + const { container } = renderCollapsibleControl({ tooltip: tooltipText }); + const tooltip = container.querySelector('[data-test="info-tooltip"]'); + expect(tooltip).toBeInTheDocument(); + expect(tooltip).toHaveAttribute('data-tooltip', tooltipText); + }); + + it('starts collapsed when initialValue is false', () => { + const { container } = renderCollapsibleControl({ initialValue: false }); + const childContent = container.querySelector('[data-test="child-content"]'); + expect(childContent).not.toBeInTheDocument(); + }); + + it('starts expanded when initialValue is true', () => { + const { container } = renderCollapsibleControl({ initialValue: true }); + const childContent = container.querySelector('[data-test="child-content"]'); + expect(childContent).toBeInTheDocument(); + }); + + it('toggles content when clicked', () => { + const { container, getByText } = renderCollapsibleControl(); + const checkbox = getByText('Test Control').closest('label'); + + // Initially collapsed + expect(container.querySelector('[data-test="child-content"]')).not.toBeInTheDocument(); + + // Expand + if (checkbox) { + fireEvent.click(checkbox); + expect(container.querySelector('[data-test="child-content"]')).toBeInTheDocument(); + + // Collapse + fireEvent.click(checkbox); + expect(container.querySelector('[data-test="child-content"]')).not.toBeInTheDocument(); + } + }); + + it('calls onChange handler when toggled', () => { + const onChangeMock = jest.fn(); + const { getByText } = renderCollapsibleControl({ onChange: onChangeMock }); + const checkbox = getByText('Test Control').closest('label'); + + if (checkbox) { + fireEvent.click(checkbox); + expect(onChangeMock).toHaveBeenCalledWith(true); + + fireEvent.click(checkbox); + expect(onChangeMock).toHaveBeenCalledWith(false); + } + }); + + it('respects disabled prop', () => { + const onChangeMock = jest.fn(); + const { container } = renderCollapsibleControl({ + disabled: true, + onChange: onChangeMock + }); + + const checkbox = container.querySelector('input[type="checkbox"]'); + expect(checkbox).toBeDisabled(); + + if (checkbox) { + fireEvent.click(checkbox); + expect(onChangeMock).not.toHaveBeenCalled(); + } + }); + + it('updates when controlled checked prop changes', () => { + const { rerender, queryByTestId } = renderCollapsibleControl({ checked: false }); + expect(queryByTestId('child-content')).not.toBeInTheDocument(); + + rerender( + <ThemeProvider theme={supersetTheme}> + <CollapsibleControl {...defaultProps} checked={true} /> + </ThemeProvider> + ); + expect(queryByTestId('child-content')).toBeInTheDocument(); + }); + + it('maintains local state when in uncontrolled mode', () => { + const { getByText, queryByTestId } = renderCollapsibleControl({ initialValue: false }); + const checkbox = getByText('Test Control').closest('label'); + + expect(queryByTestId('child-content')).not.toBeInTheDocument(); + + if (checkbox) { + fireEvent.click(checkbox); + expect(queryByTestId('child-content')).toBeInTheDocument(); + + fireEvent.click(checkbox); + expect(queryByTestId('child-content')).not.toBeInTheDocument(); + } + }); +}); Review Comment: Can you install `pre-commit` to make sure you are making CI-ready commits? -- 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]
