2015-02-27 14:50:06 -08:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2015-01-08 09:11:33 -08:00
|
|
|
|
2015-03-01 22:17:36 +01:00
|
|
|
var _global = typeof window === 'undefined' ? global : window;
|
|
|
|
|
2015-02-10 07:39:29 +01:00
|
|
|
export {proxy} from 'rtts_assert/rtts_assert';
|
2015-03-01 22:17:36 +01:00
|
|
|
export var describe = _global.describe;
|
|
|
|
export var xdescribe = _global.xdescribe;
|
|
|
|
export var ddescribe = _global.ddescribe;
|
|
|
|
export var it = _global.it;
|
|
|
|
export var xit = _global.xit;
|
|
|
|
export var iit = _global.iit;
|
|
|
|
export var beforeEach = _global.beforeEach;
|
|
|
|
export var afterEach = _global.afterEach;
|
|
|
|
export var expect = _global.expect;
|
2014-11-07 09:31:51 -08:00
|
|
|
export var IS_DARTIUM = false;
|
2015-03-01 22:17:36 +01:00
|
|
|
export var IS_NODEJS = typeof window === 'undefined';
|
2014-09-25 14:04:46 -07:00
|
|
|
|
|
|
|
// To make testing consistent between dart and js
|
2015-03-01 22:17:36 +01:00
|
|
|
_global.print = function(msg) {
|
|
|
|
if (_global.dump) {
|
|
|
|
_global.dump(msg);
|
2014-09-26 17:38:38 -07:00
|
|
|
} else {
|
2015-03-01 22:17:36 +01:00
|
|
|
_global.console.log(msg);
|
2014-09-26 17:38:38 -07:00
|
|
|
}
|
|
|
|
};
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-01-27 14:52:15 -08:00
|
|
|
// 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 :-(
|
2015-03-01 22:17:36 +01:00
|
|
|
_global.Map.prototype.jasmineToString = function() {
|
2015-01-27 14:52:15 -08:00
|
|
|
var m = this;
|
|
|
|
if (!m) {
|
|
|
|
return ''+m;
|
|
|
|
}
|
|
|
|
var res = [];
|
|
|
|
m.forEach( (v,k) => {
|
|
|
|
res.push(`${k}:${v}`);
|
|
|
|
});
|
|
|
|
return `{ ${res.join(',')} }`;
|
|
|
|
}
|
|
|
|
|
2015-03-01 22:17:36 +01:00
|
|
|
_global.beforeEach(function() {
|
2014-09-30 14:56:33 -04:00
|
|
|
jasmine.addMatchers({
|
2015-01-27 14:52:15 -08:00
|
|
|
// Custom handler for Map as Jasmine does not support it yet
|
2014-11-17 17:39:39 -08:00
|
|
|
toEqual: function(util, customEqualityTesters) {
|
|
|
|
return {
|
|
|
|
compare: function(actual, expected) {
|
2015-01-27 14:52:15 -08:00
|
|
|
return {
|
|
|
|
pass: util.equals(actual, expected, [compareMap])
|
|
|
|
};
|
2014-11-17 17:39:39 -08:00
|
|
|
}
|
|
|
|
};
|
2015-01-27 14:52:15 -08:00
|
|
|
|
|
|
|
function compareMap(actual, expected) {
|
|
|
|
if (actual instanceof Map) {
|
|
|
|
var pass = actual.size === expected.size;
|
|
|
|
if (pass) {
|
|
|
|
actual.forEach( (v,k) => {
|
|
|
|
pass = pass && util.equals(v, expected.get(k));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return pass;
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 17:39:39 -08:00
|
|
|
},
|
|
|
|
|
2014-10-10 15:44:56 -04:00
|
|
|
toBePromise: function() {
|
2014-09-30 14:56:33 -04:00
|
|
|
return {
|
|
|
|
compare: function (actual, expectedClass) {
|
|
|
|
var pass = typeof actual === 'object' && typeof actual.then === 'function';
|
|
|
|
return {
|
|
|
|
pass: pass,
|
|
|
|
get message() {
|
2014-10-10 15:44:56 -04:00
|
|
|
return 'Expected ' + actual + ' to be a promise';
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
toBeAnInstanceOf: function() {
|
|
|
|
return {
|
|
|
|
compare: function(actual, expectedClass) {
|
|
|
|
var pass = typeof actual === 'object' && actual instanceof expectedClass;
|
|
|
|
return {
|
|
|
|
pass: pass,
|
|
|
|
get message() {
|
|
|
|
return 'Expected ' + actual + ' to be an instance of ' + expectedClass;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2014-11-25 15:16:53 -08:00
|
|
|
},
|
|
|
|
|
2015-01-08 09:11:33 -08:00
|
|
|
toHaveText: function() {
|
|
|
|
return {
|
|
|
|
compare: function(actual, expectedText) {
|
|
|
|
var actualText = elementText(actual);
|
|
|
|
return {
|
|
|
|
pass: actualText == expectedText,
|
|
|
|
get message() {
|
|
|
|
return 'Expected ' + actualText + ' to be equal to ' + expectedText;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2014-11-25 15:16:53 -08:00
|
|
|
toImplement: function() {
|
|
|
|
return {
|
|
|
|
compare: function(actualObject, expectedInterface) {
|
|
|
|
var objProps = Object.keys(actualObject.constructor.prototype);
|
|
|
|
var intProps = Object.keys(expectedInterface.prototype);
|
|
|
|
|
|
|
|
var missedMethods = [];
|
|
|
|
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(", ");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-11-17 17:39:39 -08:00
|
|
|
|
2014-11-25 15:16:53 -08:00
|
|
|
export class SpyObject {
|
|
|
|
spy(name){
|
|
|
|
if (! this[name]) {
|
|
|
|
this[name] = this._createGuinnessCompatibleSpy();
|
|
|
|
}
|
|
|
|
return this[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
rttsAssert(value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createGuinnessCompatibleSpy(){
|
|
|
|
var newSpy = jasmine.createSpy();
|
|
|
|
newSpy.andCallFake = newSpy.and.callFake;
|
|
|
|
return newSpy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-08 09:11:33 -08:00
|
|
|
function elementText(n) {
|
2015-03-01 22:17:36 +01:00
|
|
|
if (!IS_NODEJS) {
|
|
|
|
var hasNodes = (n) => {var children = DOM.childNodes(n); return children && children.length > 0;}
|
2015-01-08 09:11:33 -08:00
|
|
|
|
2015-03-01 22:17:36 +01:00
|
|
|
if (n instanceof Comment) return '';
|
2015-01-08 09:11:33 -08:00
|
|
|
|
2015-03-01 22:17:36 +01:00
|
|
|
if (n instanceof Array) return n.map((nn) => elementText(nn)).join("");
|
|
|
|
if (n instanceof Element && DOM.tagName(n) == 'CONTENT')
|
|
|
|
return elementText(Array.prototype.slice.apply(n.getDistributedNodes()));
|
|
|
|
if (DOM.hasShadowRoot(n)) return elementText(DOM.childNodesAsList(n.shadowRoot));
|
|
|
|
if (hasNodes(n)) return elementText(DOM.childNodesAsList(n));
|
2015-01-08 09:11:33 -08:00
|
|
|
|
2015-03-01 22:17:36 +01:00
|
|
|
return n.textContent;
|
|
|
|
} else {
|
|
|
|
if (DOM.hasShadowRoot(n)) {
|
|
|
|
return elementText(DOM.getShadowRoot(n).childNodes);
|
|
|
|
} else if (n instanceof Array) {
|
|
|
|
return n.map((nn) => elementText(nn)).join("");
|
|
|
|
} else if (DOM.tagName(n) == 'content') {
|
|
|
|
//minimal implementation of getDistributedNodes()
|
|
|
|
var host = null;
|
|
|
|
var temp = n;
|
|
|
|
while (temp.parent) {
|
|
|
|
if (DOM.hasShadowRoot(temp)) {
|
|
|
|
host = temp;
|
|
|
|
}
|
|
|
|
temp = temp.parent;
|
|
|
|
}
|
|
|
|
if (host) {
|
|
|
|
var list = [];
|
|
|
|
var select = DOM.getAttribute(n, "select");
|
|
|
|
var selectClass = select? select.substring(1): null;
|
|
|
|
DOM.childNodes(host).forEach((child) => {
|
|
|
|
var classList = DOM.classList(child);
|
|
|
|
if (selectClass && classList.indexOf(selectClass) > -1 || selectClass == null && classList.length == 0) {
|
|
|
|
list.push(child);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return list.length > 0? elementText(list): "";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return DOM.getText(n);
|
|
|
|
}
|
|
|
|
}
|
2015-02-27 14:50:06 -08:00
|
|
|
}
|