fix some unit tests

This commit is contained in:
Ashley Harrison
2025-12-02 16:51:42 +00:00
parent 0da94b11ee
commit efeac25952
14 changed files with 46 additions and 67 deletions
@@ -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
);
});
@@ -317,7 +317,7 @@ const BaseActions = ({ children, disabled, variant, className }: ActionsProps) =
<div className={cx(css, className)}>
{React.Children.map(children, (child) => {
return React.isValidElement<Record<string, unknown>>(child)
? cloneElement(child, { disabled: isDisabled, ...child.props })
? cloneElement(child, child.type !== React.Fragment ? { disabled: isDisabled, ...child.props } : undefined)
: null;
})}
</div>
@@ -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
});
@@ -85,7 +85,7 @@ export const Field = React.forwardRef<HTMLDivElement, FieldProps>(
<div className={cx(styles.field, horizontal && styles.fieldHorizontal, className)} {...otherProps}>
{labelElement}
<div>
<div ref={ref}>{React.cloneElement(children, childProps)}</div>
<div ref={ref}>{React.cloneElement(children, children.type !== React.Fragment ? childProps : undefined)}</div>
{invalid && error && !horizontal && (
<div
className={cx(styles.fieldValidationWrapper, {
@@ -1,4 +1,4 @@
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { SendResetMailPage } from './SendResetMailPage';
@@ -38,7 +38,7 @@ describe('VerifyEmail Page', () => {
it('should pass validation checks for email field', async () => {
render(<SendResetMailPage />);
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(<SendResetMailPage />);
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',
@@ -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();
});
@@ -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
);
});
@@ -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);
});
@@ -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);
@@ -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', () => {
@@ -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 {
@@ -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('');
};
@@ -265,8 +265,8 @@ export function makeDatasourceSetup({
updated: '',
version: '',
logos: {
small: '',
large: '',
small: 'small.png',
large: 'large.png',
},
},
id: id.toString(),
@@ -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),
});
});
});