# All files / src waitForElementToBeRemoved.ts

100% Statements 12/12
100% Branches 7/7
100% Functions 3/3
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

    51x

    10x

    10x
    10x
    1x

    9x

    44x
    44x

    3x

    41x
    39x

    2x

     import waitFor from './waitFor';
    import type { WaitForOptions } from './waitFor';
    import { ErrorWithStack } from './helpers/errors';

    function isRemoved<T>(result: T): boolean {
      return !result || (Array.isArray(result) && !result.length);
    }

    export default async function waitForElementToBeRemoved<T>(
      expectation: () => T,
      options?: WaitForOptions
    ): Promise<T> {
      // Created here so we get a nice stacktrace
      const timeoutError = new ErrorWithStack(
        'Timed out in waitForElementToBeRemoved.',
        waitForElementToBeRemoved
      );

      // Elements have to be present initally and then removed.
      const initialElements = expectation();
      if (isRemoved(initialElements)) {
        throw new ErrorWithStack(
          'The element(s) given to waitForElementToBeRemoved are already removed. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal.',
          waitForElementToBeRemoved
        );
      }

      return waitFor(() => {
        let result;
        try {
          result = expectation();
        } catch (error) {
          return initialElements;
        }

        if (!isRemoved(result)) {
          throw timeoutError;
        }

        return initialElements;
      }, options);
    }