github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/frontend-service/src/ui/components/ReleaseCardMini/ReleaseCardMini.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 import { ReleaseCardMini, ReleaseCardMiniProps } from './ReleaseCardMini'; 17 import { render } from '@testing-library/react'; 18 import { UpdateOverview } from '../../utils/store'; 19 import { MemoryRouter } from 'react-router-dom'; 20 import { Environment, Priority, Release, UndeploySummary } from '../../../api/api'; 21 import { Spy } from 'spy4js'; 22 import { elementQuerySelectorSafe, makeRelease } from '../../../setupTests'; 23 24 const mock_FormattedDate = Spy.mockModule('../FormattedDate/FormattedDate', 'FormattedDate'); 25 26 describe('Release Card Mini', () => { 27 const getNode = (overrides: ReleaseCardMiniProps) => ( 28 <MemoryRouter> 29 <ReleaseCardMini {...overrides} /> 30 </MemoryRouter> 31 ); 32 const getWrapper = (overrides: ReleaseCardMiniProps) => render(getNode(overrides)); 33 34 type TestData = { 35 name: string; 36 expectedMessage: string; 37 expectedLabel: string | undefined; 38 props: { 39 app: string; 40 version: number; 41 }; 42 rels: Release[]; 43 environments: Environment[]; 44 }; 45 const data: TestData[] = [ 46 { 47 name: 'using A release', 48 props: { app: 'test2', version: 2 }, 49 rels: [makeRelease(2, 'd1.2.3')], 50 expectedMessage: 'test2', 51 expectedLabel: 'd1.2.3 ', 52 environments: [], 53 }, 54 { 55 name: 'with commit id', 56 props: { app: 'test2', version: 2 }, 57 rels: [makeRelease(2, '')], 58 expectedMessage: 'test2', 59 expectedLabel: 'commit2 ', 60 environments: [], 61 }, 62 { 63 name: 'withthout commit id, without displayVersion', 64 props: { app: 'test2', version: 2 }, 65 rels: [makeRelease(2, '', '')], 66 expectedMessage: 'test2', 67 expectedLabel: '#2 ', 68 environments: [], 69 }, 70 { 71 name: 'A release three days ago with an env', 72 props: { app: 'test2', version: 2 }, 73 rels: [makeRelease(2, '')], 74 environments: [ 75 { 76 name: 'other', 77 locks: {}, 78 distanceToUpstream: 0, 79 priority: 0, 80 applications: { 81 test2: { 82 version: 2, 83 queuedVersion: 0, 84 name: 'test2', 85 locks: {}, 86 undeployVersion: false, 87 }, 88 }, 89 }, 90 ], 91 expectedMessage: 'test2', 92 expectedLabel: 'commit2 ', 93 }, 94 { 95 name: 'A release with undeploy version', 96 props: { app: 'test2', version: 2 }, 97 rels: [makeRelease(2, '', '', true)], 98 environments: [ 99 { 100 name: 'other', 101 locks: {}, 102 distanceToUpstream: 0, 103 priority: 0, 104 applications: { 105 test2: { 106 version: 2, 107 queuedVersion: 0, 108 name: 'test2', 109 locks: {}, 110 undeployVersion: false, 111 }, 112 }, 113 }, 114 ], 115 expectedMessage: 'Undeploy Version', 116 expectedLabel: 'undeploy ', 117 }, 118 ]; 119 120 describe.each(data)(`Renders a Release Card`, (testcase) => { 121 it(testcase.name, () => { 122 // given 123 mock_FormattedDate.FormattedDate.returns(<div>some formatted date</div>); 124 // when 125 UpdateOverview.set({ 126 applications: { 127 [testcase.props.app]: { 128 name: testcase.props.app, 129 releases: testcase.rels, 130 sourceRepoUrl: 'url', 131 undeploySummary: UndeploySummary.NORMAL, 132 team: 'no-team', 133 warnings: [], 134 }, 135 }, 136 environmentGroups: [ 137 { 138 environments: testcase.environments, 139 distanceToUpstream: 2, 140 environmentGroupName: 'test-group', 141 priority: Priority.UNRECOGNIZED, 142 }, 143 ], 144 }); 145 const { container } = getWrapper(testcase.props); 146 expect(container.querySelector('.release__details-mini')?.textContent).toContain( 147 testcase.rels[0].sourceAuthor 148 ); 149 expect(elementQuerySelectorSafe(container, '.env-group-chip-list-test').children.length).toBe( 150 testcase.environments.length 151 ); 152 expect(container.querySelector('.release__details-header-title')?.textContent).toBe( 153 testcase.expectedMessage 154 ); 155 expect(container.querySelector('.links-left')?.textContent).toBe(testcase.expectedLabel); 156 }); 157 }); 158 });