github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/frontend-service/src/ui/components/EnvironmentCard/EnvironmentCard.test.tsx (about) 1 /*This file is part of kuberpult. 2 3 Kuberpult is free software: you can redistribute it and/or modify 4 it under the terms of the Expat(MIT) License as published by 5 the Free Software Foundation. 6 7 Kuberpult is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 MIT License for more details. 11 12 You should have received a copy of the MIT License 13 along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>. 14 15 Copyright 2023 freiheit.com*/ 16 17 import { render } from '@testing-library/react'; 18 import { MemoryRouter } from 'react-router-dom'; 19 import { EnvironmentGroup, Priority } from '../../../api/api'; 20 import { EnvironmentGroupCard } from './EnvironmentCard'; 21 import { UpdateOverview } from '../../utils/store'; 22 23 const getNode = (group: EnvironmentGroup): JSX.Element | any => ( 24 <MemoryRouter> 25 <EnvironmentGroupCard environmentGroup={group} /> 26 </MemoryRouter> 27 ); 28 const getWrapper = (group: EnvironmentGroup) => render(getNode(group)); 29 30 describe('Test Environment Cards', () => { 31 interface dataEnvT { 32 name: string; 33 group: EnvironmentGroup; 34 expectedNumEnvLockButtons: number; 35 expectedNumGroupsLockButtons: number; 36 expectedPriorityClassName: string; 37 expectedNumButtonsEnv: number; 38 } 39 40 const sampleEnvData: dataEnvT[] = [ 41 { 42 name: '1 group 0 envs', 43 group: { 44 environmentGroupName: 'group1', 45 distanceToUpstream: 2, 46 priority: Priority.UNRECOGNIZED, 47 environments: [], 48 }, 49 expectedNumGroupsLockButtons: 1, 50 expectedNumEnvLockButtons: 0, 51 expectedPriorityClassName: 'environment-priority-unrecognized', // group priority is UNRECOGNIZED / unknown 52 expectedNumButtonsEnv: 1, 53 }, 54 { 55 name: '1 group 1 env', 56 group: { 57 environmentGroupName: 'group1', 58 distanceToUpstream: 2, 59 priority: Priority.PRE_PROD, 60 environments: [ 61 { 62 name: 'env1', 63 distanceToUpstream: 2, 64 locks: {}, 65 applications: {}, 66 priority: Priority.PRE_PROD, 67 config: {}, 68 }, 69 ], 70 }, 71 expectedNumGroupsLockButtons: 1, 72 expectedNumEnvLockButtons: 1, 73 expectedPriorityClassName: 'environment-priority-pre_prod', 74 expectedNumButtonsEnv: 3, 75 }, 76 { 77 name: '1 group 2 env', 78 group: { 79 environmentGroupName: 'group1', 80 distanceToUpstream: 2, 81 priority: Priority.UPSTREAM, 82 environments: [ 83 { 84 name: 'env1', 85 distanceToUpstream: 2, 86 locks: {}, 87 applications: {}, 88 priority: Priority.UPSTREAM, 89 config: {}, 90 }, 91 { 92 name: 'env2', 93 distanceToUpstream: 2, 94 locks: {}, 95 applications: {}, 96 priority: Priority.UPSTREAM, 97 config: {}, 98 }, 99 ], 100 }, 101 expectedNumGroupsLockButtons: 1, 102 expectedNumEnvLockButtons: 2, 103 expectedPriorityClassName: 'environment-priority-upstream', 104 expectedNumButtonsEnv: 5, 105 }, 106 ]; 107 108 describe.each(sampleEnvData)(`Test Lock IDs`, (testcase) => { 109 it(testcase.name, () => { 110 // given 111 UpdateOverview.set({ 112 environmentGroups: [testcase.group], 113 }); 114 // when 115 const { container } = getWrapper(testcase.group); 116 // then 117 const lockGroupElems = container.getElementsByClassName('test-lock-group'); 118 expect(lockGroupElems).toHaveLength(testcase.expectedNumGroupsLockButtons); 119 const lockEnvElems = container.getElementsByClassName('test-lock-env'); 120 expect(lockEnvElems).toHaveLength(testcase.expectedNumEnvLockButtons); 121 const buttons = container.getElementsByClassName('environment-action'); 122 expect(buttons).toHaveLength(testcase.expectedNumButtonsEnv); 123 124 // when 125 const envGroupHeader = container.querySelector('.environment-group-lane__header'); 126 // then 127 expect(envGroupHeader?.className).toContain(testcase.expectedPriorityClassName); 128 }); 129 }); 130 });