Skip to content

Commit

Permalink
Merge pull request #438 from bartoval/refactor_core_test_folder
Browse files Browse the repository at this point in the history
Refactor core test folder
  • Loading branch information
bartoval authored Aug 8, 2024
2 parents f1c556e + 52af4fc commit b5bee2f
Show file tree
Hide file tree
Showing 127 changed files with 393 additions and 425 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint-fix": "yarn lint --fix",
"format": "prettier --write 'src/**/*.{ts,tsx,json,css}'",
"bundle-report": "STATS=server yarn build",
"find-deadcode": "ts-prune",
"find-unused-modules": "ts-prune",
"prepare": "husky",
"commit": "git-cz"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render, screen } from '@testing-library/react';

import ResourceIcon from './index';
import ResourceIcon from '@core/components/ResourceIcon';

describe('ResourceIcon', () => {
it('renders the Skupper icon for the skupper type', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { render, fireEvent, waitFor } from '@testing-library/react';

import SankeyFilter from './../SankeyFilter';
import { ServiceClientResourceOptions, ServiceServerResourceOptions, sankeyMetricOptions } from '../SkSankey.constants';
import {
ServiceClientResourceOptions,
ServiceServerResourceOptions,
sankeyMetricOptions
} from '@core/components/SKSanckeyChart/SkSankey.constants';
import SankeyFilter from '@core/components/SKSanckeyChart/SkSankeyFilter';

describe('SankeyFilter', () => {
it('should render the SankeyFilter component', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';

import SkBreadcrumb from './index';
import SkBreadcrumb from '@core/components/SkBreadcrumb';

describe('SkBreadcrumb component', () => {
describe('SkBreadcrumb', () => {
it('should render null when there are less than 2 paths', () => {
render(
<MemoryRouter initialEntries={['/']}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import { render, screen } from '@testing-library/react';

import { EmptyDataLabels } from './EmptyData.enum';
import SKEmptyData, { SkEmptyDataLabels } from '@core/components/SkEmptyData';

import EmptyData from './index';

describe('EmptyData component', () => {
describe('SkEmptyData', () => {
it('should render with default message', () => {
render(<EmptyData />);
expect(screen.getByText(EmptyDataLabels.Default)).toBeInTheDocument();
render(<SKEmptyData />);
expect(screen.getByText(SkEmptyDataLabels.Default)).toBeInTheDocument();
});

it('should render with custom message', () => {
render(<EmptyData message="Custom Message" />);
render(<SKEmptyData message="Custom Message" />);
expect(screen.getByText('Custom Message')).toBeInTheDocument();
});

it('should render with description', () => {
render(<EmptyData description="This is a description" />);
render(<SKEmptyData description="This is a description" />);
expect(screen.getByText('This is a description')).toBeInTheDocument();
});

it('should render with custom icon', () => {
const CustomIcon = function () {
return <div>Custom Icon</div>;
};
render(<EmptyData icon={CustomIcon} />);
render(<SKEmptyData icon={CustomIcon} />);
expect(screen.getByText('Custom Icon')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import React from 'react';
import { render } from '@testing-library/react';

import { VarColors } from '@config/colors';
import SkHighlightValueCell from '@core/components/SkHighlightValueCell';

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

describe('HighlightValueCell', () => {
describe('SkHighlightValueCell', () => {
afterEach(() => {
jest.restoreAllMocks();
});
Expand All @@ -19,7 +18,7 @@ describe('HighlightValueCell', () => {
const useRefSpy = jest.spyOn(React, 'useRef');
useRefSpy.mockReturnValue({ current: value });

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

expect(getByText('42')).toBeInTheDocument();
expect(format).toHaveBeenCalledWith(value);
Expand All @@ -35,7 +34,7 @@ describe('HighlightValueCell', () => {
const useRefSpy = jest.spyOn(React, 'useRef');
useRefSpy.mockReturnValue({ current: value });

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

expect(getByText('50')).toBeInTheDocument();
expect(format).toHaveBeenCalledWith(newValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
import { render, screen } from '@testing-library/react';

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

import LinkCell from '../index';

describe('LinkCell', () => {
describe('SkLinkCell', () => {
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" />);
render(<SkLinkCell 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" />
<SkLinkCell 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" />);
render(<SkLinkCell 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} />
<SkLinkCell 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" />);
render(<SkLinkCell data={data} value={undefined} link="/some-link" />);
const emptyElement = screen.getByText("''");
expect(emptyElement).toBeInTheDocument();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { render, screen, waitFor } from '@testing-library/react';

import { VarColors } from '@config/colors';
import SkSankeyChart, { addThemeChangeListener, getColors, valueFormat } from '@core/components/SKSanckeyChart/index';
import { DEFAULT_SANKEY_CHART_FLOW_VALUE } from '@core/components/SKSanckeyChart/SkSankey.constants';
import { MetricsLabels } from '@pages/shared/Metrics/Metrics.enum';

import SkSankeyChart, { addThemeChangeListener, getColors, valueFormat } from '../index';
import { DEFAULT_SANKEY_CHART_FLOW_VALUE } from '../SkSankey.constants';

describe('SkSankeyChart', () => {
it('should return an empty string if the value is the default flow value', () => {
expect(valueFormat(DEFAULT_SANKEY_CHART_FLOW_VALUE)).toEqual('');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { SkSearchFilterController } from '..'; // Assuming your service file path
import { FilterType, FilterTypeWithSearchText, FilterSelected } from '../../SkFilter.interfaces'; // Assuming your interface file path
import { SkSearchFilterController } from '@core/components/SkTable/SkSearchFilter/services'; // Assuming your service file path
import {
FilterType,
FilterTypeWithSearchText,
FilterSelected
} from '@core/components/SkTable/SkSearchFilter/SkFilter.interfaces'; // Assuming your interface file path

describe('SkSearchFilterController', () => {
describe('getFilterTypesWithSearchValues', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { render } from '@testing-library/react';
import eventUser from '@testing-library/user-event';

import SkSearchFilter from '..';
import { testIds } from '../SkSearchFilter.testIds';
import SkSearchFilter from '@core/components/SkTable/SkSearchFilter';
import { testIds } from '@core/components/SkTable/SkSearchFilter/SkSearchFilter.testIds';

describe('SkSearchFilter', () => {
const onSearchMock = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fireEvent, render } from '@testing-library/react';
import eventUser from '@testing-library/user-event';

import SkSelectMultiTypeaheadCheckbox from '@core/SkMultiTypeheadWithCheckbox';
import SkSelectTypeHeadWithCheckbox from '../../core/components/SkSelectTypeHeadWithCheckbox';

const initialIdsSelected: string[] = [];
const mockOnSelected = jest.fn();
Expand All @@ -11,10 +11,10 @@ const initOptions = [
{ key: '3', value: '3', label: 'Service 3' }
];

describe('SkSelectMultiTypeaheadCheckbox', () => {
describe('SkSelectTypeaHeadWithCheckbox', () => {
it('renders select component with correct props', () => {
const { getByRole, getByPlaceholderText } = render(
<SkSelectMultiTypeaheadCheckbox
<SkSelectTypeHeadWithCheckbox
initIdsSelected={initialIdsSelected}
initOptions={initOptions}
onSelected={mockOnSelected}
Expand All @@ -30,7 +30,7 @@ describe('SkSelectMultiTypeaheadCheckbox', () => {

it('renders options correctly', async () => {
const { getByRole, getByText } = render(
<SkSelectMultiTypeaheadCheckbox
<SkSelectTypeHeadWithCheckbox
initIdsSelected={initialIdsSelected}
initOptions={initOptions}
onSelected={mockOnSelected}
Expand All @@ -48,7 +48,7 @@ describe('SkSelectMultiTypeaheadCheckbox', () => {

it('calls selectService function on selecting an option', async () => {
const { getByRole, getByText } = render(
<SkSelectMultiTypeaheadCheckbox
<SkSelectTypeHeadWithCheckbox
initIdsSelected={initialIdsSelected}
initOptions={initOptions}
onSelected={mockOnSelected}
Expand All @@ -66,7 +66,7 @@ describe('SkSelectMultiTypeaheadCheckbox', () => {

it('filters options correctly based on search input', async () => {
const { getByRole, getByPlaceholderText, queryByText } = render(
<SkSelectMultiTypeaheadCheckbox
<SkSelectTypeHeadWithCheckbox
initIdsSelected={initialIdsSelected}
initOptions={initOptions}
onSelected={mockOnSelected}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { act, renderHook } from '@testing-library/react';

import { useData } from '@core/SkMultiTypeheadWithCheckbox/useData';
import { SkSelectTypeHeadWithCheckboxUseData } from '@core/components/SkSelectTypeHeadWithCheckbox/SkSelectTypeHeadWithCheckboxUseData';

describe('useData', () => {
describe('SkSelectTypeHeadWithCheckboxUseData', () => {
const mockOnSelected = jest.fn();
const initOptions = [
{ key: '1', value: '1', label: 'Service 1' },
Expand All @@ -11,7 +11,9 @@ describe('useData', () => {
];

it('should toggle service menu correctly', () => {
const { result } = renderHook(() => useData({ initIdsSelected: [], initOptions, onSelected: mockOnSelected }));
const { result } = renderHook(() =>
SkSelectTypeHeadWithCheckboxUseData({ initIdsSelected: [], initOptions, onSelected: mockOnSelected })
);

expect(result.current.isOpen).toBe(false);
act(() => {
Expand All @@ -21,7 +23,9 @@ describe('useData', () => {
});

it('should select and deselect services correctly', () => {
const { result } = renderHook(() => useData({ initIdsSelected: [], initOptions, onSelected: mockOnSelected }));
const { result } = renderHook(() =>
SkSelectTypeHeadWithCheckboxUseData({ initIdsSelected: [], initOptions, onSelected: mockOnSelected })
);

expect(result.current.selected).toEqual([]);
act(() => {
Expand All @@ -41,7 +45,9 @@ describe('useData', () => {
});

it('should select all services correctly', () => {
const { result } = renderHook(() => useData({ initIdsSelected: [], initOptions, onSelected: mockOnSelected }));
const { result } = renderHook(() =>
SkSelectTypeHeadWithCheckboxUseData({ initIdsSelected: [], initOptions, onSelected: mockOnSelected })
);

expect(result.current.selected).toEqual([]);
act(() => {
Expand All @@ -56,7 +62,9 @@ describe('useData', () => {
});

it('should handle menu arrow keys (ArrowUp and ArrowDown)', () => {
const { result } = renderHook(() => useData({ initIdsSelected: [], initOptions, onSelected: mockOnSelected }));
const { result } = renderHook(() =>
SkSelectTypeHeadWithCheckboxUseData({ initIdsSelected: [], initOptions, onSelected: mockOnSelected })
);

act(() => {
result.current.toggleServiceMenu();
Expand Down Expand Up @@ -88,7 +96,9 @@ describe('useData', () => {
});

it('should handle input key down events correctly', () => {
const { result } = renderHook(() => useData({ initIdsSelected: [], initOptions, onSelected: mockOnSelected }));
const { result } = renderHook(() =>
SkSelectTypeHeadWithCheckboxUseData({ initIdsSelected: [], initOptions, onSelected: mockOnSelected })
);

// Simulate Enter key press (open menu)
act(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ import { render, screen } from '@testing-library/react';
import eventUser from '@testing-library/user-event';

import { defaultTimeInterval, timeIntervalMap } from '@config/prometheus';

import SkTimeRangeFilter from '../index';
import { TimeRangeFilterProps } from '../SkTimeRange.interfaces';

const defaultProps: TimeRangeFilterProps = {
duration: defaultTimeInterval.seconds,
onSelectTimeInterval: jest.fn()
};
import SkTimeRangeFilter from '@core/components/SkTimeRangeFilter/index';

describe('SkTimeRangeFilter', () => {
it('should toggle isOpen state on button click', async () => {
const mockHandleToggle = jest.fn();
render(<SkTimeRangeFilter {...defaultProps} onSelectTimeInterval={mockHandleToggle} />);
render(<SkTimeRangeFilter duration={defaultTimeInterval.seconds} onSelectTimeInterval={mockHandleToggle} />);

await eventUser.click(screen.getByTestId('sk-time-range-filter-type'));
await eventUser.click(screen.getByText(timeIntervalMap.fiveMinutes.label));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react';

import SkUpdateDataButton, { refreshDataIntervalMap } from '@core/components/SkUpdateDataButton';
import { Wrapper } from '@core/components/Wrapper';

import SkUpdateDataButton, { refreshDataIntervalMap } from '..';

describe('SkUpdateDataButton component', () => {
it('should renders the component with "Refresh off" text', () => {
render(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { render, fireEvent } from '@testing-library/react';

import ViewDetailCell from '../index';
import SkViewDetailCell from '@core/components/SkViewDetailsCell/index';

describe('ViewDetailCell', () => {
it('should render a button with a search icon', () => {
const { getByLabelText } = render(<ViewDetailCell value="test" />);
const { getByLabelText } = render(<SkViewDetailCell value="test" />);
const button = getByLabelText('Action');
expect(button).toBeInTheDocument();
});

it('should call onClick when button is clicked', () => {
const onClick = jest.fn();
const { getByLabelText } = render(<ViewDetailCell value="test" onClick={onClick} />);
const { getByLabelText } = render(<SkViewDetailCell value="test" onClick={onClick} />);
const button = getByLabelText('Action');

fireEvent.click(button);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertToPercentage } from './convertToPercentage';
import { convertToPercentage } from '@core/utils/convertToPercentage';

describe('convertToPercentage', () => {
it('should return a percentage string removing the cents', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepMergeJSONObjects } from './deepMergeWithJSONObjects';
import { deepMergeJSONObjects } from '@core/utils/deepMergeWithJSONObjects';

describe('deepMergeJSONObjects', () => {
it('should merge two objects with shallow properties', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatByteRate, formatBytes } from './formatBytes';
import { formatByteRate, formatBytes } from '@core/utils/formatBytes';

describe('formatBytes', () => {
it('should return an empty string if bytesSized is NaN', () => {
Expand Down
Loading

0 comments on commit b5bee2f

Please sign in to comment.