|
| 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