1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/js/mixins/dom_iterable_test.ts

80 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-01-21 14:03:30 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEquals } from "../test_util.ts";
2018-10-23 07:43:43 -04: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 {
private [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,
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
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2019-02-13 08:50:15 -05:00
DomIterable: (Deno as any).DomIterableMixin(Base, dataSymbol)
2018-10-23 07:43:43 -04:00
};
}
test(function testDomIterable() {
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"]);
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]);
}
assertEquals(fixture, result);
2018-10-23 07:43:43 -04:00
result = [];
const scope = {};
function callback(value, key, parent): void {
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);
assertEquals(fixture, result);
2018-10-23 07:43:43 -04:00
assertEquals(DomIterable.name, Base.name);
2018-10-23 07:43:43 -04:00
});
test(function testDomIterableScope() {
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(): void {
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);
});