angular-docs-cn/modules/@angular/platform-browser/testing/matchers.ts

274 lines
7.3 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
2016-10-19 16:42:39 -04:00
import {global} from './facade/lang';
import {getDOM} from './private_import_platform-browser';
/**
* Jasmine matchers that check Angular specific conditions.
*/
export interface NgMatchers extends jasmine.Matchers {
/**
* Expect the value to be a `Promise`.
*
* ## Example
*
* {@example testing/ts/matchers.ts region='toBePromise'}
*/
toBePromise(): boolean;
/**
* Expect the value to be an instance of a class.
*
* ## Example
*
* {@example testing/ts/matchers.ts region='toBeAnInstanceOf'}
*/
toBeAnInstanceOf(expected: any): boolean;
/**
* Expect the element to have exactly the given text.
*
* ## Example
*
* {@example testing/ts/matchers.ts region='toHaveText'}
*/
2016-07-29 15:20:52 -04:00
toHaveText(expected: string): boolean;
/**
* Expect the element to have the given CSS class.
*
* ## Example
*
* {@example testing/ts/matchers.ts region='toHaveCssClass'}
*/
2016-07-29 15:20:52 -04:00
toHaveCssClass(expected: string): boolean;
/**
* Expect the element to have the given CSS styles.
*
* ## Example
*
* {@example testing/ts/matchers.ts region='toHaveCssStyle'}
*/
2016-07-29 15:20:52 -04:00
toHaveCssStyle(expected: {[k: string]: string}|string): boolean;
/**
* Expect a class to implement the interface of the given class.
*
* ## Example
*
* {@example testing/ts/matchers.ts region='toImplement'}
*/
toImplement(expected: any): boolean;
/**
* Expect an exception to contain the given error text.
*
* ## Example
*
* {@example testing/ts/matchers.ts region='toContainError'}
*/
toContainError(expected: any): boolean;
/**
* Invert the matchers.
*/
not: NgMatchers;
}
2016-07-29 15:20:52 -04:00
const _global = <any>(typeof window === 'undefined' ? global : window);
/**
* Jasmine matching function with Angular matchers mixed in.
*
* ## Example
*
* {@example testing/ts/matchers.ts region='toHaveText'}
*/
2016-07-29 15:20:52 -04:00
export const expect: (actual: any) => NgMatchers = <any>_global.expect;
// Some Map polyfills don't polyfill Map.toString correctly, which
// gives us bad error messages in tests.
// The only way to do this in Jasmine is to monkey patch a method
// to the object :-(
2016-07-29 15:20:52 -04:00
(Map as any).prototype['jasmineToString'] = function() {
const m = this;
if (!m) {
return '' + m;
}
2016-07-29 15:20:52 -04:00
const res: any[] = [];
m.forEach((v: any, k: any) => { res.push(`${k}:${v}`); });
return `{ ${res.join(',')} }`;
};
_global.beforeEach(function() {
jasmine.addMatchers({
// Custom handler for Map as Jasmine does not support it yet
2016-07-29 15:20:52 -04:00
toEqual: function(util) {
return {
2016-07-29 15:20:52 -04:00
compare: function(actual: any, expected: any) {
return {pass: util.equals(actual, expected, [compareMap])};
}
};
2016-07-29 15:20:52 -04:00
function compareMap(actual: any, expected: any) {
if (actual instanceof Map) {
2016-07-29 15:20:52 -04:00
let pass = actual.size === expected.size;
if (pass) {
2016-07-29 15:20:52 -04:00
actual.forEach((v: any, k: any) => { pass = pass && util.equals(v, expected.get(k)); });
}
return pass;
} else {
return undefined;
}
}
},
toBePromise: function() {
return {
2016-07-29 15:20:52 -04:00
compare: function(actual: any) {
const pass = typeof actual === 'object' && typeof actual.then === 'function';
return {pass: pass, get message() { return 'Expected ' + actual + ' to be a promise'; }};
}
};
},
toBeAnInstanceOf: function() {
return {
2016-07-29 15:20:52 -04:00
compare: function(actual: any, expectedClass: any) {
const pass = typeof actual === 'object' && actual instanceof expectedClass;
return {
pass: pass,
get message() {
return 'Expected ' + actual + ' to be an instance of ' + expectedClass;
}
};
}
};
},
toHaveText: function() {
return {
2016-07-29 15:20:52 -04:00
compare: function(actual: any, expectedText: string) {
const actualText = elementText(actual);
return {
pass: actualText == expectedText,
get message() { return 'Expected ' + actualText + ' to be equal to ' + expectedText; }
};
}
};
},
toHaveCssClass: function() {
return {compare: buildError(false), negativeCompare: buildError(true)};
2016-07-29 15:20:52 -04:00
function buildError(isNot: boolean) {
return function(actual: any, className: string) {
return {
pass: getDOM().hasClass(actual, className) == !isNot,
get message() {
return `Expected ${actual.outerHTML} ${isNot ? 'not ' : ''}to contain the CSS class "${className}"`;
}
};
};
}
},
toHaveCssStyle: function() {
return {
2016-07-29 15:20:52 -04:00
compare: function(actual: any, styles: {[k: string]: string}|string) {
let allPassed: boolean;
2016-10-19 16:42:39 -04:00
if (typeof styles === 'string') {
allPassed = getDOM().hasStyle(actual, styles);
} else {
allPassed = Object.keys(styles).length !== 0;
Object.keys(styles).forEach(prop => {
allPassed = allPassed && getDOM().hasStyle(actual, prop, styles[prop]);
2016-07-29 15:20:52 -04:00
});
}
return {
pass: allPassed,
get message() {
2016-10-19 16:42:39 -04:00
const expectedValueStr = typeof styles === 'string' ? styles : JSON.stringify(styles);
return `Expected ${actual.outerHTML} ${!allPassed ? ' ' : 'not '}to contain the
2016-10-19 16:42:39 -04:00
CSS ${typeof styles === 'string' ? 'property' : 'styles'} "${expectedValueStr}"`;
}
};
}
};
},
toContainError: function() {
return {
2016-07-29 15:20:52 -04:00
compare: function(actual: any, expectedText: any) {
const errorMessage = actual.toString();
return {
pass: errorMessage.indexOf(expectedText) > -1,
get message() { return 'Expected ' + errorMessage + ' to contain ' + expectedText; }
};
}
};
},
toImplement: function() {
return {
2016-07-29 15:20:52 -04:00
compare: function(actualObject: any, expectedInterface: any) {
const intProps = Object.keys(expectedInterface.prototype);
2016-07-29 15:20:52 -04:00
const missedMethods: any[] = [];
intProps.forEach((k) => {
if (!actualObject.constructor.prototype[k]) missedMethods.push(k);
});
return {
pass: missedMethods.length == 0,
get message() {
return 'Expected ' + actualObject + ' to have the following methods: ' +
missedMethods.join(', ');
}
};
}
};
}
});
});
2016-07-29 15:20:52 -04:00
function elementText(n: any): string {
const hasNodes = (n: any) => {
2016-07-29 15:20:52 -04:00
const children = getDOM().childNodes(n);
return children && children.length > 0;
};
if (n instanceof Array) {
return n.map(elementText).join('');
}
if (getDOM().isCommentNode(n)) {
return '';
}
if (getDOM().isElementNode(n) && getDOM().tagName(n) == 'CONTENT') {
return elementText(Array.prototype.slice.apply(getDOM().getDistributedNodes(n)));
}
if (getDOM().hasShadowRoot(n)) {
return elementText(getDOM().childNodesAsList(getDOM().getShadowRoot(n)));
}
if (hasNodes(n)) {
return elementText(getDOM().childNodesAsList(n));
}
return getDOM().getText(n);
}