EnxDev commented on code in PR #43453: URL: https://github.com/apache/superset/pull/43453#discussion_r3850806265
########## superset-frontend/src/components/Datasource/components/DatasourceEditor/tests/DatasourceEditorMetricFormatWarning.test.tsx: ########## @@ -0,0 +1,161 @@ +/** + * 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 fetchMock from 'fetch-mock'; +import { screen, userEvent, waitFor } from 'spec/helpers/testing-library'; +import { isCountExpression, isPercentD3Format } from '../DatasourceEditor'; +import { + createProps, + DATASOURCE_ENDPOINT, + setupDatasourceEditorMocks, + cleanupAsyncOperations, + fastRender, + dismissDatasourceWarning, +} from './DatasourceEditor.test.utils'; + +beforeEach(() => { + fetchMock.get(DATASOURCE_ENDPOINT, [], { name: DATASOURCE_ENDPOINT }); + setupDatasourceEditorMocks(); +}); + +afterEach(async () => { + await cleanupAsyncOperations(); + fetchMock.clearHistory().removeRoutes(); +}); + +const WARNING_TEXT = /D3 format is a percentage/i; + +test('isCountExpression matches only a bare COUNT(...) call', () => { + expect(isCountExpression('COUNT(*)')).toBe(true); + expect(isCountExpression('count( * )')).toBe(true); + expect(isCountExpression('COUNT(DISTINCT name)')).toBe(true); + expect(isCountExpression('COUNT(*) / COUNT(*)')).toBe(false); + expect(isCountExpression('COUNT(*) * 100')).toBe(false); + expect(isCountExpression('SUM(num)')).toBe(false); + expect(isCountExpression(undefined)).toBe(false); +}); + +test('isPercentD3Format accepts only a valid D3 percent spec', () => { + expect(isPercentD3Format('.0%')).toBe(true); + expect(isPercentD3Format(',.2%')).toBe(true); + expect(isPercentD3Format('foo%')).toBe(false); + expect(isPercentD3Format('.0%garbage%')).toBe(false); + expect(isPercentD3Format(',.0f')).toBe(false); + expect(isPercentD3Format(undefined)).toBe(false); +}); + +// A '%' format is valid syntax, so it never hits the "Invalid format" fallback. +test('warns when a percent D3 format is set on a COUNT metric', async () => { + const testProps = createProps(); + fastRender(testProps); + await dismissDatasourceWarning(); + + await userEvent.click(await screen.findByTestId('collection-tab-Metrics')); + const expandToggles = await screen.findAllByLabelText(/expand row/i); + // Rows sort by metric id descending, so `COUNT(*)` (id 7) is first. + await userEvent.click(expandToggles[0]); + + expect(screen.queryByText(WARNING_TEXT)).not.toBeInTheDocument(); + + await userEvent.type(await screen.findByPlaceholderText('%y/%m/%d'), '.0%'); + + expect(await screen.findByText(WARNING_TEXT)).toBeInTheDocument(); +}); + +test('does not warn for a non-percent format on a COUNT metric', async () => { + const testProps = createProps(); + fastRender(testProps); + await dismissDatasourceWarning(); + + await userEvent.click(await screen.findByTestId('collection-tab-Metrics')); + const expandToggles = await screen.findAllByLabelText(/expand row/i); + await userEvent.click(expandToggles[0]); + + await userEvent.type(await screen.findByPlaceholderText('%y/%m/%d'), ',.0f'); + + await waitFor(() => Review Comment: await waitFor(() => expect(...).not.toBeInTheDocument()) resolves on its first synchronous check — before TextControl's 250ms FAST_DEBOUNCE ever commits the typed value. So all four "does not warn" tests passed regardless of whether the warning logic was even correct (this is exactly how the p-format bug went undetected). Fixed by waiting Constants.FAST_DEBOUNCE + 50ms before asserting absence, so the assertion now actually constrains behavior. [DatasourceEditorMetricFormatWarning.test.tsx:49](vscode-webview://03msa8759m0ieil9fgknfejjnv98udeg1o5qi9olm6r74uil1bk4/superset-frontend/src/components/Datasource/components/DatasourceEditor/tests/DatasourceEditorMetricFormatWarning.test.tsx#L49) -- 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]
