github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/frontend-service/src/setupTests.ts (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 '@testing-library/jest-dom';
    17  import 'react-use-sub/test-util';
    18  import { Lock, Release } from './api/api';
    19  import { DisplayLock, UpdateFrontendConfig, UpdateOverview, updateTag } from './ui/utils/store';
    20  
    21  // test utility to await all running promises
    22  global.nextTick = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0));
    23  
    24  // CSS.supports is required a dependency of react-tooltip
    25  // @ts-ignore
    26  global.CSS = { supports: jest.fn() };
    27  
    28  export const documentQuerySelectorSafe = (selectors: string): HTMLElement => {
    29      const result = document.querySelector(selectors);
    30      if (!result) {
    31          throw new Error('documentQuerySelectorSafe: did not find in selector in document ' + selectors);
    32      }
    33      if (!(result instanceof HTMLElement)) {
    34          throw new Error(
    35              'documentQuerySelectorSafe: did find element in selector but it is not an html element: ' + selectors
    36          );
    37      }
    38      return result;
    39  };
    40  
    41  export const elementQuerySelectorSafe = (element: HTMLElement, selectors: string): HTMLElement => {
    42      const result = element.querySelector(selectors);
    43      if (!result) {
    44          throw new Error('elementQuerySelectorSafe: did not find in selector in element "' + selectors + '"');
    45      }
    46      if (!(result instanceof HTMLElement)) {
    47          throw new Error(
    48              'elementQuerySelectorSafe: did find element in selector but it is not an html element: "' + selectors + '"'
    49          );
    50      }
    51      return result;
    52  };
    53  
    54  export const getElementsByClassNameSafe = (element: HTMLElement, selectors: string): HTMLCollectionOf<Element> => {
    55      const result = element.getElementsByClassName(selectors);
    56      if (!result || result.length === 0) {
    57          throw new Error('getElementsByClassNameSafe: did not find in selector in element "' + selectors + '"');
    58      }
    59      return result;
    60  };
    61  
    62  export const makeRelease = (
    63      version: number,
    64      displayVersion: string = '',
    65      sourceCommitId: string = 'commit' + version,
    66      undeployVersion: boolean = false
    67  ): Release => ({
    68      version: version,
    69      sourceMessage: 'test' + version,
    70      sourceAuthor: 'test-author',
    71      sourceCommitId: sourceCommitId,
    72      displayVersion: displayVersion,
    73      createdAt: new Date(2002),
    74      undeployVersion: undeployVersion,
    75      prNumber: '666',
    76  });
    77  
    78  const date = new Date(2023, 6, 12);
    79  
    80  export const makeLock = (input: Partial<Lock>): Lock => ({
    81      lockId: 'l1',
    82      message: 'lock msg 1',
    83      createdAt: date,
    84      createdBy: {
    85          name: 'default',
    86          email: 'default@example.com',
    87      },
    88      ...input,
    89  });
    90  
    91  export const makeDisplayLock = (input: Partial<DisplayLock>): DisplayLock => ({
    92      lockId: 'l1',
    93      message: 'lock msg 1',
    94      environment: 'default-env',
    95      date: date,
    96      // application: 'default-app', // application should not be set here, because it cannot be overwritten with undefined
    97      authorEmail: 'default@example.com',
    98      authorName: 'default',
    99      ...input,
   100  });
   101  
   102  export const fakeLoadEverything = (load: boolean): void => {
   103      UpdateOverview.set({
   104          loaded: load,
   105      });
   106      UpdateFrontendConfig.set({
   107          configReady: load,
   108      });
   109      updateTag.set({
   110          tagsReady: load,
   111      });
   112  };