From efeac2595220567033afca8c5de2fd9d8f60b849 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Tue, 2 Dec 2025 16:51:42 +0000 Subject: [PATCH] fix some unit tests --- .../components/MetricsLabelsSection.test.tsx | 4 +- .../grafana-ui/src/components/Card/Card.tsx | 2 +- .../src/components/Combobox/Combobox.test.tsx | 8 +-- .../grafana-ui/src/components/Forms/Field.tsx | 2 +- .../SendResetMailPage.test.tsx | 6 +- .../rule-viewer/RuleViewer.test.tsx | 4 +- .../StandardAnnotationQueryEditor.test.tsx | 12 ++-- .../sharing/ShareDrawer/ShareDrawer.test.tsx | 2 +- .../containers/DashboardPageProxy.test.tsx | 56 +++++++------------ .../PublicDashboardPageProxy.test.tsx | 5 +- .../picker/DataSourceModal.test.tsx | 2 +- .../components/common/SearchBarInput.tsx | 2 +- .../features/explore/spec/helper/setup.tsx | 4 +- .../plugins/extensions/utils.test.tsx | 4 -- 14 files changed, 46 insertions(+), 67 deletions(-) diff --git a/packages/grafana-prometheus/src/querybuilder/components/MetricsLabelsSection.test.tsx b/packages/grafana-prometheus/src/querybuilder/components/MetricsLabelsSection.test.tsx index b351e4670b1..ffc8d855ecb 100644 --- a/packages/grafana-prometheus/src/querybuilder/components/MetricsLabelsSection.test.tsx +++ b/packages/grafana-prometheus/src/querybuilder/components/MetricsLabelsSection.test.tsx @@ -99,7 +99,7 @@ describe('MetricsLabelsSection', () => { onBlur: onBlur, variableEditor: undefined, }), - expect.anything() + undefined ); }); @@ -124,7 +124,7 @@ describe('MetricsLabelsSection', () => { labelsFilters: defaultQuery.labels, variableEditor: undefined, }), - expect.anything() + undefined ); }); diff --git a/packages/grafana-ui/src/components/Card/Card.tsx b/packages/grafana-ui/src/components/Card/Card.tsx index a89001213a0..cf61b8e7bae 100644 --- a/packages/grafana-ui/src/components/Card/Card.tsx +++ b/packages/grafana-ui/src/components/Card/Card.tsx @@ -317,7 +317,7 @@ const BaseActions = ({ children, disabled, variant, className }: ActionsProps) =
{React.Children.map(children, (child) => { return React.isValidElement>(child) - ? cloneElement(child, { disabled: isDisabled, ...child.props }) + ? cloneElement(child, child.type !== React.Fragment ? { disabled: isDisabled, ...child.props } : undefined) : null; })}
diff --git a/packages/grafana-ui/src/components/Combobox/Combobox.test.tsx b/packages/grafana-ui/src/components/Combobox/Combobox.test.tsx index b99e58a447b..1b0305f32a1 100644 --- a/packages/grafana-ui/src/components/Combobox/Combobox.test.tsx +++ b/packages/grafana-ui/src/components/Combobox/Combobox.test.tsx @@ -530,10 +530,8 @@ describe('Combobox', () => { const input = screen.getByRole('combobox'); await user.click(input); - await act(async () => { - await user.type(input, 'fir'); - jest.advanceTimersByTime(500); // Custom value while typing - }); + await user.type(input, 'fir'); + await act(async () => jest.advanceTimersByTime(500)); // Custom value while typing const customItem = screen.getByRole('option'); @@ -604,8 +602,8 @@ describe('Combobox', () => { const input = screen.getByRole('combobox'); await user.click(input); + await user.type(input, 'Opt'); await act(async () => { - await user.type(input, 'Opt'); jest.advanceTimersByTime(500); // Custom value while typing }); diff --git a/packages/grafana-ui/src/components/Forms/Field.tsx b/packages/grafana-ui/src/components/Forms/Field.tsx index 91355060d60..1152f223cc2 100644 --- a/packages/grafana-ui/src/components/Forms/Field.tsx +++ b/packages/grafana-ui/src/components/Forms/Field.tsx @@ -85,7 +85,7 @@ export const Field = React.forwardRef(
{labelElement}
-
{React.cloneElement(children, childProps)}
+
{React.cloneElement(children, children.type !== React.Fragment ? childProps : undefined)}
{invalid && error && !horizontal && (
{ it('should pass validation checks for email field', async () => { render(); - fireEvent.click(screen.getByRole('button', { name: 'Send reset email' })); + await userEvent.click(screen.getByRole('button', { name: 'Send reset email' })); expect(await screen.findByText('Email or username is required')).toBeInTheDocument(); await userEvent.type(screen.getByRole('textbox', { name: /User Enter your information/i }), 'test@gmail.com'); @@ -49,7 +49,7 @@ describe('VerifyEmail Page', () => { render(); await userEvent.type(screen.getByRole('textbox', { name: /User Enter your information/i }), 'test@gmail.com'); - fireEvent.click(screen.getByRole('button', { name: 'Send reset email' })); + await userEvent.click(screen.getByRole('button', { name: 'Send reset email' })); await waitFor(() => expect(postMock).toHaveBeenCalledWith('/api/user/password/send-reset-email', { userOrEmail: 'test@gmail.com', diff --git a/public/app/features/alerting/unified/components/rule-viewer/RuleViewer.test.tsx b/public/app/features/alerting/unified/components/rule-viewer/RuleViewer.test.tsx index 0d8636c98bc..c12a2ac6264 100644 --- a/public/app/features/alerting/unified/components/rule-viewer/RuleViewer.test.tsx +++ b/public/app/features/alerting/unified/components/rule-viewer/RuleViewer.test.tsx @@ -479,7 +479,7 @@ describe('RuleViewer', () => { expect.objectContaining({ ruleUid: 'test-rule-uid', }), - expect.any(Object) + undefined ); expect(screen.getByTestId('enrichment-section')).toBeInTheDocument(); }); @@ -500,7 +500,7 @@ describe('RuleViewer', () => { expect.objectContaining({ ruleUid: 'test-rule-uid', }), - expect.any(Object) + undefined ); expect(screen.getByTestId('enrichment-section')).toBeInTheDocument(); }); diff --git a/public/app/features/annotations/components/StandardAnnotationQueryEditor.test.tsx b/public/app/features/annotations/components/StandardAnnotationQueryEditor.test.tsx index 9c4d52373ee..619d4d09521 100644 --- a/public/app/features/annotations/components/StandardAnnotationQueryEditor.test.tsx +++ b/public/app/features/annotations/components/StandardAnnotationQueryEditor.test.tsx @@ -67,7 +67,7 @@ describe('StandardAnnotationQueryEditor', () => { expect.objectContaining({ query: expect.objectContaining({ queryType: 'defaultAnnotationsQuery', refId: 'initialAnnotationRef' }), }), - expect.anything() + undefined ); }); @@ -85,7 +85,7 @@ describe('StandardAnnotationQueryEditor', () => { expect.objectContaining({ query: expect.objectContaining({ refId: 'initialAnnotationRef' }), }), - expect.anything() + undefined ); }); @@ -204,7 +204,7 @@ describe('StandardAnnotationQueryEditor', () => { refId: 'A', }), }), - expect.anything() + undefined ); }); @@ -242,7 +242,7 @@ describe('StandardAnnotationQueryEditor', () => { legendFormat: '{{method}} {{endpoint}}', }), }), - expect.anything() + undefined ); }); @@ -284,7 +284,7 @@ describe('StandardAnnotationQueryEditor', () => { refId: 'AnnoTarget', }), }), - expect.anything() + undefined ); }); @@ -320,7 +320,7 @@ describe('StandardAnnotationQueryEditor', () => { expr: 'up', }), }), - expect.anything() + undefined ); }); diff --git a/public/app/features/dashboard-scene/sharing/ShareDrawer/ShareDrawer.test.tsx b/public/app/features/dashboard-scene/sharing/ShareDrawer/ShareDrawer.test.tsx index 35eaec800d1..2c22cd5e88b 100644 --- a/public/app/features/dashboard-scene/sharing/ShareDrawer/ShareDrawer.test.tsx +++ b/public/app/features/dashboard-scene/sharing/ShareDrawer/ShareDrawer.test.tsx @@ -38,7 +38,7 @@ describe('ShareDrawer', () => { expect(locationService.getSearch().get('shareView')).toBe('link'); expect(await screen.findByText('Share externally')).toBeInTheDocument(); const closeButton = await screen.findByTestId(selectors.components.Drawer.General.close); - await act(() => userEvent.click(closeButton)); + await userEvent.click(closeButton); expect(locationService.getSearch().get('shareView')).toBe(null); }); diff --git a/public/app/features/dashboard/containers/DashboardPageProxy.test.tsx b/public/app/features/dashboard/containers/DashboardPageProxy.test.tsx index e26b1099d2d..5083affcf22 100644 --- a/public/app/features/dashboard/containers/DashboardPageProxy.test.tsx +++ b/public/app/features/dashboard/containers/DashboardPageProxy.test.tsx @@ -1,4 +1,4 @@ -import { act, screen, waitFor } from '@testing-library/react'; +import { screen, waitFor } from '@testing-library/react'; import { useParams } from 'react-router-dom-v5-compat'; import { Props } from 'react-virtualized-auto-sizer'; import { render } from 'test/test-utils'; @@ -120,10 +120,8 @@ describe('DashboardPageProxy', () => { it('home dashboard', async () => { getDashboardScenePageStateManager().setDashboardCache(HOME_DASHBOARD_CACHE_KEY, dashMock); - act(() => { - setup({ - route: { routeName: DashboardRoutes.Home, component: () => null, path: '/' }, - }); + setup({ + route: { routeName: DashboardRoutes.Home, component: () => null, path: '/' }, }); await waitFor(() => { @@ -134,11 +132,9 @@ describe('DashboardPageProxy', () => { it('uid dashboard', async () => { getDashboardScenePageStateManager().setDashboardCache('abc-def', dashMock); - act(() => { - setup({ - route: { routeName: DashboardRoutes.Normal, component: () => null, path: '/' }, - uid: 'abc-def', - }); + setup({ + route: { routeName: DashboardRoutes.Normal, component: () => null, path: '/' }, + uid: 'abc-def', }); await waitFor(() => { @@ -156,11 +152,9 @@ describe('DashboardPageProxy', () => { describe('when user can edit a dashboard ', () => { it('should not render DashboardScenePage if route is Home', async () => { getDashboardScenePageStateManager().setDashboardCache(HOME_DASHBOARD_CACHE_KEY, homeMockEditable); - act(() => { - setup({ - route: { routeName: DashboardRoutes.Home, component: () => null, path: '/' }, - uid: '', - }); + setup({ + route: { routeName: DashboardRoutes.Home, component: () => null, path: '/' }, + uid: '', }); await waitFor(() => { @@ -170,11 +164,9 @@ describe('DashboardPageProxy', () => { it('should not render DashboardScenePage if route is Normal and has uid', async () => { getDashboardScenePageStateManager().setDashboardCache('abc-def', dashMockEditable); - act(() => { - setup({ - route: { routeName: DashboardRoutes.Normal, component: () => null, path: '/' }, - uid: 'abc-def', - }); + setup({ + route: { routeName: DashboardRoutes.Normal, component: () => null, path: '/' }, + uid: 'abc-def', }); await waitFor(() => { expect(screen.queryAllByTestId('dashboard-scene-page')).toHaveLength(0); @@ -185,11 +177,9 @@ describe('DashboardPageProxy', () => { describe('when user can only view a dashboard ', () => { it('should render DashboardScenePage if route is Home', async () => { getDashboardScenePageStateManager().setDashboardCache(HOME_DASHBOARD_CACHE_KEY, homeMock); - act(() => { - setup({ - route: { routeName: DashboardRoutes.Home, component: () => null, path: '/' }, - uid: '', - }); + setup({ + route: { routeName: DashboardRoutes.Home, component: () => null, path: '/' }, + uid: '', }); await waitFor(() => { @@ -199,11 +189,9 @@ describe('DashboardPageProxy', () => { it('should render DashboardScenePage if route is Normal and has uid', async () => { getDashboardScenePageStateManager().setDashboardCache('uid', dashMock); - act(() => { - setup({ - route: { routeName: DashboardRoutes.Normal, component: () => null, path: '/' }, - uid: 'uid', - }); + setup({ + route: { routeName: DashboardRoutes.Normal, component: () => null, path: '/' }, + uid: 'uid', }); await waitFor(() => { expect(screen.queryAllByTestId('dashboard-scene-page')).toHaveLength(1); @@ -212,11 +200,9 @@ describe('DashboardPageProxy', () => { it('should render not DashboardScenePage if dashboard UID does not match route UID', async () => { getDashboardScenePageStateManager().setDashboardCache('uid', dashMock); - act(() => { - setup({ - route: { routeName: DashboardRoutes.Normal, component: () => null, path: '/' }, - uid: 'wrongUID', - }); + setup({ + route: { routeName: DashboardRoutes.Normal, component: () => null, path: '/' }, + uid: 'wrongUID', }); await waitFor(() => { expect(screen.queryAllByTestId('dashboard-scene-page')).toHaveLength(0); diff --git a/public/app/features/dashboard/containers/PublicDashboardPageProxy.test.tsx b/public/app/features/dashboard/containers/PublicDashboardPageProxy.test.tsx index 0bb44738440..495532e5951 100644 --- a/public/app/features/dashboard/containers/PublicDashboardPageProxy.test.tsx +++ b/public/app/features/dashboard/containers/PublicDashboardPageProxy.test.tsx @@ -5,7 +5,7 @@ import { render } from 'test/test-utils'; import { selectors as e2eSelectors } from '@grafana/e2e-selectors'; import { config, locationService } from '@grafana/runtime'; import { backendSrv } from 'app/core/services/backend_srv'; -import { DashboardRoutes } from 'app/types/dashboard'; +import { DashboardDTO, DashboardRoutes } from 'app/types/dashboard'; import PublicDashboardPageProxy, { PublicDashboardPageProxyProps } from './PublicDashboardPageProxy'; @@ -56,8 +56,7 @@ describe('PublicDashboardPageProxy', () => { // Mock the dashboard UID response so we don't get any refused connection errors // from this test (as the fetch polyfill means this logic would actually try and call the API) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - jest.spyOn(backendSrv, 'getPublicDashboardByUid').mockResolvedValue({ dashboard: {}, meta: {} } as any); + jest.spyOn(backendSrv, 'getPublicDashboardByUid').mockResolvedValue({ dashboard: {}, meta: {} } as DashboardDTO); }); describe('when scene feature enabled', () => { diff --git a/public/app/features/datasources/components/picker/DataSourceModal.test.tsx b/public/app/features/datasources/components/picker/DataSourceModal.test.tsx index 7ab78dda2ca..116282707e0 100644 --- a/public/app/features/datasources/components/picker/DataSourceModal.test.tsx +++ b/public/app/features/datasources/components/picker/DataSourceModal.test.tsx @@ -19,7 +19,7 @@ const pluginMetaInfo: PluginMetaInfo = { version: '', updated: '', links: [], - logos: { small: '', large: '' }, + logos: { small: 'small.png', large: 'large.png' }, }; function createPluginMeta(name: string, builtIn: boolean): DataSourcePluginMeta { diff --git a/public/app/features/explore/TraceView/components/common/SearchBarInput.tsx b/public/app/features/explore/TraceView/components/common/SearchBarInput.tsx index a6527ca1c0e..831aa856dbf 100644 --- a/public/app/features/explore/TraceView/components/common/SearchBarInput.tsx +++ b/public/app/features/explore/TraceView/components/common/SearchBarInput.tsx @@ -22,7 +22,7 @@ type Props = { onChange: (value: string) => void; }; -const SearchBarInput = memo(({ value, onChange }: Props) => { +const SearchBarInput = memo(({ value = '', onChange }: Props) => { const clearUiFind = () => { onChange(''); }; diff --git a/public/app/features/explore/spec/helper/setup.tsx b/public/app/features/explore/spec/helper/setup.tsx index 647284ad25c..3b4295b3cac 100644 --- a/public/app/features/explore/spec/helper/setup.tsx +++ b/public/app/features/explore/spec/helper/setup.tsx @@ -265,8 +265,8 @@ export function makeDatasourceSetup({ updated: '', version: '', logos: { - small: '', - large: '', + small: 'small.png', + large: 'large.png', }, }, id: id.toString(), diff --git a/public/app/features/plugins/extensions/utils.test.tsx b/public/app/features/plugins/extensions/utils.test.tsx index 2c1563f8f6c..4e00a80cf27 100644 --- a/public/app/features/plugins/extensions/utils.test.tsx +++ b/public/app/features/plugins/extensions/utils.test.tsx @@ -785,7 +785,6 @@ describe('Plugin Extensions / Utils', () => { expect(log.error).toHaveBeenCalledWith(`Extension "${pluginId}/${extensionTitle}" failed to load.`, { message: 'Test error', componentStack: expect.any(String), - digest: expect.any(String), }); expect(screen.getByText(`Extension failed to load: "${pluginId}/${extensionTitle}"`)).toBeVisible(); @@ -818,7 +817,6 @@ describe('Plugin Extensions / Utils', () => { expect(log.error).toHaveBeenCalledWith(`Extension "${pluginId}/${extensionTitle}" failed to load.`, { message: 'Test error', componentStack: expect.any(String), - digest: expect.any(String), }); expect(screen.getByText(`Extension failed to load: "${pluginId}/${extensionTitle}"`)).toBeVisible(); @@ -965,7 +963,6 @@ describe('Plugin Extensions / Utils', () => { expect(log.error).toHaveBeenCalledWith(`Extension "${pluginId}/${extensionTitle}" failed to load.`, { message: 'Test error', componentStack: expect.any(String), - digest: expect.any(String), }); }); @@ -995,7 +992,6 @@ describe('Plugin Extensions / Utils', () => { expect(log.error).toHaveBeenCalledWith(`Extension "${pluginId}/${extensionTitle}" failed to load.`, { message: 'Test error', componentStack: expect.any(String), - digest: expect.any(String), }); }); });