The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/core/navigation/GrafanaRoute.test.tsx

89 lines
2.2 KiB

import { screen } from '@testing-library/react';
import { lazy, ComponentType } from 'react';
import { render } from 'test/test-utils';
import { setEchoSrv } from '@grafana/runtime';
import { Echo } from '../services/echo/Echo';
import { GrafanaRoute, Props } from './GrafanaRoute';
import { GrafanaRouteComponentProps } from './types';
const mockLocation = {
search: '?query=hello&test=asd',
pathname: '',
state: undefined,
hash: '',
};
function setup(overrides: Partial<Props>) {
const props: Props = {
location: mockLocation,
history: {
length: 0,
action: 'PUSH',
location: mockLocation,
push: jest.fn(),
replace: jest.fn(),
go: jest.fn(),
goBack: jest.fn(),
goForward: jest.fn(),
block: jest.fn(),
listen: jest.fn(),
createHref: jest.fn(),
},
match: {
params: {},
isExact: false,
path: '',
url: '',
},
route: {
path: '/',
component: () => <div />,
},
...overrides,
};
render(<GrafanaRoute {...props} />);
}
describe('GrafanaRoute', () => {
beforeEach(() => {
setEchoSrv(new Echo());
});
it('Parses search', () => {
let capturedProps: GrafanaRouteComponentProps;
const PageComponent = (props: GrafanaRouteComponentProps) => {
capturedProps = props;
return <div />;
};
setup({ route: { component: PageComponent, path: '' } });
expect(capturedProps!.queryParams.query).toBe('hello');
});
it('Shows loading on lazy load', async () => {
const PageComponent = lazy(() => {
return new Promise<{ default: ComponentType }>(() => {});
});
setup({ route: { component: PageComponent, path: '' } });
expect(await screen.findByLabelText('Loading')).toBeInTheDocument();
});
it('Shows error on page error', async () => {
const PageComponent = () => {
throw new Error('Page threw error');
};
const consoleError = jest.fn();
jest.spyOn(console, 'error').mockImplementation(consoleError);
setup({ route: { component: PageComponent, path: '' } });
expect(await screen.findByRole('heading', { name: 'An unexpected error happened' })).toBeInTheDocument();
expect(consoleError).toHaveBeenCalled();
});
});