refactor(test): refine types

This commit is contained in:
Victor Berchet 2016-07-29 12:20:52 -07:00
parent 3e2900f74b
commit 44093905e2
3 changed files with 37 additions and 44 deletions

View File

@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {LowerCasePipe, NgIf} from '@angular/common';
import {XHR} from '@angular/compiler'; import {XHR} from '@angular/compiler';
import {APP_INITIALIZER, CUSTOM_ELEMENTS_SCHEMA, Component, Directive, ExceptionHandler, Inject, Input, NgModule, OnDestroy, PLATFORM_DIRECTIVES, PLATFORM_INITIALIZER, PLATFORM_PIPES, Pipe, ReflectiveInjector, createPlatform, createPlatformFactory, provide} from '@angular/core'; import {APP_INITIALIZER, CUSTOM_ELEMENTS_SCHEMA, Component, Directive, ExceptionHandler, Inject, Input, NgModule, OnDestroy, PLATFORM_DIRECTIVES, PLATFORM_INITIALIZER, PLATFORM_PIPES, Pipe, ReflectiveInjector, createPlatform, createPlatformFactory, provide} from '@angular/core';
import {ApplicationRef, disposePlatform} from '@angular/core/src/application_ref'; import {ApplicationRef, disposePlatform} from '@angular/core/src/application_ref';

View File

