deno/js/mixins/dom_iterable_test.ts
Ryan Dahl c42a9d7370
Upgrade deno_std (#1892)
A major API change was that asserts are imported from testing/asserts.ts
now rather than testing/mod.ts and assertEqual as renamed to
assertEquals to conform to what is most common in JavaScript.
2019-03-06 20:48:46 -05:00

81 lines
2.2 KiB
TypeScript

// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEquals } from "../test_util.ts";
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,
// This is using an internal API we don't want published as types, so having
// to cast to any to "trick" TypeScript
// tslint:disable-next-line:no-any
DomIterable: (Deno as any).DomIterableMixin(Base, dataSymbol)
};
}
test(function testDomIterable() {
// tslint:disable-next-line:variable-name
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(value, key, parent) {
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);
});
test(function testDomIterableScope() {
// tslint:disable-next-line:variable-name
const { DomIterable } = setup();
const domIterable = new DomIterable([["foo", 1]]);
// tslint:disable-next-line:no-any
function checkScope(thisArg: any, expected: any) {
function callback() {
assertEquals(this, expected);
}
domIterable.forEach(callback, thisArg);
}
checkScope(0, Object(0));
checkScope("", Object(""));
checkScope(null, window);
checkScope(undefined, window);
});