Skip to content

Commit

Permalink
chore: merged in upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
ckbedwell committed Nov 25, 2024
2 parents 7740f28 + 1f550c6 commit 6339837
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AppRootProps } from '@grafana/data';
import { css, Global } from '@emotion/react';

import { ProvisioningJsonData } from 'types';
import { InitialisedRouter } from 'routing/Routers';
import { InitialisedRouter } from 'routing/InitialisedRouter';
import { MetaContextProvider } from 'contexts/MetaContext';
import { PermissionsContextProvider } from 'contexts/PermissionsContext';
import { SMDatasourceProvider } from 'contexts/SMDatasourceContext';
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/SMDatasourceContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { createContext, PropsWithChildren, useContext } from 'react';
import { PluginPage } from '@grafana/runtime';

import { UninitialisedRouter } from 'routing/Routers';
import { UninitialisedRouter } from 'routing/UninitialisedRouter';
import { SMDataSource } from 'datasource/DataSource';
import { useGetSMDatasource } from 'data/useSMSetup';
import { CenteredSpinner } from 'components/CenteredSpinner';
Expand Down
2 changes: 1 addition & 1 deletion src/page/ChecksPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { server } from 'test/server';

import { AlertSensitivity, Check, CheckTypeGroup, ROUTES } from 'types';
import { PLUGIN_URL_PATH } from 'routing/constants';
import { InitialisedRouter } from 'routing/Routers';
import { InitialisedRouter } from 'routing/InitialisedRouter';
import { generateRoutePath } from 'routing/utils';

import { FeatureFlagProvider } from '../components/FeatureFlagProvider';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ import { type CustomRenderOptions, render } from 'test/render';

import { ROUTES } from 'types';
import { PLUGIN_URL_PATH } from 'routing/constants';
import { InitialisedRouter, UninitialisedRouter } from 'routing/Routers';
import { InitialisedRouter } from 'routing/InitialisedRouter';
import { getRoute } from 'routing/utils';

function renderInitialisedRouting(options?: CustomRenderOptions) {
return render(<InitialisedRouter />, options);
}

function renderUninitialisedRouting(options?: CustomRenderOptions) {
render(<UninitialisedRouter />, options);
}