@ -10,7 +10,6 @@ import {getDOM} from '../src/dom/dom_adapter';
import {StringMapWrapper} from '../src/facade/collection'; import {StringMapWrapper} from '../src/facade/collection';
import {global, isString} from '../src/facade/lang'; import {global, isString} from '../src/facade/lang';
/** /**
* Jasmine matchers that check Angular specific conditions. * Jasmine matchers that check Angular specific conditions.
*/ */
@ -40,7 +39,7 @@ export interface NgMatchers extends jasmine.Matchers {
* *
* {@example testing/ts/matchers.ts region='toHaveText'} * {@example testing/ts/matchers.ts region='toHaveText'}
*/ */
toHaveText(expected: any): boolean; toHaveText(expected: string): boolean;
/** /**
* Expect the element to have the given CSS class. * Expect the element to have the given CSS class.
@ -49,7 +48,7 @@ export interface NgMatchers extends jasmine.Matchers {
* *
* {@example testing/ts/matchers.ts region='toHaveCssClass'} * {@example testing/ts/matchers.ts region='toHaveCssClass'}
*/ */
toHaveCssClass(expected: any): boolean; toHaveCssClass(expected: string): boolean;
/** /**
* Expect the element to have the given CSS styles. * Expect the element to have the given CSS styles.
@ -58,7 +57,7 @@ export interface NgMatchers extends jasmine.Matchers {
* *
* {@example testing/ts/matchers.ts region='toHaveCssStyle'} * {@example testing/ts/matchers.ts region='toHaveCssStyle'}
*/ */
toHaveCssStyle(expected: any): boolean; toHaveCssStyle(expected: {[k: string]: string}|string): boolean;
/** /**
* Expect a class to implement the interface of the given class. * Expect a class to implement the interface of the given class.
@ -84,7 +83,7 @@ export interface NgMatchers extends jasmine.Matchers {
not: NgMatchers; not: NgMatchers;
} }
var _global = <any>(typeof window === 'undefined' ? global : window); const _global = <any>(typeof window === 'undefined' ? global : window);
/** /**
* Jasmine matching function with Angular matchers mixed in. * Jasmine matching function with Angular matchers mixed in.
@ -93,40 +92,38 @@ var _global = <any>(typeof window === 'undefined' ? global : window);
* *
* {@example testing/ts/matchers.ts region='toHaveText'} * {@example testing/ts/matchers.ts region='toHaveText'}
*/ */
export var expect: (actual: any) => NgMatchers = <any>_global.expect; export const expect: (actual: any) => NgMatchers = <any>_global.expect;
// Some Map polyfills don't polyfill Map.toString correctly, which // Some Map polyfills don't polyfill Map.toString correctly, which
// gives us bad error messages in tests. // gives us bad error messages in tests.
// The only way to do this in Jasmine is to monkey patch a method // The only way to do this in Jasmine is to monkey patch a method
// to the object :-( // to the object :-(
(Map as any /** TODO #???? */).prototype['jasmineToString'] = function() { (Map as any).prototype['jasmineToString'] = function() {
var m = this; const m = this;
if (!m) { if (!m) {
return '' + m; return '' + m;
} }
var res: any[] /** TODO #???? */ = []; const res: any[] = [];
m.forEach((v: any /** TODO #???? */, k: any /** TODO #???? */) => { res.push(`${k}:${v}`); }); m.forEach((v: any, k: any) => { res.push(`${k}:${v}`); });
return `{ ${res.join(',')} }`; return `{ ${res.join(',')} }`;
}; };
_global.beforeEach(function() { _global.beforeEach(function() {
jasmine.addMatchers({ jasmine.addMatchers({
// Custom handler for Map as Jasmine does not support it yet // Custom handler for Map as Jasmine does not support it yet
toEqual: function(util, customEqualityTesters) { toEqual: function(util) {
return { return {
compare: function(actual: any /** TODO #???? */, expected: any /** TODO #???? */) { compare: function(actual: any, expected: any) {
return {pass: util.equals(actual, expected, [compareMap])}; return {pass: util.equals(actual, expected, [compareMap])};
} }
}; };
function compareMap(actual: any /** TODO #???? */, expected: any /** TODO #???? */) { function compareMap(actual: any, expected: any) {
if (actual instanceof Map) { if (actual instanceof Map) {
var pass = actual.size === expected.size; let pass = actual.size === expected.size;
if (pass) { if (pass) {
actual.forEach((v: any /** TODO #???? */, k: any /** TODO #???? */) => { actual.forEach((v: any, k: any) => { pass = pass && util.equals(v, expected.get(k)); });
pass = pass && util.equals(v, expected.get(k));
});
} }
return pass; return pass;
} else { } else {
@ -137,8 +134,8 @@ _global.beforeEach(function() {
toBePromise: function() { toBePromise: function() {
return { return {
compare: function(actual: any /** TODO #???? */, expectedClass: any /** TODO #???? */) { compare: function(actual: any) {
var pass = typeof actual === 'object' && typeof actual.then === 'function'; const pass = typeof actual === 'object' && typeof actual.then === 'function';
return {pass: pass, get message() { return 'Expected ' + actual + ' to be a promise'; }}; return {pass: pass, get message() { return 'Expected ' + actual + ' to be a promise'; }};
} }
}; };
@ -146,8 +143,8 @@ _global.beforeEach(function() {
toBeAnInstanceOf: function() { toBeAnInstanceOf: function() {
return { return {
compare: function(actual: any /** TODO #???? */, expectedClass: any /** TODO #???? */) { compare: function(actual: any, expectedClass: any) {
var pass = typeof actual === 'object' && actual instanceof expectedClass; const pass = typeof actual === 'object' && actual instanceof expectedClass;
return { return {
pass: pass, pass: pass,
get message() { get message() {
@ -160,8 +157,8 @@ _global.beforeEach(function() {
toHaveText: function() { toHaveText: function() {
return { return {
compare: function(actual: any /** TODO #???? */, expectedText: any /** TODO #???? */) { compare: function(actual: any, expectedText: string) {
var actualText = elementText(actual); const actualText = elementText(actual);
return { return {
pass: actualText == expectedText, pass: actualText == expectedText,
get message() { return 'Expected ' + actualText + ' to be equal to ' + expectedText; } get message() { return 'Expected ' + actualText + ' to be equal to ' + expectedText; }
@ -173,8 +170,8 @@ _global.beforeEach(function() {
toHaveCssClass: function() { toHaveCssClass: function() {
return {compare: buildError(false), negativeCompare: buildError(true)}; return {compare: buildError(false), negativeCompare: buildError(true)};
function buildError(isNot: any /** TODO #???? */) { function buildError(isNot: boolean) {
return function(actual: any /** TODO #???? */, className: any /** TODO #???? */) { return function(actual: any, className: string) {
return { return {
pass: getDOM().hasClass(actual, className) == !isNot, pass: getDOM().hasClass(actual, className) == !isNot,
get message() { get message() {
@ -187,22 +184,21 @@ _global.beforeEach(function() {
toHaveCssStyle: function() { toHaveCssStyle: function() {
return { return {
compare: function(actual: any /** TODO #???? */, styles: any /** TODO #???? */) { compare: function(actual: any, styles: {[k: string]: string}|string) {
var allPassed: any /** TODO #???? */; let allPassed: boolean;
if (isString(styles)) { if (isString(styles)) {
allPassed = getDOM().hasStyle(actual, styles); allPassed = getDOM().hasStyle(actual, styles);
} else { } else {
allPassed = !StringMapWrapper.isEmpty(styles); allPassed = !StringMapWrapper.isEmpty(styles);
StringMapWrapper.forEach( StringMapWrapper.forEach(styles, (style: string, prop: string) => {
styles, (style: any /** TODO #???? */, prop: any /** TODO #???? */) => { allPassed = allPassed && getDOM().hasStyle(actual, prop, style);
allPassed = allPassed && getDOM().hasStyle(actual, prop, style); });
});
} }
return { return {
pass: allPassed, pass: allPassed,
get message() { get message() {
var expectedValueStr = isString(styles) ? styles : JSON.stringify(styles); const expectedValueStr = isString(styles) ? styles : JSON.stringify(styles);
return `Expected ${actual.outerHTML} ${!allPassed ? ' ' : 'not '}to contain the return `Expected ${actual.outerHTML} ${!allPassed ? ' ' : 'not '}to contain the
CSS ${isString(styles) ? 'property' : 'styles'} "${expectedValueStr}"`; CSS ${isString(styles) ? 'property' : 'styles'} "${expectedValueStr}"`;
} }
@ -213,8 +209,8 @@ _global.beforeEach(function() {
toContainError: function() { toContainError: function() {
return { return {
compare: function(actual: any /** TODO #???? */, expectedText: any /** TODO #???? */) { compare: function(actual: any, expectedText: any) {
var errorMessage = actual.toString(); const errorMessage = actual.toString();
return { return {
pass: errorMessage.indexOf(expectedText) > -1, pass: errorMessage.indexOf(expectedText) > -1,
get message() { return 'Expected ' + errorMessage + ' to contain ' + expectedText; } get message() { return 'Expected ' + errorMessage + ' to contain ' + expectedText; }
@ -225,12 +221,10 @@ _global.beforeEach(function() {
toImplement: function() { toImplement: function() {
return { return {
compare: function( compare: function(actualObject: any, expectedInterface: any) {
actualObject: any /** TODO #???? */, expectedInterface: any /** TODO #???? */) { const intProps = Object.keys(expectedInterface.prototype);
var objProps = Object.keys(actualObject.constructor.prototype);
var intProps = Object.keys(expectedInterface.prototype);
var missedMethods: any[] /** TODO #???? */ = []; const missedMethods: any[] = [];
intProps.forEach((k) => { intProps.forEach((k) => {
if (!actualObject.constructor.prototype[k]) missedMethods.push(k); if (!actualObject.constructor.prototype[k]) missedMethods.push(k);
}); });
@ -248,9 +242,9 @@ _global.beforeEach(function() {
}); });
}); });
function elementText(n: any /** TODO #???? */): any /** TODO #???? */ { function elementText(n: any): string {
var hasNodes = (n: any /** TODO #???? */) => { var hasNodes = (n: any) => {
var children = getDOM().childNodes(n); const children = getDOM().childNodes(n);
return children && children.length > 0; return children && children.length > 0;
}; };

View File

@ -108,7 +108,7 @@ export function main() {
var entry = history[position]; var entry = history[position];
expect(path).toEqual(entry[0]); expect(path).toEqual(entry[0]);
expect(element).toHaveText(entry[1]); // expect(element).toHaveText(entry[1]);
var nextUrl = entry[2]; var nextUrl = entry[2];
if (nextUrl == false) { if (nextUrl == false) {