Skip to content

Commit

Permalink
removed dynamic-plugin-sdk dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
lokanandaprabhu committed Jul 1, 2024
1 parent b5b5d60 commit 177c9a7
Show file tree
Hide file tree
Showing 24 changed files with 716 additions and 291 deletions.
2 changes: 0 additions & 2 deletions packages/pipelines/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
"@aonic-ui/jest-config": "*",
"@aonic-ui/rollup-config": "*",
"@aonic-ui/typescript-config": "*",
"@openshift-console/dynamic-plugin-sdk": "1.2.0",
"@openshift-console/dynamic-plugin-sdk-webpack": "1.1.0",
"@patternfly/react-core": "^5.1.2",
"@patternfly/react-icons": "^5.1.2",
"@patternfly/react-tokens": "^5.1.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
import { renderHook } from '@testing-library/react';
import { usePipelineRunsForPipelineOrRepository } from '../usePipelineRunsForPipelineOrRepository';
import { usePipelineRuns } from '../usePipelineRuns';
import {
ConsoleProxyFetchJSON,
FetchUtilsType,
commonFetchJSON,
commonFetchText,
} from '../../types/k8s';

jest.mock('../usePipelineRuns', () => ({
usePipelineRuns: jest.fn(),
}));

const createMockCommonFetchJson = (): jest.MockedFunction<commonFetchJSON> => {
const mockFn = jest.fn() as unknown as jest.MockedFunction<commonFetchJSON>;

// Add the additional methods to the mock function
mockFn.put = jest.fn();
mockFn.post = jest.fn();
mockFn.patch = jest.fn();
mockFn.delete = jest.fn();

return mockFn;
};

const mockCommonFetchJson = createMockCommonFetchJson();
const mockCommonFetchText: jest.MockedFunction<commonFetchText> =
jest.fn() as jest.MockedFunction<commonFetchText>;
const mockConsoleProxyFetchJSON: jest.MockedFunction<ConsoleProxyFetchJSON> =
jest.fn() as jest.MockedFunction<ConsoleProxyFetchJSON>;
const mockConsoleProxyFetchLog: jest.MockedFunction<ConsoleProxyFetchJSON> =
jest.fn() as jest.MockedFunction<ConsoleProxyFetchJSON>;

const mockFetchUtils: FetchUtilsType = {
hooks: {
useK8sWatchResource: jest.fn(),
},
resourceFetchers: {
commonFetchText: mockCommonFetchText,
commonFetchJson: mockCommonFetchJson,
consoleProxyFetchJSON: mockConsoleProxyFetchJSON,
consoleProxyFetchLog: mockConsoleProxyFetchLog,
},
};

describe('usePipelineRunsForPipelineOrRepository', () => {
const mockUsePipelineRuns = usePipelineRuns as jest.Mock;
const tektonResultsBaseURL = 'https://tekton-results.example.com';

beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -23,14 +62,23 @@ describe('usePipelineRunsForPipelineOrRepository', () => {
mockUsePipelineRuns.mockReturnValue([[], true, null, jest.fn()]);

renderHook(() =>
usePipelineRunsForPipelineOrRepository(namespace, options, cacheKey, isTektonResultEnabled),
usePipelineRunsForPipelineOrRepository(
mockFetchUtils,
namespace,
tektonResultsBaseURL,
isTektonResultEnabled,
options,
cacheKey,
),
);

expect(mockUsePipelineRuns).toHaveBeenCalledWith(
mockFetchUtils,
namespace,
tektonResultsBaseURL,
isTektonResultEnabled,
{ selector },
cacheKey,
isTektonResultEnabled,
);
});

Expand All @@ -48,14 +96,23 @@ describe('usePipelineRunsForPipelineOrRepository', () => {
mockUsePipelineRuns.mockReturnValue([[], true, null, jest.fn()]);

renderHook(() =>
usePipelineRunsForPipelineOrRepository(namespace, options, cacheKey, isTektonResultEnabled),
usePipelineRunsForPipelineOrRepository(
mockFetchUtils,
namespace,
tektonResultsBaseURL,
isTektonResultEnabled,
options,
cacheKey,
),
);

expect(mockUsePipelineRuns).toHaveBeenCalledWith(
mockFetchUtils,
namespace,
tektonResultsBaseURL,
isTektonResultEnabled,
{ selector },
cacheKey,
isTektonResultEnabled,
);
});

Expand All @@ -69,14 +126,23 @@ describe('usePipelineRunsForPipelineOrRepository', () => {
mockUsePipelineRuns.mockReturnValue([[], true, null, jest.fn()]);

renderHook(() =>
usePipelineRunsForPipelineOrRepository(namespace, options, cacheKey, isTektonResultEnabled),
usePipelineRunsForPipelineOrRepository(
mockFetchUtils,
namespace,
tektonResultsBaseURL,
isTektonResultEnabled,
options,
cacheKey,
),
);

expect(mockUsePipelineRuns).toHaveBeenCalledWith(
mockFetchUtils,
namespace,
tektonResultsBaseURL,
isTektonResultEnabled,
{ selector },
cacheKey,
isTektonResultEnabled,
);
});
});
109 changes: 77 additions & 32 deletions packages/pipelines/src/hooks/__tests__/useRuns.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
import { renderHook } from '@testing-library/react';
import {
getGroupVersionKindForModel,
useK8sWatchResource,
} from '@openshift-console/dynamic-plugin-sdk';
import { mockK8sPipelineRuns } from '../__data__/mock-PipelineRun-data-k8s';
import { mockTRPipelineRuns } from '../__data__/mock-PipelineRun-data-TR';
import { useRuns } from '../useRuns';
import { useTRPipelineRuns } from '../useTRPipelineRuns';
import {
ConsoleProxyFetchJSON,
FetchUtilsType,
commonFetchJSON,
commonFetchText,
} from '../../types/k8s';

