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-02-10 07:39:29 +01:00
|
|
|
export {proxy} from 'rtts_assert/rtts_assert';
|
2014-09-25 14:04:46 -07:00
|
|
|
export var describe = window.describe;
|
2014-10-01 22:05:46 -07:00
|
|
|
export var xdescribe = window.xdescribe;
|
2014-09-28 13:55:01 -07:00
|
|
|
export var ddescribe = window.ddescribe;
|
2014-09-25 14:04:46 -07:00
|
|
|
export var it = window.it;
|
2014-10-01 22:05:46 -07:00
|
|
|
export var xit = window.xit;
|
2014-09-28 13:55:01 -07:00
|
|
|
export var iit = window.iit;
|
2014-09-25 14:04:46 -07:00
|
|
|
export var beforeEach = window.beforeEach;
|
|
|
|
export var afterEach = window.afterEach;
|
|
|
|
export var expect = window.expect;
|
2014-11-07 09:31:51 -08:00
|
|
|
export var IS_DARTIUM = false;
|
2014-09-25 14:04:46 -07:00
|
|
|
|
|
|
|
// To make testing consistent between dart and js
|
2014-09-26 17:38:38 -07:00
|
|
|
window.print = function(msg) {
|
|
|
|
if (window.dump) {
|
|
|
|
window.dump(msg);
|
|
|
|
} else {
|
|
|
|
window.console.log(msg);
|
|
|
|
}
|
|
|
|
};
|
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 :-(
|
|
|
|
window.Map.prototype.jasmineToString = function() {
|
|
|
|
var m = this;
|
|
|
|
if (!m) {
|
|
|
|
return ''+m;
|
|
|
|
}
|
|
|
|
var res = [];
|
|
|
|
m.forEach( (v,k) => {
|
|
|
|
res.push(`${k}:${v}`);
|
|
|
|
});
|
|
|
|
return `{ ${res.join(',')} }`;
|
|
|
|
}
|
|
|
|
|
2014-09-30 14:56:33 -04:00
|
|
|
window.beforeEach(function() {
|
|
|
|
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-02-19 11:18:23 +01:00
|
|
|
var hasNodes = (n) => {var children = DOM.childNodes(n); return children && children.length > 0;}
|
2015-01-08 09:11:33 -08:00
|
|
|
|
|
|
|
if (n instanceof Comment) return '';
|
|
|
|
|
|
|
|
if (n instanceof Array) return n.map((nn) => elementText(nn)).join("");
|
2015-02-19 11:18:23 +01:00
|
|
|
if (n instanceof Element && DOM.tagName(n) == 'CONTENT')
|
2015-01-08 09:11:33 -08:00
|
|
|
return elementText(Array.prototype.slice.apply(n.getDistributedNodes()));
|
2015-02-27 14:50:06 -08:00
|
|
|
if (DOM.hasShadowRoot(n)) return elementText(DOM.childNodesAsList(n.shadowRoot));
|
2015-01-08 09:11:33 -08:00
|
|
|
if (hasNodes(n)) return elementText(DOM.childNodesAsList(n));
|
|
|
|
|
|
|
|
return n.textContent;
|
2015-02-27 14:50:06 -08:00
|
|
|
}
|