github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/frontend-service/src/ui/components/LockDisplay/LockDisplay.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 { act, render } from '@testing-library/react';
    17  import { Spy } from 'spy4js';
    18  import { DisplayLock } from '../../utils/store';
    19  import { isOutdated, LockDisplay } from './LockDisplay';
    20  import { documentQuerySelectorSafe } from '../../../setupTests';
    21  const mock_addAction = Spy.mockModule('../../utils/store', 'addAction');
    22  
    23  describe('Test Auxiliary Functions for Lock Display', () => {
    24      describe('Test isOutdated', () => {
    25          interface dataT {
    26              name: string;
    27              date: Date;
    28              expected: boolean;
    29          }
    30          const cases: dataT[] = [
    31              {
    32                  name: 'working for normal lock',
    33                  date: new Date(2022, 1, 1),
    34                  expected: false,
    35              },
    36              {
    37                  name: 'working for outdated lock',
    38                  date: new Date(2022, 0, 15),
    39                  expected: true,
    40              },
    41          ];
    42  
    43          describe.each(cases)(`Tests each isOutdated`, (testcase) => {
    44              beforeAll(() => {
    45                  jest.useFakeTimers();
    46                  jest.setSystemTime(new Date(2022, 1, 1));
    47              });
    48  
    49              afterAll(() => {
    50                  jest.useRealTimers();
    51              });
    52  
    53              it(testcase.name, () => {
    54                  expect(isOutdated(testcase.date)).toBe(testcase.expected);
    55              });
    56          });
    57      });
    58  });
    59  
    60  describe('Test delete lock button', () => {
    61      interface dataT {
    62          name: string;
    63          lock: DisplayLock;
    64          date: Date;
    65      }
    66      const lock = {
    67          environment: 'test-env',
    68          lockId: 'test-lock-id',
    69          message: 'test-lock-123',
    70      };
    71      const data: dataT[] = [
    72          {
    73              name: 'Test environment lock delete button',
    74              date: new Date(2022, 0, 2),
    75              lock: lock,
    76          },
    77          {
    78              name: 'Test application lock delete button',
    79              date: new Date(2022, 0, 2),
    80              lock: { ...lock, application: 'test-app' },
    81          },
    82      ];
    83  
    84      describe.each(data)('lock type', (testcase) => {
    85          it(testcase.name, () => {
    86              render(<LockDisplay lock={testcase.lock} />);
    87              const result = documentQuerySelectorSafe('.service-action--delete');
    88              act(() => {
    89                  result.click();
    90              });
    91              // then
    92              expect(JSON.stringify(mock_addAction.addAction.getAllCallArguments()[0][0])).toContain(
    93                  testcase.lock.lockId
    94              );
    95              if (testcase.lock.application) {
    96                  expect(JSON.stringify(mock_addAction.addAction.getAllCallArguments()[0][0])).toContain(
    97                      'deleteEnvironmentApplicationLock'
    98                  );
    99              } else {
   100                  expect(JSON.stringify(mock_addAction.addAction.getAllCallArguments()[0][0])).toContain(
   101                      'deleteEnvironmentLock'
   102                  );
   103              }
   104          });
   105      });
   106  });