Skip to content

Commit

Permalink
fix: adjust tests for namespace instead of workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoPedroPP committed Feb 11, 2025
1 parent 4953839 commit baa6680
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useComponent, useComponents } from '../../../../hooks/useComponents';
import {
createK8sWatchResourceMock,
createUseApplicationMock,
createUseWorkspaceInfoMock,
createUseNamespaceMock,
routerRenderer,
} from '../../../../utils/test-utils';
import { pipelineWithCommits } from '../../../Commits/__data__/pipeline-with-commits';
Expand Down Expand Up @@ -39,7 +39,7 @@ const useParamsMock = useParams as jest.Mock;
describe('ComponentActivityTab', () => {
let navigateMock: jest.Mock;

createUseWorkspaceInfoMock({ namespace: 'test-ns', workspace: 'test-ws' });
createUseNamespaceMock('test-ns');

beforeEach(() => {
watchResourceMock.mockReturnValue([pipelineWithCommits.slice(0, 4), true]);
Expand All @@ -49,7 +49,6 @@ describe('ComponentActivityTab', () => {
useNavigateMock.mockImplementation(() => navigateMock);
useParamsMock.mockReturnValue({
activityTab: 'latest-commits',
workspaceName: 'test-ws',
componentName: 'test-component',
});
});
Expand All @@ -73,7 +72,7 @@ describe('ComponentActivityTab', () => {

await act(() => fireEvent.click(plrTab));
expect(navigateMock).toHaveBeenCalledWith(
'/workspaces/test-ws/applications/my-test-output/components/test-component/activity/pipelineruns',
'/workspaces/test-ns/applications/my-test-output/components/test-component/activity/pipelineruns',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useComponent } from '../../../../hooks/useComponents';
import { PACState } from '../../../../hooks/usePACState';
import {
createK8sWatchResourceMock,
createUseWorkspaceInfoMock,
createUseNamespaceMock,
renderWithQueryClientAndRouter,
WithTestWorkspaceContext,
} from '../../../../utils/test-utils';
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('ComponentDetailsView', () => {
let navigateMock: jest.Mock;
const showModalMock = jest.fn();

createUseWorkspaceInfoMock({ namespace: 'test-ns', workspace: 'test-ws' });
createUseNamespaceMock('test-ns');

beforeEach(() => {
useComponentMock.mockReturnValue([mockComponent, true]);
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('ComponentDetailsView', () => {

await act(() => fireEvent.click(activityTab));
expect(navigateMock).toHaveBeenCalledWith(
'/workspaces/test-ws/applications/test-application/components/human-resources/activity',
'/workspaces/test-ns/applications/test-application/components/human-resources/activity',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useTRPipelineRuns } from '../../../../hooks/useTektonResults';
import { ComponentGroupVersionKind, PipelineRunGroupVersionKind } from '../../../../models';
import {
createK8sWatchResourceMock,
createUseWorkspaceInfoMock,
createUseApplicationMock,
createUseNamespaceMock,
} from '../../../../utils/test-utils';
import { componentCRMocks } from '../../__data__/mock-data';
import { mockPipelineRuns } from '../../__data__/mock-pipeline-run';
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('ComponentListViewPage', () => {
paramValues = {};
});

createUseWorkspaceInfoMock({ namespace: 'test-ns', workspace: 'test-ws' });
createUseNamespaceMock('test-ns');

it('should render skeleton if data is not loaded', () => {
useK8sWatchResourceMock.mockReturnValue([[], false]);
Expand All @@ -107,7 +107,7 @@ describe('ComponentListViewPage', () => {
const button = screen.getByText('Add component');
expect(button).toBeInTheDocument();
expect(button.closest('a').href).toBe(
'http://localhost/workspaces/test-ws/import?application=test-app',
'http://localhost/workspaces/test-ns/import?application=test-app',
);
});

Expand Down
13 changes: 13 additions & 0 deletions src/utils/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@testing-library/react';
import { FormikValues, Formik } from 'formik';
import * as NamespaceUtils from '../components/Namespace/namespace-context';
import * as NamespaceHook from '../components/Namespace/useNamespaceInfo';
import * as WorkspaceHook from '../components/Workspace/useWorkspaceInfo';
import * as WorkspaceUtils from '../components/Workspace/workspace-context';
import * as ApplicationHook from '../hooks/useApplications';
Expand Down Expand Up @@ -237,6 +238,18 @@ export const createUseWorkspaceInfoMock = (
return mockFn;
};

export const createUseNamespaceMock = (initialValue: string = ''): jest.Mock => {
const mockFn = jest.fn().mockReturnValue(initialValue);

jest.spyOn(NamespaceHook, 'useNamespace').mockImplementation(mockFn);

beforeEach(() => {
mockFn.mockReturnValue(initialValue);
});

return mockFn;
};

export const createUseApplicationMock = (
initialValue: [{ metadata: { name: string } }, boolean] = [{ metadata: { name: '' } }, false],
): jest.Mock => {
Expand Down

0 comments on commit baa6680

Please sign in to comment.