// Mocking these pages because they renders scenes, which makes jest explode
jest.mock('page/DashboardPage', () => ({
DashboardPage: () => <h1>Dashboard page</h1>,
Expand All @@ -27,52 +23,6 @@ jest.mock('page/SceneHomepage', () => ({

const notaRoute = `${PLUGIN_URL_PATH}/404`;

describe('Renders specific welcome pages when app is not initializd', () => {
test(`Route Home`, async () => {
renderUninitialisedRouting({ path: getRoute(ROUTES.Home) });
const text = await screen.findByText('Up and running in seconds, no instrumentation required');
expect(text).toBeInTheDocument();
});

test(`Route Probes`, async () => {
renderUninitialisedRouting({ path: getRoute(ROUTES.Probes) });
const text = await screen.findByText(
'Click the See Probes button to initialize the plugin and see a list of public probes',
{ exact: false }
);
expect(text).toBeInTheDocument();
});

test(`Route Alerts`, async () => {
renderUninitialisedRouting({ path: getRoute(ROUTES.Alerts) });
const text = await screen.findByText(
'Click the See Alerting button to initialize the plugin and see a list of default alerts',
{ exact: false }
);
expect(text).toBeInTheDocument();
});
test(`Route Checks`, async () => {
renderUninitialisedRouting({ path: getRoute(ROUTES.Checks) });
const text = await screen.findByText('Click the Create a Check button to initialize the plugin and create checks', {
exact: false,
});
expect(text).toBeInTheDocument();
});

test(`Route Config`, async () => {
renderUninitialisedRouting({ path: getRoute(ROUTES.Config) });

const text = await screen.findByText('Synthetic Monitoring is not yet initialized');
expect(text).toBeInTheDocument();
});

test('Non-existent route (404)', async () => {
renderUninitialisedRouting({ path: notaRoute });
const text = await screen.findByText('Up and running in seconds, no instrumentation required');
expect(text).toBeInTheDocument();
});
});

// Would like to have asserted on the h1s but rendering the Grafana pluginpage is tricky
describe('Routes to pages correctly', () => {
test('Home page renders', async () => {
Expand Down
35 changes: 0 additions & 35 deletions src/routing/Routers.tsx → src/routing/InitialisedRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ import { LegacyEditRedirect } from 'routing/LegacyEditRedirect';
import { getNewCheckTypeRedirects, getRoute } from 'routing/utils';
import { useCanWriteSM } from 'hooks/useDSPermission';
import { useLimits } from 'hooks/useLimits';
import { useMeta } from 'hooks/useMeta';
import { QueryParamMap, useNavigation } from 'hooks/useNavigation';
import { useQuery } from 'hooks/useQuery';
import { AlertingPage } from 'page/AlertingPage';
import { AlertingWelcomePage } from 'page/AlertingWelcomePage';
import { CheckList } from 'page/CheckList';
import { ChecksWelcomePage } from 'page/ChecksWelcomePage';
import { ConfigPageLayout } from 'page/ConfigPageLayout';
import { AccessTokensTab } from 'page/ConfigPageLayout/tabs/AccessTokensTab';
import { GeneralTab } from 'page/ConfigPageLayout/tabs/GeneralTab';
import { TerraformTab } from 'page/ConfigPageLayout/tabs/TerraformTab';
import { UninitializedTab } from 'page/ConfigPageLayout/tabs/UninitializedTab';
import { DashboardPage } from 'page/DashboardPage';
import { EditCheck } from 'page/EditCheck';
import { EditProbe } from 'page/EditProbe';
Expand All @@ -27,10 +23,7 @@ import { NewProbe } from 'page/NewProbe';
import { CheckNotFound } from 'page/NotFound/CheckNotFound';
import { PluginPageNotFound } from 'page/NotFound/NotFound';
import { Probes } from 'page/Probes';
import { ProbesWelcomePage } from 'page/ProbesWelcomePage';
import { SceneHomepage } from 'page/SceneHomepage';
import { UnprovisionedSetup } from 'page/UnprovisionedSetup';
import { WelcomePage } from 'page/WelcomePage';

import { ChooseCheckGroup } from '../components/ChooseCheckGroup';
import { SceneRedirecter } from '../components/SceneRedirecter';
Expand Down Expand Up @@ -126,31 +119,3 @@ export const InitialisedRouter = () => {
</Routes>
);
};

export const UninitialisedRouter = () => {
const meta = useMeta();
const provisioned = Boolean(meta.jsonData?.metrics?.grafanaName);

// todo: is this the correct check for provisioning?
// todo: is this state even possible in Grafana v11?
if (!provisioned) {
return <UnprovisionedSetup />;
}

return (
<Routes>
<Route path={ROUTES.Home} element={<WelcomePage />} />
<Route path={ROUTES.Scene} element={<WelcomePage />} />
<Route path={ROUTES.Checks} element={<ChecksWelcomePage />} />
<Route path={ROUTES.Probes} element={<ProbesWelcomePage />} />
<Route path={ROUTES.Alerts} element={<AlertingWelcomePage />} />
<Route path={ROUTES.Config} Component={ConfigPageLayout}>
<Route index element={<UninitializedTab />} />
<Route path="*" element={<UninitializedTab />} />
</Route>

{/* TODO: Create 404 instead of navigating to home(?) */}
<Route path="*" element={<Navigate to={ROUTES.Home} />} />
</Routes>
);
};
60 changes: 60 additions & 0 deletions src/routing/UninitialisedRouter.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { screen } from '@testing-library/react';
import { type CustomRenderOptions, render } from 'test/render';

import { ROUTES } from 'types';
import { PLUGIN_URL_PATH } from 'routing/constants';
import { UninitialisedRouter } from 'routing/UninitialisedRouter';
import { getRoute } from 'routing/utils';

function renderUninitialisedRouting(options?: CustomRenderOptions) {
render(<UninitialisedRouter />, options);
}

const notaRoute = `${PLUGIN_URL_PATH}/404`;

describe('Renders specific welcome pages when app is not initializd', () => {
test(`Route Home`, async () => {
renderUninitialisedRouting({ path: getRoute(ROUTES.Home) });
const text = await screen.findByText('Up and running in seconds, no instrumentation required');
expect(text).toBeInTheDocument();
});

test(`Route Probes`, async () => {
renderUninitialisedRouting({ path: getRoute(ROUTES.Probes) });
const text = await screen.findByText(
'Click the See Probes button to initialize the plugin and see a list of public probes',
{ exact: false }
);
expect(text).toBeInTheDocument();
});

test(`Route Alerts`, async () => {
renderUninitialisedRouting({ path: getRoute(ROUTES.Alerts) });
const text = await screen.findByText(
'Click the See Alerting button to initialize the plugin and see a list of default alerts',
{ exact: false }
);
expect(text).toBeInTheDocument();
});
test(`Route Checks`, async () => {
renderUninitialisedRouting({ path: getRoute(ROUTES.Checks) });
const text = await screen.findByText('Click the Create a Check button to initialize the plugin and create checks', {
exact: false,
});
expect(text).toBeInTheDocument();
});

test(`Route Config`, async () => {
renderUninitialisedRouting({ path: getRoute(ROUTES.Config) });

const text = await screen.findByText('Synthetic Monitoring is not yet initialized');
expect(text).toBeInTheDocument();
});

test('Non-existent route (404)', async () => {
renderUninitialisedRouting({ path: notaRoute });
const text = await screen.findByText('Up and running in seconds, no instrumentation required');
expect(text).toBeInTheDocument();
});
});
40 changes: 40 additions & 0 deletions src/routing/UninitialisedRouter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { Navigate, Route, Routes } from 'react-router-dom-v5-compat';

import { ROUTES } from 'types';
import { useMeta } from 'hooks/useMeta';
import { AlertingWelcomePage } from 'page/AlertingWelcomePage';
import { ChecksWelcomePage } from 'page/ChecksWelcomePage';
import { ConfigPageLayout } from 'page/ConfigPageLayout';
import { UninitializedTab } from 'page/ConfigPageLayout/tabs/UninitializedTab';
import { ProbesWelcomePage } from 'page/ProbesWelcomePage';
import { UnprovisionedSetup } from 'page/UnprovisionedSetup';
import { WelcomePage } from 'page/WelcomePage';

export const UninitialisedRouter = () => {
const meta = useMeta();
const provisioned = Boolean(meta.jsonData?.metrics?.grafanaName);

// todo: is this the correct check for provisioning?
// todo: is this state even possible in Grafana v11?
if (!provisioned) {
return <UnprovisionedSetup />;
}

return (
<Routes>
<Route path={ROUTES.Home} element={<WelcomePage />} />
<Route path={ROUTES.Scene} element={<WelcomePage />} />
<Route path={ROUTES.Checks} element={<ChecksWelcomePage />} />
<Route path={ROUTES.Probes} element={<ProbesWelcomePage />} />
<Route path={ROUTES.Alerts} element={<AlertingWelcomePage />} />
<Route path={ROUTES.Config} Component={ConfigPageLayout}>
<Route index element={<UninitializedTab />} />
<Route path="*" element={<UninitializedTab />} />
</Route>

{/* TODO: Create 404 instead of navigating to home(?) */}
<Route path="*" element={<Navigate to={ROUTES.Home} />} />
</Routes>
);
};
3 changes: 2 additions & 1 deletion src/routing/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { InitialisedRouter, UninitialisedRouter } from './Routers';
export { InitialisedRouter } from './InitialisedRouter';
export { UninitialisedRouter } from './UninitialisedRouter';

0 comments on commit 6339837

Please sign in to comment.