mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-07-26 01:34:24 +08:00
test: some of the test case is added (#2607)
* test: some of the test case is added * fix: build is fixed * chore: tsc is fixed --------- Co-authored-by: Vishal Sharma <makeavish786@gmail.com>
This commit is contained in:
parent
234a69de8c
commit
2f27908434
90
frontend/src/components/RouteTab/RouteTab.test.tsx
Normal file
90
frontend/src/components/RouteTab/RouteTab.test.tsx
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import { fireEvent, render, screen } from '@testing-library/react';
|
||||||
|
import { createMemoryHistory } from 'history';
|
||||||
|
import { Router } from 'react-router-dom';
|
||||||
|
|
||||||
|
import RouteTab from './index';
|
||||||
|
|
||||||
|
function DummyComponent1(): JSX.Element {
|
||||||
|
return <div>Dummy Component 1</div>;
|
||||||
|
}
|
||||||
|
function DummyComponent2(): JSX.Element {
|
||||||
|
return <div>Dummy Component 2</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const testRoutes = [
|
||||||
|
{
|
||||||
|
name: 'Tab1',
|
||||||
|
route: '/tab1',
|
||||||
|
Component: DummyComponent1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Tab2',
|
||||||
|
route: '/tab2',
|
||||||
|
Component: DummyComponent2,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
describe('RouteTab component', () => {
|
||||||
|
test('renders correctly', () => {
|
||||||
|
const history = createMemoryHistory();
|
||||||
|
render(
|
||||||
|
<Router history={history}>
|
||||||
|
<RouteTab history={history} routes={testRoutes} activeKey="Tab1" />
|
||||||
|
</Router>,
|
||||||
|
);
|
||||||
|
expect(screen.getByRole('tab', { name: 'Tab1' })).toBeInTheDocument();
|
||||||
|
expect(screen.getByRole('tab', { name: 'Tab2' })).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('renders correct number of tabs', () => {
|
||||||
|
const history = createMemoryHistory();
|
||||||
|
render(
|
||||||
|
<Router history={history}>
|
||||||
|
<RouteTab history={history} routes={testRoutes} activeKey="Tab1" />
|
||||||
|
</Router>,
|
||||||
|
);
|
||||||
|
const tabs = screen.getAllByRole('tab');
|
||||||
|
expect(tabs.length).toBe(testRoutes.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('sets provided activeKey as active tab', () => {
|
||||||
|
const history = createMemoryHistory();
|
||||||
|
render(
|
||||||
|
<Router history={history}>
|
||||||
|
<RouteTab history={history} routes={testRoutes} activeKey="Tab2" />
|
||||||
|
</Router>,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
screen.getByRole('tab', { name: 'Tab2', selected: true }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('navigates to correct route on tab click', () => {
|
||||||
|
const history = createMemoryHistory();
|
||||||
|
render(
|
||||||
|
<Router history={history}>
|
||||||
|
<RouteTab history={history} routes={testRoutes} activeKey="Tab1" />
|
||||||
|
</Router>,
|
||||||
|
);
|
||||||
|
expect(history.location.pathname).toBe('/');
|
||||||
|
fireEvent.click(screen.getByRole('tab', { name: 'Tab2' }));
|
||||||
|
expect(history.location.pathname).toBe('/tab2');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('calls onChangeHandler on tab change', () => {
|
||||||
|
const onChangeHandler = jest.fn();
|
||||||
|
const history = createMemoryHistory();
|
||||||
|
render(
|
||||||
|
<Router history={history}>
|
||||||
|
<RouteTab
|
||||||
|
routes={testRoutes}
|
||||||
|
activeKey="Tab1"
|
||||||
|
onChangeHandler={onChangeHandler}
|
||||||
|
history={history}
|
||||||
|
/>
|
||||||
|
</Router>,
|
||||||
|
);
|
||||||
|
fireEvent.click(screen.getByRole('tab', { name: 'Tab2' }));
|
||||||
|
expect(onChangeHandler).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
@ -1,10 +1,11 @@
|
|||||||
import { Tabs, TabsProps } from 'antd';
|
import { Tabs, TabsProps } from 'antd';
|
||||||
import history from 'lib/history';
|
import { History } from 'history';
|
||||||
|
|
||||||
function RouteTab({
|
function RouteTab({
|
||||||
routes,
|
routes,
|
||||||
activeKey,
|
activeKey,
|
||||||
onChangeHandler,
|
onChangeHandler,
|
||||||
|
history,
|
||||||
...rest
|
...rest
|
||||||
}: RouteTabProps & TabsProps): JSX.Element {
|
}: RouteTabProps & TabsProps): JSX.Element {
|
||||||
const onChange = (activeRoute: string): void => {
|
const onChange = (activeRoute: string): void => {
|
||||||
@ -47,6 +48,7 @@ interface RouteTabProps {
|
|||||||
}[];
|
}[];
|
||||||
activeKey: TabsProps['activeKey'];
|
activeKey: TabsProps['activeKey'];
|
||||||
onChangeHandler?: VoidFunction;
|
onChangeHandler?: VoidFunction;
|
||||||
|
history: History<unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
RouteTab.defaultProps = {
|
RouteTab.defaultProps = {
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import RouteTab from 'components/RouteTab';
|
import RouteTab from 'components/RouteTab';
|
||||||
import ROUTES from 'constants/routes';
|
import ROUTES from 'constants/routes';
|
||||||
import ResourceAttributesFilter from 'container/ResourceAttributesFilter';
|
import ResourceAttributesFilter from 'container/ResourceAttributesFilter';
|
||||||
import { memo } from 'react';
|
import history from 'lib/history';
|
||||||
|
import { memo, useMemo } from 'react';
|
||||||
import { generatePath, useParams } from 'react-router-dom';
|
import { generatePath, useParams } from 'react-router-dom';
|
||||||
import { useLocation } from 'react-use';
|
import { useLocation } from 'react-use';
|
||||||
|
|
||||||
@ -52,11 +53,8 @@ function ServiceMetrics(): JSX.Element {
|
|||||||
|
|
||||||
const activeKey = getActiveKey();
|
const activeKey = getActiveKey();
|
||||||
|
|
||||||
return (
|
const routes = useMemo(
|
||||||
<>
|
() => [
|
||||||
<ResourceAttributesFilter />
|
|
||||||
<RouteTab
|
|
||||||
routes={[
|
|
||||||
{
|
{
|
||||||
Component: OverViewTab,
|
Component: OverViewTab,
|
||||||
name: overMetrics,
|
name: overMetrics,
|
||||||
@ -78,9 +76,14 @@ function ServiceMetrics(): JSX.Element {
|
|||||||
servicename,
|
servicename,
|
||||||
})}?tab=${externalMetrics}`,
|
})}?tab=${externalMetrics}`,
|
||||||
},
|
},
|
||||||
]}
|
],
|
||||||
activeKey={activeKey}
|
[servicename],
|
||||||
/>
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<ResourceAttributesFilter />
|
||||||
|
<RouteTab routes={routes} history={history} activeKey={activeKey} />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
100
frontend/src/hooks/useComponentPermission.test.ts
Normal file
100
frontend/src/hooks/useComponentPermission.test.ts
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
import { renderHook } from '@testing-library/react';
|
||||||
|
import { ComponentTypes } from 'utils/permission';
|
||||||
|
|
||||||
|
import useComponentPermission from './useComponentPermission';
|
||||||
|
|
||||||
|
describe('useComponentPermission', () => {
|
||||||
|
const componentList: ComponentTypes[] = [
|
||||||
|
'current_org_settings',
|
||||||
|
'invite_members',
|
||||||
|
'create_new_dashboards',
|
||||||
|
'import_dashboard',
|
||||||
|
'export_dashboard',
|
||||||
|
'add_new_alert',
|
||||||
|
'add_new_channel',
|
||||||
|
'set_retention_period',
|
||||||
|
'action',
|
||||||
|
'save_layout',
|
||||||
|
'edit_dashboard',
|
||||||
|
'delete_widget',
|
||||||
|
'new_dashboard',
|
||||||
|
'new_alert_action',
|
||||||
|
'edit_widget',
|
||||||
|
'add_panel',
|
||||||
|
];
|
||||||
|
|
||||||
|
it('should return correct permissions for ADMIN role', () => {
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
useComponentPermission(componentList, 'ADMIN'),
|
||||||
|
);
|
||||||
|
const expectedResult = [
|
||||||
|
true, // current_org_settings
|
||||||
|
true, // invite_members
|
||||||
|
true, // create_new_dashboards
|
||||||
|
true, // import_dashboard
|
||||||
|
true, // export_dashboard
|
||||||
|
true, // add_new_alert
|
||||||
|
true, // add_new_channel
|
||||||
|
true, // set_retention_period
|
||||||
|
true, // action
|
||||||
|
true, // save_layout
|
||||||
|
true, // edit_dashboard
|
||||||
|
true, // delete_widget
|
||||||
|
true, // new_dashboard
|
||||||
|
true, // new_alert_action
|
||||||
|
true, // edit_widget
|
||||||
|
true, // add_panel
|
||||||
|
];
|
||||||
|
expect(result.current).toEqual(expectedResult);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return correct permissions for EDITOR role', () => {
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
useComponentPermission(componentList, 'EDITOR'),
|
||||||
|
);
|
||||||
|
const expectedResult = [
|
||||||
|
false, // current_org_settings
|
||||||
|
false, // invite_members
|
||||||
|
true, // create_new_dashboards
|
||||||
|
true, // import_dashboard
|
||||||
|
true, // export_dashboard
|
||||||
|
true, // add_new_alert
|
||||||
|
false, // add_new_channel
|
||||||
|
false, // set_retention_period
|
||||||
|
true, // action
|
||||||
|
true, // save_layout
|
||||||
|
true, // edit_dashboard
|
||||||
|
true, // delete_widget
|
||||||
|
true, // new_dashboard
|
||||||
|
false, // new_alert_action
|
||||||
|
true, // edit_widget
|
||||||
|
true, // add_panel
|
||||||
|
];
|
||||||
|
expect(result.current).toEqual(expectedResult);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return correct permissions for VIEWER role', () => {
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
useComponentPermission(componentList, 'VIEWER'),
|
||||||
|
);
|
||||||
|
const expectedResult = [
|
||||||
|
false, // current_org_settings
|
||||||
|
false, // invite_members
|
||||||
|
false, // create_new_dashboards
|
||||||
|
false, // import_dashboard
|
||||||
|
true, // export_dashboard
|
||||||
|
false, // add_new_alert
|
||||||
|
false, // add_new_channel
|
||||||
|
false, // set_retention_period
|
||||||
|
false, // action
|
||||||
|
false, // save_layout
|
||||||
|
false, // edit_dashboard
|
||||||
|
false, // delete_widget
|
||||||
|
false, // new_dashboard
|
||||||
|
false, // new_alert_action
|
||||||
|
false, // edit_widget
|
||||||
|
false, // add_panel
|
||||||
|
];
|
||||||
|
expect(result.current).toEqual(expectedResult);
|
||||||
|
});
|
||||||
|
});
|
@ -11,6 +11,7 @@ function SettingsPage(): JSX.Element {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
return (
|
return (
|
||||||
<RouteTab
|
<RouteTab
|
||||||
|
history={history}
|
||||||
{...{
|
{...{
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
|
@ -2,25 +2,31 @@ import RouteTab from 'components/RouteTab';
|
|||||||
import ROUTES from 'constants/routes';
|
import ROUTES from 'constants/routes';
|
||||||
import AllErrorsContainer from 'container/AllError';
|
import AllErrorsContainer from 'container/AllError';
|
||||||
import ResourceAttributesFilter from 'container/ResourceAttributesFilter';
|
import ResourceAttributesFilter from 'container/ResourceAttributesFilter';
|
||||||
|
import history from 'lib/history';
|
||||||
|
import { useMemo } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
function AllErrors(): JSX.Element {
|
function AllErrors(): JSX.Element {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
return (
|
const routes = useMemo(
|
||||||
<>
|
() => [
|
||||||
<ResourceAttributesFilter />
|
|
||||||
<RouteTab
|
|
||||||
{...{
|
|
||||||
routes: [
|
|
||||||
{
|
{
|
||||||
Component: AllErrorsContainer,
|
Component: AllErrorsContainer,
|
||||||
name: t('routes.all_errors'),
|
name: t('routes.all_errors'),
|
||||||
route: ROUTES.ALL_ERROR,
|
route: ROUTES.ALL_ERROR,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
activeKey: t('routes.all_errors'),
|
[t],
|
||||||
}}
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<ResourceAttributesFilter />
|
||||||
|
<RouteTab
|
||||||
|
routes={routes}
|
||||||
|
activeKey={t('routes.all_errors')}
|
||||||
|
history={history}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
@ -52,10 +52,9 @@ function SettingsPage(): JSX.Element {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<RouteTab
|
<RouteTab
|
||||||
{...{
|
routes={common}
|
||||||
routes: common,
|
activeKey={getActiveKey(pathName)}
|
||||||
activeKey: getActiveKey(pathName),
|
history={history}
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user