jest.mock('../useTRPipelineRuns', () => ({
useTRPipelineRuns: jest.fn(),
}));

const mockUseK8sWatchResource = jest.fn();

const createMockCommonFetchJson = (): jest.MockedFunction<commonFetchJSON> => {
const mockFn = jest.fn() as unknown as jest.MockedFunction<commonFetchJSON>;

// Add the additional methods to the mock function
mockFn.put = jest.fn();
mockFn.post = jest.fn();
mockFn.patch = jest.fn();
mockFn.delete = jest.fn();

return mockFn;
};

const mockCommonFetchJson = createMockCommonFetchJson();
const mockCommonFetchText: jest.MockedFunction<commonFetchText> =
jest.fn() as jest.MockedFunction<commonFetchText>;
const mockConsoleProxyFetchJSON: jest.MockedFunction<ConsoleProxyFetchJSON> =
jest.fn() as jest.MockedFunction<ConsoleProxyFetchJSON>;
const mockConsoleProxyFetchLog: jest.MockedFunction<ConsoleProxyFetchJSON> =
jest.fn() as jest.MockedFunction<ConsoleProxyFetchJSON>;

const mockFetchUtils: FetchUtilsType = {
hooks: {
useK8sWatchResource: mockUseK8sWatchResource,
},
resourceFetchers: {
commonFetchText: mockCommonFetchText,
commonFetchJson: mockCommonFetchJson,
consoleProxyFetchJSON: mockConsoleProxyFetchJSON,
consoleProxyFetchLog: mockConsoleProxyFetchLog,
},
};

describe('useRuns', () => {
const mockUseK8sWatchResource = useK8sWatchResource as jest.Mock;
const mockUseTRPipelineRuns = useTRPipelineRuns as jest.Mock;
const mockGetGroupVersionKindForModel = getGroupVersionKindForModel as jest.Mock;
const tektonResultsBaseURL = 'https://tekton-results.example.com';
beforeEach(() => {
jest.clearAllMocks();
});
Expand All @@ -24,17 +59,18 @@ describe('useRuns', () => {
const groupVersionKind = { group: 'tekton.dev', version: 'v1', kind: 'PipelineRun' };
const namespace = 'test-namespace';
const cacheKey = 'test-cache-key';

mockUseK8sWatchResource.mockReturnValue([mockK8sPipelineRuns, true, null]);
mockGetGroupVersionKindForModel.mockReturnValue({
group: 'tekton.dev',
version: 'v1',
kind: 'PipelineRun',
});
mockUseTRPipelineRuns.mockReturnValue([mockTRPipelineRuns, true, null, jest.fn()]);

const { result: trResult } = renderHook(() =>
useRuns(groupVersionKind, namespace, undefined, cacheKey, true),
useRuns(
mockFetchUtils,
groupVersionKind,
namespace,
tektonResultsBaseURL,
true,
undefined,
cacheKey,
),
);

expect(trResult.current[0].length).toBe(4); // 2 k8s resources + 2 from tekton-results
Expand All @@ -48,15 +84,18 @@ describe('useRuns', () => {
const cacheKey = 'test-cache-key';

mockUseK8sWatchResource.mockReturnValue([mockK8sPipelineRuns, true, null]);
mockGetGroupVersionKindForModel.mockReturnValue({
group: 'tekton.dev',
version: 'v1',
kind: 'PipelineRun',
});
mockUseTRPipelineRuns.mockReturnValue([mockTRPipelineRuns, true, null, jest.fn()]);

const { result: trResult } = renderHook(() =>
useRuns(groupVersionKind, namespace, undefined, cacheKey, false),
useRuns(
mockFetchUtils,
groupVersionKind,
namespace,
tektonResultsBaseURL,
false,
undefined,
cacheKey,
),
);

expect(trResult.current[0].length).toBe(2); // 2 from tekton-results
Expand All @@ -70,11 +109,6 @@ describe('useRuns', () => {
const cacheKey = 'test-cache-key';

mockUseK8sWatchResource.mockReturnValue([mockK8sPipelineRuns, true, null]);
mockGetGroupVersionKindForModel.mockReturnValue({
group: 'tekton.dev',
version: 'v1',
kind: 'PipelineRun',
});
mockUseTRPipelineRuns.mockReturnValue([
[],
false,
Expand All @@ -83,7 +117,15 @@ describe('useRuns', () => {
]);

const { result: trResult } = renderHook(() =>
useRuns(groupVersionKind, namespace, undefined, cacheKey, true),
useRuns(
mockFetchUtils,
groupVersionKind,
namespace,
tektonResultsBaseURL,
true,
undefined,
cacheKey,
),
);

expect(trResult.current[0].length).toBe(2); // Only k8s resources
Expand All @@ -97,15 +139,18 @@ describe('useRuns', () => {
const cacheKey = 'test-cache-key';

mockUseK8sWatchResource.mockReturnValue([[], true, null]);
mockGetGroupVersionKindForModel.mockReturnValue({
group: 'tekton.dev',
version: 'v1',
kind: 'PipelineRun',
});
mockUseTRPipelineRuns.mockReturnValue([mockTRPipelineRuns, true, null, jest.fn()]);

const { result: trResult } = renderHook(() =>
useRuns(groupVersionKind, namespace, undefined, cacheKey, true),
useRuns(
mockFetchUtils,
groupVersionKind,
namespace,
tektonResultsBaseURL,
true,
undefined,
cacheKey,
),
);

expect(trResult.current[0].length).toBe(2); // Only Tekton Results
Expand Down
Loading

0 comments on commit 177c9a7

Please sign in to comment.