# All files / src/test-utils IntersectionObserver.ts

2% Statements 1/50
100% Branches 0/0
0% Functions 0/1
2% Lines 1/50

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
    511x

     export class IntersectionObserverMock implements IntersectionObserver {
      root: Document | Element | null;
      rootMargin: string;
      thresholds: readonly number[];

      elements: Set<Element>;

      constructor(
        public callback: IntersectionObserverCallback,
        options: IntersectionObserverInit = {}
      ) {
        this.root = options.root || null;
        this.rootMargin = options.rootMargin || "";
        this.thresholds =
          typeof options.threshold === "number"
            ? [options.threshold]
            : options.threshold ?? [];

        this.elements = new Set();
      }

      observe(target: Element): void {
        this.elements.add(target);

        this.callback(this.takeRecords(), this);
      }

      unobserve(target: Element): void {
        this.elements.delete(target);
      }

      takeRecords(): IntersectionObserverEntry[] {
        return [...this.elements].map<IntersectionObserverEntry>((target) => ({
          time: Date.now(),
          target,
          boundingClientRect: target.getBoundingClientRect(),
          intersectionRatio: 0,
          intersectionRect: target.getBoundingClientRect(),
          isIntersecting: false,
          rootBounds:
            this.root && "getBoundingClientRect" in this.root
              ? this.root.getBoundingClientRect()
              : null,
        }));
      }

      disconnect(): void {
        this.elements.clear();
      }
    }