forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGrafanaRoute.test.tsx
72 lines (57 loc) · 1.97 KB
/
GrafanaRoute.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { render, screen } from '@testing-library/react';
import { History, Location } from 'history';
import React, { ComponentType } from 'react';
import { match } from 'react-router-dom';
import { TestProvider } from 'test/helpers/TestProvider';
import { setEchoSrv } from '@grafana/runtime';
import { Echo } from '../services/echo/Echo';
import { GrafanaRoute, Props } from './GrafanaRoute';
import { GrafanaRouteComponentProps } from './types';
function setup(overrides: Partial<Props>) {
const props: Props = {
location: { search: '?query=hello&test=asd' } as Location,
history: {} as History,
match: {} as match,
route: {
path: '/',
component: () => <div />,
},
...overrides,
};
render(
<TestProvider>
<GrafanaRoute {...props} />
</TestProvider>
);
}
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 = React.lazy(() => {
return new Promise<{ default: ComponentType }>(() => {});
});
setup({ route: { component: PageComponent, path: '' } });
expect(await screen.findByText('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();
});
});