All files / wrappers WeakWrapper.ts

100% Statements 26/26
100% Branches 6/6
100% Functions 10/10
100% Lines 23/23

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 55 56 57          24x 15x   15x   24x 3x     24x 2x                 24x 2x 4x         24x 12x   3x     9x   2x   9x       24x 10x     24x 2x 1x   1x   24x  
import toArray from '../utils/dom/toArray';
import each from '../utils/dom/each';
import lazy from '../utils/misc/lazy';
import Simulate, { FireEvent } from '../Simulate';
 
export default class WeakWrapper {
  private readonly elements = lazy(() => toArray(this.nodeList));
 
  constructor(private nodeList: NodeListOf<Element>) {}
 
  get instance(): null {
    return null;
  }
 
  get root() {
    return this.assertSingle();
  }
 
  /**
   * Simulate firing an event on all of the elements
   *
   * @param type event type
   * @param options options to override event object
   */
  simulate<T extends {}>(type: string, options?: T) {
    const fire = (Simulate as any)[type] as FireEvent | undefined;
    each(this.nodeList, node => fire!(node, options));
  }
 
  get(index: number): Element;
  get(): ReadonlyArray<Element>;
  get(index?: number): Element | ReadonlyArray<Element> {
    if (index === undefined) {
      // return all the elements
      return this.elements();
    } else {
      // return each element
      if (index < 0) {
        // ...in reversed order
        index = this.nodeList.length + index;
      }
      return this.nodeList.item(index);
    }
  }
 
  get length() {
    return this.nodeList.length;
  }
 
  private assertSingle() {
    if (this.nodeList.length !== 1) {
      throw new Error('Count of nodes must be one!');
    }
    return this.nodeList.item(0);
  }
}