# All files / src/helpers deprecation.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 4/4
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54

    772x
    772x
    9264x

    9264x

    772x

    9264x

    9264x
    120x

    120x
    120x

    9264x

    48x

     import { getQueryPrefix } from './query-name';

    export function deprecateQueries<Queries extends Record<string, any>>(
      queriesObject: Queries,
      recommendation: string
    ): Queries {
      const result = {} as Queries;
      Object.keys(queriesObject).forEach((queryName) => {
        const queryFn = queriesObject[queryName];
        // @ts-expect-error: generic typing is hard
        result[queryName] = deprecateQuery(queryFn, queryName, recommendation);
      });

      return result;
    }

    function deprecateQuery<QueryFn extends (...args: any) => any>(
      queryFn: QueryFn,
      queryName: string,
      recommendation: string
    ): QueryFn {
      const formattedRecommendation = recommendation.replace(
        /{queryPrefix}/g,
        getQueryPrefix(queryName)
      );

      // @ts-expect-error: generic typing is hard
      const wrapper: QueryFn = (...args: any) => {
        const errorMessage = `${queryName}(...) is deprecated and will be removed in the future.\n\n${formattedRecommendation}`;
        // eslint-disable-next-line no-console
        console.warn(errorMessage);
        return queryFn(...args);
      };

      return wrapper;
    }

    const warned: { [functionName: string]: boolean } = {};

    /* istanbul ignore next: occasionally used */
    export function printDeprecationWarning(functionName: string) {
      if (warned[functionName]) {
        return;
      }

      // eslint-disable-next-line no-console
      console.warn(`
      Deprecation Warning:
      Use of ${functionName} is not recommended and will be deleted in future versions of @testing-library/react-native.
      `);

      warned[functionName] = true;
    }