github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/frontend-service/src/ui/Pages/CommitInfo/CommitInfoPage.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, Route, Routes } from 'react-router-dom'; 19 import { CommitInfoPage } from './CommitInfoPage'; 20 import { fakeLoadEverything } from '../../../setupTests'; 21 import { updateCommitInfo, CommitInfoState } from '../../utils/store'; 22 import { GetCommitInfoResponse } from '../../../api/api'; 23 24 describe('Commit info page tests', () => { 25 type TestCase = { 26 name: string; 27 commitHash: string; 28 fakeLoadEverything: boolean; 29 commitInfoStoreData: 30 | { 31 commitInfoReady: CommitInfoState; 32 response: GetCommitInfoResponse | undefined; 33 } 34 | undefined; 35 expectedSpinnerCount: number; 36 expectedMainContentCount: number; 37 expectedText: string; 38 }; 39 40 const testCases: TestCase[] = [ 41 { 42 name: 'A loading spinner renders when the page is still loading', 43 fakeLoadEverything: false, 44 commitHash: 'potato', 45 expectedSpinnerCount: 1, 46 expectedMainContentCount: 0, 47 expectedText: 'Loading Configuration', 48 commitInfoStoreData: { 49 commitInfoReady: CommitInfoState.LOADING, 50 response: undefined, 51 }, 52 }, 53 { 54 name: 'An Error is shown when the commit ID is not provided in the URL', 55 fakeLoadEverything: true, 56 commitHash: '', 57 expectedSpinnerCount: 0, 58 expectedMainContentCount: 1, 59 expectedText: 'commit ID not provided', 60 commitInfoStoreData: { 61 commitInfoReady: CommitInfoState.LOADING, 62 response: undefined, 63 }, 64 }, 65 { 66 name: 'A spinner is shown when waiting for the server to respond', 67 fakeLoadEverything: true, 68 commitHash: 'potato', 69 expectedSpinnerCount: 1, 70 expectedMainContentCount: 0, 71 expectedText: 'Loading commit info', 72 commitInfoStoreData: { 73 commitInfoReady: CommitInfoState.LOADING, 74 response: undefined, 75 }, 76 }, 77 { 78 name: 'An error message is shown when the backend returns an error', 79 fakeLoadEverything: true, 80 commitHash: 'potato', 81 expectedSpinnerCount: 0, 82 expectedMainContentCount: 1, 83 expectedText: 'Backend error', 84 commitInfoStoreData: { 85 response: undefined, 86 commitInfoReady: CommitInfoState.ERROR, 87 }, 88 }, 89 { 90 name: 'An error message is shown when the backend returns a not found status', 91 fakeLoadEverything: true, 92 commitHash: 'potato', 93 expectedSpinnerCount: 0, 94 expectedMainContentCount: 1, 95 expectedText: 96 'The provided commit ID was not found in the manifest repo. This is because either the commit ID is incorrect, or it refers to an old commit whose release has been cleaned up by now.', 97 commitInfoStoreData: { 98 response: undefined, 99 commitInfoReady: CommitInfoState.NOTFOUND, 100 }, 101 }, 102 { 103 name: 'Some main content exists when the page is done loading', 104 fakeLoadEverything: true, 105 commitHash: 'potato', 106 expectedSpinnerCount: 0, 107 expectedMainContentCount: 1, 108 expectedText: 'Commit Add google to windows', // this "Commit + commit_message_first_line" string comes from the CommitInfo component logic (so we know that it actually rendered without having some mocking magic) 109 commitInfoStoreData: { 110 commitInfoReady: CommitInfoState.READY, 111 response: { 112 commitHash: 'potato', 113 touchedApps: ['google', 'windows'], 114 commitMessage: `Add google to windows 115 Commit message body line 1 116 Commit message body line 2`, 117 events: [], 118 previousCommitHash: '', 119 nextCommitHash: '', 120 }, 121 }, 122 }, 123 ]; 124 describe.each(testCases)('', (tc) => { 125 test(tc.name, () => { 126 fakeLoadEverything(tc.fakeLoadEverything); 127 if (tc.commitInfoStoreData !== undefined) updateCommitInfo.set(tc.commitInfoStoreData); 128 129 const { container } = render( 130 <MemoryRouter initialEntries={['/ui/commits/' + tc.commitHash]}> 131 <Routes> 132 <Route 133 path={'/ui/commits/' + (tc.commitHash !== '' ? ':commit' : '')} 134 element={<CommitInfoPage />} 135 /> 136 </Routes> 137 </MemoryRouter> 138 ); 139 140 expect(container.getElementsByClassName('spinner')).toHaveLength(tc.expectedSpinnerCount); 141 expect(container.getElementsByClassName('main-content')).toHaveLength(tc.expectedMainContentCount); 142 143 for (const expectedString of tc.expectedText) expect(container.textContent).toContain(expectedString); 144 }); 145 }); 146 });