github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/frontend-service/src/ui/Pages/Locks/LocksPage.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 { render, renderHook } from '@testing-library/react'; 17 import { LocksPage } from './LocksPage'; 18 import { 19 DisplayLock, 20 UpdateOverview, 21 useAllLocks, 22 useEnvironmentLock, 23 useFilteredEnvironmentLockIDs, 24 } from '../../utils/store'; 25 import { MemoryRouter } from 'react-router-dom'; 26 import { Environment, Priority } from '../../../api/api'; 27 import { fakeLoadEverything } from '../../../setupTests'; 28 29 describe('LocksPage', () => { 30 const getNode = (): JSX.Element | any => ( 31 <MemoryRouter> 32 <LocksPage /> 33 </MemoryRouter> 34 ); 35 const getWrapper = () => render(getNode()); 36 37 it('Renders full app', () => { 38 fakeLoadEverything(true); 39 const { container } = getWrapper(); 40 expect(container.getElementsByClassName('mdc-data-table')[0]).toHaveTextContent('Environment Locks'); 41 expect(container.getElementsByClassName('mdc-data-table')[1]).toHaveTextContent('Application Locks'); 42 }); 43 it('Renders spinner', () => { 44 // given 45 UpdateOverview.set({ 46 loaded: false, 47 }); 48 // when 49 const { container } = getWrapper(); 50 // then 51 expect(container.getElementsByClassName('spinner')).toHaveLength(1); 52 }); 53 }); 54 55 describe('Test env locks', () => { 56 interface dataEnvT { 57 name: string; 58 envs: Environment[]; 59 sortOrder: 'oldestToNewest' | 'newestToOldest'; 60 expectedLockIDs: string[]; 61 } 62 63 const sampleEnvData: dataEnvT[] = [ 64 { 65 name: 'no locks', 66 envs: [], 67 sortOrder: 'oldestToNewest', 68 expectedLockIDs: [], 69 }, 70 { 71 name: 'get one lock', 72 envs: [ 73 { 74 name: 'integration', 75 locks: { locktest: { message: 'locktest', lockId: 'ui-v2-1337' } }, 76 applications: {}, 77 distanceToUpstream: 0, 78 priority: 2, 79 }, 80 ], 81 sortOrder: 'oldestToNewest', 82 expectedLockIDs: ['ui-v2-1337'], 83 }, 84 { 85 name: 'get a few locks (sorted, newestToOldest)', 86 envs: [ 87 { 88 name: 'integration', 89 locks: { 90 locktest: { message: 'locktest', lockId: 'ui-v2-1337', createdAt: new Date(1995, 11, 17) }, 91 lockfoo: { message: 'lockfoo', lockId: 'ui-v2-123', createdAt: new Date(1995, 11, 16) }, 92 lockbar: { message: 'lockbar', lockId: 'ui-v2-321', createdAt: new Date(1995, 11, 15) }, 93 }, 94 applications: {}, 95 distanceToUpstream: 0, 96 priority: 2, 97 }, 98 ], 99 sortOrder: 'newestToOldest', 100 expectedLockIDs: ['ui-v2-1337', 'ui-v2-123', 'ui-v2-321'], 101 }, 102 { 103 name: 'get a few locks (sorted, oldestToNewest)', 104 envs: [ 105 { 106 name: 'integration', 107 locks: { 108 lockbar: { message: 'lockbar', lockId: 'ui-v2-321', createdAt: new Date(1995, 11, 15) }, 109 lockfoo: { message: 'lockfoo', lockId: 'ui-v2-123', createdAt: new Date(1995, 11, 16) }, 110 locktest: { message: 'locktest', lockId: 'ui-v2-1337', createdAt: new Date(1995, 11, 17) }, 111 }, 112 applications: {}, 113 distanceToUpstream: 0, 114 priority: 2, 115 }, 116 ], 117 sortOrder: 'oldestToNewest', 118 expectedLockIDs: ['ui-v2-321', 'ui-v2-123', 'ui-v2-1337'], 119 }, 120 ]; 121 122 describe.each(sampleEnvData)(`Test Lock IDs`, (testcase) => { 123 it(testcase.name, () => { 124 // given 125 UpdateOverview.set({ 126 environmentGroups: [ 127 { 128 environments: testcase.envs, 129 environmentGroupName: 'dontcare', 130 distanceToUpstream: 0, 131 priority: Priority.UNRECOGNIZED, 132 }, 133 ], 134 }); 135 // when 136 const obtained = renderHook(() => useAllLocks().environmentLocks).result.current; 137 // then 138 expect(obtained.map((lock) => lock.lockId)).toStrictEqual(testcase.expectedLockIDs); 139 }); 140 }); 141 142 interface dataEnvFilteredT { 143 name: string; 144 envs: Environment[]; 145 filter: string; 146 expectedLockIDs: string[]; 147 } 148 149 const sampleFilteredEnvData: dataEnvFilteredT[] = [ 150 { 151 name: 'no locks', 152 envs: [], 153 filter: 'integration', 154 expectedLockIDs: [], 155 }, 156 { 157 name: 'get one lock', 158 envs: [ 159 { 160 name: 'integration', 161 locks: { locktest: { message: 'locktest', lockId: 'ui-v2-1337' } }, 162 applications: {}, 163 distanceToUpstream: 0, 164 priority: 0, 165 }, 166 ], 167 filter: 'integration', 168 expectedLockIDs: ['ui-v2-1337'], 169 }, 170 { 171 name: 'get filtered locks (integration, get 1 lock)', 172 envs: [ 173 { 174 name: 'integration', 175 locks: { 176 lockfoo: { message: 'lockfoo', lockId: 'ui-v2-123', createdAt: new Date(1995, 11, 16) }, 177 }, 178 applications: {}, 179 distanceToUpstream: 0, 180 priority: 0, 181 }, 182 { 183 name: 'development', 184 locks: { 185 locktest: { message: 'locktest', lockId: 'ui-v2-1337', createdAt: new Date(1995, 11, 17) }, 186 lockbar: { message: 'lockbar', lockId: 'ui-v2-321', createdAt: new Date(1995, 11, 15) }, 187 }, 188 applications: {}, 189 distanceToUpstream: 0, 190 priority: 0, 191 }, 192 ], 193 filter: 'integration', 194 expectedLockIDs: ['ui-v2-123'], 195 }, 196 { 197 name: 'get filtered locks (development, get 2 lock)', 198 envs: [ 199 { 200 name: 'integration', 201 locks: { 202 lockfoo: { message: 'lockfoo', lockId: 'ui-v2-123', createdAt: new Date(1995, 11, 16) }, 203 }, 204 applications: {}, 205 distanceToUpstream: 0, 206 priority: 0, 207 }, 208 { 209 name: 'development', 210 locks: { 211 lockbar: { message: 'lockbar', lockId: 'ui-v2-321', createdAt: new Date(1995, 11, 15) }, 212 locktest: { message: 'locktest', lockId: 'ui-v2-1337', createdAt: new Date(1995, 11, 17) }, 213 }, 214 applications: {}, 215 distanceToUpstream: 0, 216 priority: 0, 217 }, 218 ], 219 filter: 'development', 220 expectedLockIDs: ['ui-v2-321', 'ui-v2-1337'], 221 }, 222 ]; 223 224 describe.each(sampleFilteredEnvData)(`Test Filtered Lock IDs`, (testcase) => { 225 it(testcase.name, () => { 226 // given 227 UpdateOverview.set({ 228 // environments: testcase.envs, 229 environmentGroups: [ 230 { 231 distanceToUpstream: 0, 232 environmentGroupName: 'group1', 233 environments: testcase.envs, 234 priority: Priority.YOLO, 235 }, 236 ], 237 }); 238 // when 239 const obtained = renderHook(() => useFilteredEnvironmentLockIDs(testcase.filter)).result.current; 240 // then 241 expect(obtained).toStrictEqual(testcase.expectedLockIDs); 242 }); 243 }); 244 245 interface dataTranslateEnvLockT { 246 name: string; 247 envs: [Environment]; 248 id: string; 249 expectedLock: DisplayLock; 250 } 251 252 const sampleTranslateEnvLockData: dataTranslateEnvLockT[] = [ 253 { 254 name: 'Translate lockID to DisplayLock', 255 envs: [ 256 { 257 name: 'integration', 258 locks: { 259 locktest: { 260 message: 'locktest', 261 lockId: 'ui-v2-1337', 262 createdAt: new Date(1995, 11, 17), 263 createdBy: { email: 'kuberpult@fdc.com', name: 'kuberpultUser' }, 264 }, 265 }, 266 applications: {}, 267 distanceToUpstream: 0, 268 priority: 0, 269 }, 270 ], 271 id: 'ui-v2-1337', 272 expectedLock: { 273 date: new Date(1995, 11, 17), 274 lockId: 'ui-v2-1337', 275 environment: 'integration', 276 message: 'locktest', 277 authorEmail: 'kuberpult@fdc.com', 278 authorName: 'kuberpultUser', 279 }, 280 }, 281 ]; 282 283 describe.each(sampleTranslateEnvLockData)(`Test translating lockID to DisplayLock`, (testcase) => { 284 it(testcase.name, () => { 285 // given 286 UpdateOverview.set({ 287 applications: {}, 288 environmentGroups: [ 289 { 290 environments: testcase.envs, 291 environmentGroupName: 'group1', 292 distanceToUpstream: 0, 293 priority: Priority.UNRECOGNIZED, 294 }, 295 ], 296 }); 297 // when 298 const obtained = renderHook(() => useEnvironmentLock(testcase.id)).result.current; 299 // then 300 expect(obtained).toStrictEqual(testcase.expectedLock); 301 }); 302 }); 303 }); 304 305 describe('Test app locks', () => { 306 interface dataAppT { 307 name: string; 308 envs: Environment[]; 309 sortOrder: 'oldestToNewest' | 'newestToOldest'; 310 expectedLockIDs: string[]; 311 } 312 313 const sampleAppData: dataAppT[] = [ 314 { 315 name: 'no locks', 316 envs: [], 317 sortOrder: 'oldestToNewest', 318 expectedLockIDs: [], 319 }, 320 { 321 name: 'get one lock', 322 envs: [ 323 { 324 name: 'integration', 325 locks: {}, 326 distanceToUpstream: 0, 327 priority: 0, 328 applications: { 329 foo: { 330 name: 'foo', 331 version: 1337, 332 locks: { locktest: { message: 'locktest', lockId: 'ui-v2-1337' } }, 333 queuedVersion: 0, 334 undeployVersion: true, 335 }, 336 }, 337 }, 338 ], 339 sortOrder: 'oldestToNewest', 340 expectedLockIDs: ['ui-v2-1337'], 341 }, 342 { 343 name: 'get a few locks (sorted, newestToOldest)', 344 envs: [ 345 { 346 name: 'integration', 347 locks: {}, 348 distanceToUpstream: 0, 349 priority: 0, 350 applications: { 351 foo: { 352 name: 'foo', 353 version: 1337, 354 locks: { 355 locktest: { 356 message: 'locktest', 357 lockId: 'ui-v2-1337', 358 createdAt: new Date(1995, 11, 17), 359 }, 360 lockfoo: { message: 'lockfoo', lockId: 'ui-v2-123', createdAt: new Date(1995, 11, 16) }, 361 lockbar: { message: 'lockbar', lockId: 'ui-v2-321', createdAt: new Date(1995, 11, 15) }, 362 }, 363 queuedVersion: 0, 364 undeployVersion: true, 365 }, 366 }, 367 }, 368 ], 369 sortOrder: 'newestToOldest', 370 expectedLockIDs: ['ui-v2-1337', 'ui-v2-123', 'ui-v2-321'], 371 }, 372 { 373 name: 'get a few locks (sorted, oldestToNewest)', 374 envs: [ 375 { 376 name: 'integration', 377 locks: { 378 lockfoo: { message: 'lockfoo', lockId: 'ui-v2-123', createdAt: new Date(1995, 11, 16) }, 379 locktest: { message: 'locktest', lockId: 'ui-v2-1337', createdAt: new Date(1995, 11, 17) }, 380 lockbar: { message: 'lockbar', lockId: 'ui-v2-321', createdAt: new Date(1995, 11, 15) }, 381 }, 382 distanceToUpstream: 0, 383 priority: 0, 384 applications: { 385 foo: { 386 name: 'foo', 387 version: 1337, 388 queuedVersion: 0, 389 undeployVersion: false, 390 locks: { 391 lockbar: { message: 'lockbar', lockId: 'ui-v2-321', createdAt: new Date(1995, 11, 15) }, 392 }, 393 }, 394 bar: { 395 name: 'bar', 396 version: 420, 397 queuedVersion: 0, 398 undeployVersion: false, 399 locks: { 400 lockfoo: { message: 'lockfoo', lockId: 'ui-v2-123', createdAt: new Date(1995, 11, 16) }, 401 locktest: { 402 message: 'locktest', 403 lockId: 'ui-v2-1337', 404 createdAt: new Date(1995, 11, 17), 405 }, 406 }, 407 }, 408 }, 409 }, 410 ], 411 sortOrder: 'oldestToNewest', 412 expectedLockIDs: ['ui-v2-321', 'ui-v2-123', 'ui-v2-1337'], 413 }, 414 ]; 415 416 describe.each(sampleAppData)(`Test Lock IDs`, (testcase) => { 417 it(testcase.name, () => { 418 // given 419 // UpdateOverview.set({ environmentGroups: testcase.envs }); 420 UpdateOverview.set({ 421 environmentGroups: [ 422 { 423 environments: testcase.envs, 424 environmentGroupName: 'dontcare', 425 distanceToUpstream: 0, 426 priority: Priority.UNRECOGNIZED, 427 }, 428 ], 429 }); 430 431 // when 432 const obtained = renderHook(() => useAllLocks().appLocks).result.current; 433 // then 434 expect(obtained.map((lock) => lock.lockId)).toStrictEqual(testcase.expectedLockIDs); 435 }); 436 }); 437 });