Skip to content

Commit

Permalink
test(General): ✅ Increment code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoval committed Nov 6, 2023
1 parent 111787c commit 707872b
Show file tree
Hide file tree
Showing 31 changed files with 763 additions and 199 deletions.
2 changes: 1 addition & 1 deletion src/API/Prometheus.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type PrometheusApiResultValue = [number, `${number}` | 'NaN'];

export interface PrometheusResponse<T> {
data: {
result: T;
result: T | [];
};
}

Expand Down
9 changes: 0 additions & 9 deletions src/core/components/DefaultRoute/index.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';

import { render } from '@testing-library/react';

import { VarColors } from '@config/colors';

import HighlightValueCell from './../index';

describe('HighlightValueCell', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('should render without highlighting when value is not updated', () => {
const data = { id: 1, name: 'Test' };
const value = 42;
const format = jest.fn((val) => val);

const useRefSpy = jest.spyOn(React, 'useRef');
useRefSpy.mockReturnValue({ current: value });

const { getByText, queryByTestId } = render(<HighlightValueCell data={data} value={value} format={format} />);

expect(getByText('42')).toBeInTheDocument();
expect(format).toHaveBeenCalledWith(value);
expect(queryByTestId('highlighted-value')).not.toBeInTheDocument();
});

it('should render with highlighting when value is updated', () => {
const data = { id: 1, name: 'Test' };
const value = 42;
const newValue = 50;
const format = jest.fn((val) => val);

const useRefSpy = jest.spyOn(React, 'useRef');
useRefSpy.mockReturnValue({ current: value });

const { getByText, getByTestId } = render(<HighlightValueCell data={data} value={newValue} format={format} />);

expect(getByText('50')).toBeInTheDocument();
expect(format).toHaveBeenCalledWith(newValue);
expect(getByTestId('highlighted-value')).toBeInTheDocument();
expect(getByTestId('highlighted-value')).toHaveStyle(`color: ${VarColors.Green500}; font-weight: 900;`);
});
});
1 change: 1 addition & 0 deletions src/core/components/HighlightValueCell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const HighlightValueCell = function <T>({ value, format }: HighlightValueCellPro

return isValueUpdated() ? (
<div
data-testid="highlighted-value"
style={{
fontWeight: 900,
color: VarColors.Green500
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/LinkCell/LinkCell.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface LinkCellProps<T> {
data: T;
value: string;
value: string | undefined;
link: string;
isDisabled?: boolean;
type?: 'process' | 'site' | 'component' | 'service';
Expand Down
47 changes: 47 additions & 0 deletions src/core/components/LinkCell/__tests__/LinkCell.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { render, screen } from '@testing-library/react';

import { Wrapper } from '@core/components/Wrapper';

import LinkCell from '../index';

describe('LinkCell', () => {
const data = { id: 1, name: 'Test' };

it('should render a disabled cell with Truncate for non-empty value', () => {
render(<LinkCell data={data} value="Long text" isDisabled={true} link="/some-link" />);
const truncateElement = screen.getByText('Long text');
expect(truncateElement).toBeInTheDocument();
});

it('should render a non-disabled cell with Link for non-empty value', () => {
render(
<Wrapper>
<LinkCell data={data} value="Long text" link="/some-link" />
</Wrapper>
);
const linkElement = screen.getByRole('link');
expect(linkElement).toHaveAttribute('href', '#/some-link');
});

it('should render an empty cell', () => {
render(<LinkCell data={data} value="" link="/some-link" />);
const emptyElement = screen.getByText("''");
expect(emptyElement).toBeInTheDocument();
});

it('should render a non-disabled cell with fitContent', () => {
render(
<Wrapper>
<LinkCell data={data} value="Long text" link="/some-link" fitContent={true} />
</Wrapper>
);
const textElement = screen.getByText('Long text');
expect(textElement).toBeInTheDocument();
});

it('should handle non-string values', () => {
render(<LinkCell data={data} value={undefined} link="/some-link" />);
const emptyElement = screen.getByText("''");
expect(emptyElement).toBeInTheDocument();
});
});
49 changes: 32 additions & 17 deletions src/core/components/LinkCell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,44 @@ import ResourceIcon from '@core/components/ResourceIcon';

import { LinkCellProps } from './LinkCell.interfaces';

const LinkCell = function <T>({ value, link, type, isDisabled = false, fitContent }: LinkCellProps<T>) {
// Renders the value of the cell, either truncated or not depending on the fitContent prop
function renderValue(value: string, isDisabled: boolean, fitContent: boolean) {
if (isDisabled) {
return (
<Truncate content={value} position={'middle'}>
{value}
</Truncate>
);
}

return fitContent ? (
value
) : (
<Truncate content={value} position={'middle'}>
{value}
</Truncate>
);
}

// A cell that contains a link to another page
const LinkCell = function <T>({ value, link, type, isDisabled = false, fitContent = false }: LinkCellProps<T>) {
// If there is no value, display an empty string
if (!value) {
return '';
return <span>''</span>;
}

const stringValue = value.toString();

return (
<div style={{ display: 'flex' }}>
{/* If a type is provided, display a corresponding icon */}
{type && <ResourceIcon type={type} />}
{isDisabled && (
<Truncate content={value.toString()} position={'middle'}>
{value}
</Truncate>
)}
{!isDisabled && (
<Link to={link}>
{fitContent ? (
value
) : (
<Truncate content={value.toString()} position={'middle'}>
{value}
</Truncate>
)}
</Link>
{/* If the cell is disabled, render the value without a link */}
{isDisabled ? (
renderValue(stringValue, isDisabled, fitContent)
) : (
// Otherwise, render the value as a link
<Link to={link}>{renderValue(stringValue, isDisabled, fitContent)}</Link>
)}
</div>
);
Expand Down
40 changes: 6 additions & 34 deletions src/core/components/SKSanckeyChart/SankeyFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,8 @@ import {
} from '@patternfly/react-core';
import { FilterIcon } from '@patternfly/react-icons';

interface FilterValues {
[key: string]: string | undefined;
}

export const sankeyMetricOptions = [
{ id: 'none', name: 'Display only relationships' },
{ id: 'byterate', name: 'Compare avg. byterate (last hour)' }
];

export const ServiceClientResourceOptions: { name: string; id: 'client' | 'clientSite' }[] = [
{
name: 'Client sites',
id: 'clientSite'
},
{
name: 'Client processes',
id: 'client'
}
];

export const ServiceServerResourceOptions: { name: string; id: 'server' | 'serverSite' }[] = [
{
name: 'Server sites',
id: 'serverSite'
},
{
name: 'Server processes',
id: 'server'
}
];
import { ServiceClientResourceOptions, ServiceServerResourceOptions, sankeyMetricOptions } from './SkSankey.constants';
import { SankeyFilterValues } from './SkSankeyChart.interfaces';

const SankeyFilter: FC<{ onSearch?: Function }> = memo(({ onSearch }) => {
const [isClientExpanded, setIsClientExpanded] = useState(false);
Expand All @@ -55,21 +27,21 @@ const SankeyFilter: FC<{ onSearch?: Function }> = memo(({ onSearch }) => {
const [visibleMetrics, setVisibleMetrics] = useState(sankeyMetricOptions[0].id);

const handleSelectClient = (_?: ReactMouseEvent<Element, MouseEvent>, selected?: string | number) => {
const selection = selected as keyof FilterValues;
const selection = selected as keyof SankeyFilterValues;

setClientType(selection as 'client' | 'clientSite');
setIsClientExpanded(false);
};

const handleSelectServer = (_?: ReactMouseEvent<Element, MouseEvent>, selected?: string | number) => {
const selection = selected as keyof FilterValues;
const selection = selected as keyof SankeyFilterValues;

setServerType(selection as 'server' | 'serverSite');
setIsServerExpanded(false);
};

const handleMetricSelect = (_?: ReactMouseEvent<Element, MouseEvent>, selected?: string | number) => {
const selection = selected as keyof FilterValues;
const selection = selected as keyof SankeyFilterValues;

setVisibleMetrics(selection as string);
setIsMetricExpanded(false);
Expand Down Expand Up @@ -185,7 +157,7 @@ const SankeyFilter: FC<{ onSearch?: Function }> = memo(({ onSearch }) => {
);

return (
<Toolbar collapseListedFiltersBreakpoint="xl">
<Toolbar data-testid="sankey-filter" collapseListedFiltersBreakpoint="xl">
<ToolbarContent>{toolbarItems}</ToolbarContent>
</Toolbar>
);
Expand Down
55 changes: 55 additions & 0 deletions src/core/components/SKSanckeyChart/SkSankey.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { VarColors } from '@config/colors';
import { DEFAULT_FONT_VAR } from '@config/config';

import { Themes } from './SkSankeyChart.interfaces';

// Constants
export const DEFAULT_SANKEY_CHART_FLOW_VALUE = 0.000001;
export const DEFAULT_SANKEY_CHART_HEIGHT = '350px';

// Themes
const commonStyle = {
fontSize: 14,
fontFamily: DEFAULT_FONT_VAR,
tooltip: { container: { color: VarColors.Black900 } }
};

export const themeStyle: Themes = {
dark: {
...commonStyle,
labelTextColor: VarColors.White
},

light: {
...commonStyle,
labelTextColor: VarColors.Black900
}
};

//Filters
export const sankeyMetricOptions = [
{ id: 'none', name: 'Display only relationships' },
{ id: 'byterate', name: 'Compare avg. byterate (last hour)' }
];

export const ServiceClientResourceOptions: { name: string; id: 'client' | 'clientSite' }[] = [
{
name: 'Client sites',
id: 'clientSite'
},
{
name: 'Client processes',
id: 'client'
}
];

export const ServiceServerResourceOptions: { name: string; id: 'server' | 'serverSite' }[] = [
{
name: 'Server sites',
id: 'serverSite'
},
{
name: 'Server processes',
id: 'server'
}
];
32 changes: 32 additions & 0 deletions src/core/components/SKSanckeyChart/SkSankeyChart.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { VarColors } from '@config/colors';

export interface ThemesProps {
labelTextColor: VarColors;
fontFamily: string;
fontSize: number;
tooltip: { container: { color: string } };
}

export interface Themes {
[key: string]: ThemesProps;
}

export interface SkSankeyChartNode {
id: string;
nodeColor?: string;
}

interface SkSankeyChartLink {
source: string;
target: string;
value: number;
}

export interface SkSankeyChartProps {
nodes: SkSankeyChartNode[];
links: SkSankeyChartLink[];
}

export interface SankeyFilterValues {
[key: string]: string | undefined;
}
Loading

0 comments on commit 707872b

Please sign in to comment.