summaryrefslogtreecommitdiff
path: root/cli/tests/unit/dom_iterable_test.ts
blob: 30599b6e651819a3c6dba54892afee57044d9fef (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

/* TODO https://github.com/denoland/deno/issues/7540
import { unitTest, assert, assertEquals } from "./test_util.ts";

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function setup() {
  const dataSymbol = Symbol("data symbol");
  class Base {
    [dataSymbol] = new Map<string, number>();

    constructor(
      data: Array<[string, number]> | IterableIterator<[string, number]>,
    ) {
      for (const [key, value] of data) {
        this[dataSymbol].set(key, value);
      }
    }
  }

  return {
    Base,
    // This is using an internal API we don't want published as types, so having
    // to cast to any to "trick" TypeScript
    // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
    DomIterable: Deno[Deno.internal].DomIterableMixin(Base, dataSymbol),
  };
}


unitTest(function testDomIterable(): void {
  const { DomIterable, Base } = setup();

  const fixture: Array<[string, number]> = [
    ["foo", 1],
    ["bar", 2],
  ];

  const domIterable = new DomIterable(fixture);

  assertEquals(Array.from(domIterable.entries()), fixture);
  assertEquals(Array.from(domIterable.values()), [1, 2]);
  assertEquals(Array.from(domIterable.keys()), ["foo", "bar"]);

  let result: Array<[string, number]> = [];
  for (const [key, value] of domIterable) {
    assert(key != null);
    assert(value != null);
    result.push([key, value]);
  }
  assertEquals(fixture, result);

  result = [];
  const scope = {};
  function callback(
    this: typeof scope,
    value: number,
    key: string,
    parent: typeof domIterable,
  ): void {
    assertEquals(parent, domIterable);
    assert(key != null);
    assert(value != null);
    assert(this === scope);
    result.push([key, value]);
  }
  domIterable.forEach(callback, scope);
  assertEquals(fixture, result);

  assertEquals(DomIterable.name, Base.name);
});

unitTest(function testDomIterableScope(): void {
  const { DomIterable } = setup();

  const domIterable = new DomIterable([["foo", 1]]);

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  function checkScope(thisArg: any, expected: any): void {
    function callback(this: typeof thisArg): void {
      assertEquals(this, expected);
    }
    domIterable.forEach(callback, thisArg);
  }

  checkScope(0, Object(0));
  checkScope("", Object(""));
  checkScope(null, window);
  checkScope(undefined, window);
});
*/