Skip to content
This repository was archived by the owner on May 30, 2025. It is now read-only.

Commit 8eb9afc

Browse files
committed
Add unit tests
1 parent 86ac6f6 commit 8eb9afc

File tree

15 files changed

+750
-141
lines changed

15 files changed

+750
-141
lines changed

src/hacbs/components/ApplicationDetails/tabs/overview/visualization/hooks/__data__/workflow-data.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const sampleBuildPipelines: PipelineRunKind[] = [
1515
uid: '94c9a362-f5a5-4e67-b642-c61c6d1134dc',
1616
namespace: 'karthik-jk',
1717
labels: {
18-
'build.appstudio.openshift.io/component': '1-nodejs',
18+
'appstudio.openshift.io/component': '1-nodejs',
1919
'pipelines.openshift.io/runtime': 'generic',
2020
'pipelines.openshift.io/strategy': 'docker',
2121
'tekton.dev/pipeline': 'docker-build',
@@ -28,8 +28,23 @@ export const sampleBuildPipelines: PipelineRunKind[] = [
2828
'build.appstudio.openshift.io/version': '0.1',
2929
},
3030
annotations: {
31-
'build.appstudio.openshift.io/component': '1-nodejs',
32-
'pipelines.openshift.io/runtime': 'generic',
31+
'pipelinesascode.tekton.dev/on-target-branch': '[main,master]',
32+
'pipelinesascode.tekton.dev/repo-url': 'https://github.com/karthikjeeyar/test-nodeapp',
33+
'pipelinesascode.tekton.dev/sha-title': 'Appstudio update test-nodeapp',
34+
'pipelinesascode.tekton.dev/git-auth-secret': 'pac-gitauth-tfts',
35+
'appstudio.redhat.com/updateComponentOnSuccess': 'false',
36+
'build.appstudio.openshift.io/repo': 'https://github.com/karthikjeeyar/test-nodeapp',
37+
'build.appstudio.redhat.com/target_branch': 'main',
38+
'pipelinesascode.tekton.dev/max-keep-runs': '3',
39+
'build.appstudio.redhat.com/pull_request_number': '9',
40+
'pipelinesascode.tekton.dev/pull-request': '9',
41+
'pipelinesascode.tekton.dev/sha-url':
42+
'https://github.com/karthikjeeyar/test-nodeapp/commit/d187a69d325469f38fd354a02a2705d95cc6abbe',
43+
'pipelinesascode.tekton.dev/on-event': '[pull_request]',
44+
'pipelinesascode.tekton.dev/installation-id': '29976162',
45+
'build.appstudio.redhat.com/commit_sha': 'd187a69d325469f38fd354a02a2705d95cc6abbe',
46+
'build.appstudio.openshift.io/image':
47+
'quay.io/karthik_jk/test-nodeapp:on-pr-d187a69d325469f38fd354a02a2705d95cc6abbe',
3348
},
3449
},
3550
spec: {
@@ -295,7 +310,7 @@ export const sampleEnvironments: EnvironmentKind[] = [
295310
kind: 'Environment',
296311
metadata: {
297312
name: 'staging-environment',
298-
uid: '210d9440-2ebf-49c7-a6df-1fa5d21cb111',
313+
uid: '210d9440-2ebf-49c7-a6df-1fa5d21cb112',
299314
},
300315
spec: {
301316
configuration: {
@@ -318,7 +333,7 @@ export const sampleEnvironments: EnvironmentKind[] = [
318333
kind: 'Environment',
319334
metadata: {
320335
name: 'production-environment',
321-
uid: '210d9440-2ebf-49c7-a6df-1fa5d21cb111',
336+
uid: '210d9440-2ebf-49c7-a6df-1fa5d21cb113',
322337
},
323338
spec: {
324339
configuration: {
@@ -433,6 +448,7 @@ export const sampleReleasePlans: ReleasePlanKind[] = [
433448
apiVersion: 'appstudio.redhat.com/v1alpha1',
434449
kind: 'ReleasePlan',
435450
metadata: {
451+
uid: 'df7988a5-0ff4-4f65-b794-8874af3d3095',
436452
name: 'm5-release-link-managed',
437453
namespace: 'managed',
438454
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import '@testing-library/jest-dom';
2+
import { renderHook } from '@testing-library/react-hooks';
3+
import { useNamespace } from '../../../../../../../../utils/namespace-context-utils';
4+
import {
5+
useApplicationSnapshotsEB,
6+
useBuildPipelines,
7+
useComponents,
8+
useEnvironments,
9+
useIntegrationTestScenarios,
10+
useReleasePlans,
11+
useReleases,
12+
useTestPipelines,
13+
} from '../../../../../../../hooks';
14+
import { createCommitObjectFromPLR } from '../../../../../../../utils/commits-utils';
15+
import { pipelineWithCommits } from '../../../../../../Commits/__data__/pipeline-with-commits';
16+
import { WorkflowNodeType } from '../../types';
17+
import {
18+
sampleBuildPipelines,
19+
sampleComponents,
20+
sampleEnvironments,
21+
sampleReleasePlans,
22+
} from '../__data__/workflow-data';
23+
import { useCommitWorkflowData } from '../useCommitWorkflowData';
24+
25+
jest.mock('@openshift/dynamic-plugin-sdk-utils', () => ({
26+
useK8sWatchResource: jest.fn(),
27+
}));
28+
29+
jest.mock('../../../../../../../../utils/namespace-context-utils', () => ({
30+
useNamespace: jest.fn(() => 'test-ns'),
31+
}));
32+
33+
jest.mock('../../../../../../../hooks/', () => ({
34+
useComponents: jest.fn(() => [[], true]),
35+
useIntegrationTestScenarios: jest.fn(() => [[], true]),
36+
useBuildPipelines: jest.fn(() => [[], true]),
37+
useEnvironments: jest.fn(() => [[], true]),
38+
useReleases: jest.fn(() => [[], true]),
39+
useReleasePlans: jest.fn(() => [[], true]),
40+
useTestPipelines: jest.fn(() => [[], true]),
41+
useApplicationSnapshotsEB: jest.fn(() => [[], true]),
42+
}));
43+
44+
const useActiveNamespaceMock = useNamespace as jest.Mock;
45+
const useComponentsMock = useComponents as jest.Mock;
46+
const useIntegrationTestScenariosMock = useIntegrationTestScenarios as jest.Mock;
47+
const useBuildPipelinesMock = useBuildPipelines as jest.Mock;
48+
const useEnvironmentsMock = useEnvironments as jest.Mock;
49+
const useReleasesMock = useReleases as jest.Mock;
50+
const useReleasePlansMock = useReleasePlans as jest.Mock;
51+
const useTestPipelinesMock = useTestPipelines as jest.Mock;
52+
const useApplicationSnapshotsEBMock = useApplicationSnapshotsEB as jest.Mock;
53+
54+
describe('useCommitWorkflowData hook', () => {
55+
beforeEach(() => {
56+
useActiveNamespaceMock.mockReturnValue('test-ns');
57+
useComponentsMock.mockReturnValue([[sampleComponents[0]], true]);
58+
useBuildPipelinesMock.mockReturnValue([[sampleBuildPipelines[0]], true]);
59+
useIntegrationTestScenariosMock.mockReturnValue([[], true]);
60+
useEnvironmentsMock.mockReturnValue([[], true]);
61+
useReleasePlansMock.mockReturnValue([[], true]);
62+
useReleasesMock.mockReturnValue([[], true]);
63+
useTestPipelinesMock.mockReturnValue([[], true]);
64+
useApplicationSnapshotsEBMock.mockReturnValue([[], true]);
65+
66+
const createElement = document.createElement.bind(document);
67+
document.createElement = (tagName) => {
68+
if (tagName === 'canvas') {
69+
return {
70+
getContext: () => ({
71+
measureText: () => ({}),
72+
}),
73+
};
74+
}
75+
return createElement(tagName);
76+
};
77+
});
78+
79+
afterEach(jest.resetAllMocks);
80+
81+
const commit = createCommitObjectFromPLR(pipelineWithCommits[0]);
82+
83+
it('should early return empty values if any one of the resources is not loaded', () => {
84+
useComponentsMock.mockReturnValue([[], false]);
85+
86+
const { result } = renderHook(() => useCommitWorkflowData(commit));
87+
const [nodes, loaded] = result.current;
88+
expect(nodes).toHaveLength(0);
89+
expect(loaded).toBe(false);
90+
});
91+
92+
it('should early return the pipelinerun does not contain commit labels/annotations', () => {
93+
useComponentsMock.mockReturnValue([[sampleComponents[0]], true]);
94+
useBuildPipelinesMock.mockReturnValue([[sampleBuildPipelines[1]], true]);
95+
96+
const { result } = renderHook(() => useCommitWorkflowData(commit));
97+
const [nodes, loaded] = result.current;
98+
expect(nodes).toHaveLength(0);
99+
expect(loaded).toBe(true);
100+
});
101+
102+
it('should return the basic commit workflow nodes', () => {
103+
const { result } = renderHook(() => useCommitWorkflowData(commit));
104+
const [nodes, loaded] = result.current;
105+
106+
expect(nodes).toHaveLength(6);
107+
expect(loaded).toBe(true);
108+
});
109+
110+
it('should return disabled nodes if the resources are not created', () => {
111+
const { result } = renderHook(() => useCommitWorkflowData(commit));
112+
const [nodes, loaded] = result.current;
113+
114+
expect(nodes).toHaveLength(6);
115+
116+
expect(nodes.filter((n) => n.data.isDisabled)).toHaveLength(4);
117+
expect(loaded).toBe(true);
118+
});
119+
120+
it('Abstract nodes should contain static name', () => {
121+
const { result } = renderHook(() => useCommitWorkflowData(commit));
122+
const [nodes] = result.current;
123+
124+
expect(nodes[0].data.label).toBe('commit');
125+
expect(nodes[1].data.label).toBe('build-1-nodejs');
126+
});
127+
128+
it('Non Abstract nodes should contain the child resources', () => {
129+
useEnvironmentsMock.mockReturnValue([sampleEnvironments, true]);
130+
131+
const { result } = renderHook(() => useCommitWorkflowData(commit));
132+
133+
const [nodes] = result.current;
134+
135+
const environmentNodes = nodes.filter(
136+
(n) => n.data.workflowType === WorkflowNodeType.STATIC_ENVIRONMENT,
137+
);
138+
139+
expect(environmentNodes).toHaveLength(3);
140+
});
141+
142+
it('nodes should be disabled / enabled based on the availability of resources', () => {
143+
useEnvironmentsMock.mockReturnValue([sampleEnvironments, true]);
144+
145+
const { result } = renderHook(() => useCommitWorkflowData(commit));
146+
const [nodes] = result.current;
147+
const disabledNodes = nodes.filter((n) => n.data.isDisabled);
148+
const enabledNodes = nodes.filter((node) => !node.data.isDisabled);
149+
150+
expect(enabledNodes).toHaveLength(5);
151+
expect(disabledNodes).toHaveLength(3);
152+
});
153+
154+
it('label should change based on the availability of resources', () => {
155+
const { result } = renderHook(() => useCommitWorkflowData(commit));
156+
157+
const [nodes] = result.current;
158+
const sourceNode = nodes.find((n) => n.data.workflowType === WorkflowNodeType.PIPELINE);
159+
const environmentNode = nodes.find(
160+
(n) => n.data.workflowType === WorkflowNodeType.STATIC_ENVIRONMENT,
161+
);
162+
const managedEnvironmentNode = nodes.find(
163+
(n) => n.data.workflowType === WorkflowNodeType.MANAGED_ENVIRONMENT,
164+
);
165+
166+
expect(sourceNode.data.label).toBe('build-1-nodejs');
167+
expect(environmentNode.data.label).toBe('No static environments set');
168+
expect(managedEnvironmentNode.data.label).toBe('No managed environments set');
169+
});
170+
171+
it('should return all the commit workflow model', () => {
172+
useEnvironmentsMock.mockReturnValue([sampleEnvironments, true]);
173+
useReleasePlansMock.mockReturnValue([sampleReleasePlans, true]);
174+
175+
const { result } = renderHook(() => useCommitWorkflowData(commit));
176+
177+
const [nodes] = result.current;
178+
expect(nodes).toHaveLength(8);
179+
});
180+
});

0 commit comments

Comments
 (0)