2020-01-21 10:01:55 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-09 20:06:47 -04:00
|
|
|
import { unitTest, assert, assertEquals } from "./test_util.ts";
|
2018-10-23 07:43:43 -04:00
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
2018-10-23 07:43:43 -04:00
|
|
|
function setup() {
|
|
|
|
const dataSymbol = Symbol("data symbol");
|
|
|
|
class Base {
|
2020-03-28 13:03:49 -04:00
|
|
|
[dataSymbol] = new Map<string, number>();
|
2018-10-23 07:43:43 -04:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
data: Array<[string, number]> | IterableIterator<[string, number]>
|
|
|
|
) {
|
|
|
|
for (const [key, value] of data) {
|
|
|
|
this[dataSymbol].set(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
Base,
|
2019-02-13 08:50:15 -05:00
|
|
|
// This is using an internal API we don't want published as types, so having
|
|
|
|
// to cast to any to "trick" TypeScript
|
2020-02-19 15:36:18 -05:00
|
|
|
// @ts-ignore TypeScript (as of 3.7) does not support indexing namespaces by symbol
|
2020-03-28 13:03:49 -04:00
|
|
|
DomIterable: Deno[Deno.symbols.internal].DomIterableMixin(Base, dataSymbol),
|
2018-10-23 07:43:43 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function testDomIterable(): void {
|
2018-10-23 07:43:43 -04:00
|
|
|
const { DomIterable, Base } = setup();
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
const fixture: Array<[string, number]> = [
|
|
|
|
["foo", 1],
|
2020-03-28 13:03:49 -04:00
|
|
|
["bar", 2],
|
2019-11-13 13:42:34 -05:00
|
|
|
];
|
2018-10-23 07:43:43 -04:00
|
|
|
|
|
|
|
const domIterable = new DomIterable(fixture);
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(Array.from(domIterable.entries()), fixture);
|
|
|
|
assertEquals(Array.from(domIterable.values()), [1, 2]);
|
|
|
|
assertEquals(Array.from(domIterable.keys()), ["foo", "bar"]);
|
2018-10-23 07:43:43 -04:00
|
|
|
|
|
|
|
let result: Array<[string, number]> = [];
|
|
|
|
for (const [key, value] of domIterable) {
|
|
|
|
assert(key != null);
|
|
|
|
assert(value != null);
|
|
|
|
result.push([key, value]);
|
|
|
|
}
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fixture, result);
|
2018-10-23 07:43:43 -04:00
|
|
|
|
|
|
|
result = [];
|
|
|
|
const scope = {};
|
2020-02-19 15:36:18 -05:00
|
|
|
function callback(
|
|
|
|
this: typeof scope,
|
|
|
|
value: number,
|
|
|
|
key: string,
|
|
|
|
parent: typeof domIterable
|
|
|
|
): void {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(parent, domIterable);
|
2018-10-23 07:43:43 -04:00
|
|
|
assert(key != null);
|
|
|
|
assert(value != null);
|
|
|
|
assert(this === scope);
|
|
|
|
result.push([key, value]);
|
|
|
|
}
|
|
|
|
domIterable.forEach(callback, scope);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fixture, result);
|
2018-10-23 07:43:43 -04:00
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(DomIterable.name, Base.name);
|
2018-10-23 07:43:43 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function testDomIterableScope(): void {
|
2018-10-23 07:43:43 -04:00
|
|
|
const { DomIterable } = setup();
|
|
|
|
|
|
|
|
const domIterable = new DomIterable([["foo", 1]]);
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
function checkScope(thisArg: any, expected: any): void {
|
2020-02-19 15:36:18 -05:00
|
|
|
function callback(this: typeof thisArg): void {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(this, expected);
|
2018-10-23 07:43:43 -04:00
|
|
|
}
|
|
|
|
domIterable.forEach(callback, thisArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkScope(0, Object(0));
|
|
|
|
checkScope("", Object(""));
|
|
|
|
checkScope(null, window);
|
|
|
|
checkScope(undefined, window);
|
|
|
|
});
|