angular2
Version:
Angular 2 - a web framework for modern web apps
234 lines • 7.85 kB
JavaScript
;/**
* Public Test Library for unit testing Angular2 Applications. Uses the
* Jasmine framework.
*/
var lang_1 = require('angular2/src/facade/lang');
var collection_1 = require('angular2/src/facade/collection');
var test_injector_1 = require('./test_injector');
var test_injector_2 = require('./test_injector');
exports.inject = test_injector_2.inject;
exports.injectAsync = test_injector_2.injectAsync;
var matchers_1 = require('./matchers');
exports.expect = matchers_1.expect;
var _global = (typeof window === 'undefined' ? lang_1.global : window);
/**
* See http://jasmine.github.io/
*/
exports.afterEach = _global.afterEach;
/**
* See http://jasmine.github.io/
*/
exports.describe = _global.describe;
/**
* See http://jasmine.github.io/
*/
exports.ddescribe = _global.fdescribe;
/**
* See http://jasmine.github.io/
*/
exports.fdescribe = _global.fdescribe;
/**
* See http://jasmine.github.io/
*/
exports.xdescribe = _global.xdescribe;
var jsmBeforeEach = _global.beforeEach;
var jsmIt = _global.it;
var jsmIIt = _global.fit;
var jsmXIt = _global.xit;
var testProviders;
var injector;
// Reset the test providers before each test.
jsmBeforeEach(function () {
testProviders = [];
injector = null;
});
/**
* Allows overriding default providers of the test injector,
* which are defined in test_injector.js.
*
* The given function must return a list of DI providers.
*
* Example:
*
* ```
* beforeEachProviders(() => [
* bind(Compiler).toClass(MockCompiler),
* bind(SomeToken).toValue(myValue),
* ]);
* ```
*/
function beforeEachProviders(fn) {
jsmBeforeEach(function () {
var providers = fn();
if (!providers)
return;
testProviders = testProviders.concat(providers);
if (injector !== null) {
throw new Error('beforeEachProviders was called after the injector had ' +
'been used in a beforeEach or it block. This invalidates the ' +
'test injector');
}
});
}
exports.beforeEachProviders = beforeEachProviders;
function _isPromiseLike(input) {
return input && !!(input.then);
}
function runInTestZone(fnToExecute, finishCallback, failCallback) {
var pendingMicrotasks = 0;
var pendingTimeouts = [];
var ngTestZone = lang_1.global.zone
.fork({
onError: function (e) { failCallback(e); },
'$run': function (parentRun) {
return function () {
try {
return parentRun.apply(this, arguments);
}
finally {
if (pendingMicrotasks == 0 && pendingTimeouts.length == 0) {
finishCallback();
}
}
};
},
'$scheduleMicrotask': function (parentScheduleMicrotask) {
return function (fn) {
pendingMicrotasks++;
var microtask = function () {
try {
fn();
}
finally {
pendingMicrotasks--;
}
};
parentScheduleMicrotask.call(this, microtask);
};
},
'$setTimeout': function (parentSetTimeout) {
return function (fn, delay) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
var id;
var cb = function () {
fn();
collection_1.ListWrapper.remove(pendingTimeouts, id);
};
id = parentSetTimeout(cb, delay, args);
pendingTimeouts.push(id);
return id;
};
},
'$clearTimeout': function (parentClearTimeout) {
return function (id) {
parentClearTimeout(id);
collection_1.ListWrapper.remove(pendingTimeouts, id);
};
},
});
return ngTestZone.run(fnToExecute);
}
function _it(jsmFn, name, testFn, testTimeOut) {
var timeOut = testTimeOut;
if (testFn instanceof test_injector_1.FunctionWithParamTokens) {
jsmFn(name, function (done) {
if (!injector) {
injector = test_injector_1.createTestInjector(testProviders);
}
var returnedTestValue = runInTestZone(function () { return testFn.execute(injector); }, done, done.fail);
if (_isPromiseLike(returnedTestValue)) {
returnedTestValue.then(null, function (err) { done.fail(err); });
}
}, timeOut);
}
else {
// The test case doesn't use inject(). ie `it('test', (done) => { ... }));`
jsmFn(name, testFn, timeOut);
}
}
/**
* Wrapper around Jasmine beforeEach function.
* See http://jasmine.github.io/
*
* beforeEach may be used with the `inject` function to fetch dependencies.
* The test will automatically wait for any asynchronous calls inside the
* injected test function to complete.
*/
function beforeEach(fn) {
if (fn instanceof test_injector_1.FunctionWithParamTokens) {
// The test case uses inject(). ie `beforeEach(inject([ClassA], (a) => { ...
// }));`
jsmBeforeEach(function (done) {
if (!injector) {
injector = test_injector_1.createTestInjector(testProviders);
}
runInTestZone(function () { return fn.execute(injector); }, done, done.fail);
});
}
else {
// The test case doesn't use inject(). ie `beforeEach((done) => { ... }));`
if (fn.length === 0) {
jsmBeforeEach(function () { fn(); });
}
else {
jsmBeforeEach(function (done) { fn(done); });
}
}
}
exports.beforeEach = beforeEach;
/**
* Wrapper around Jasmine it function.
* See http://jasmine.github.io/
*
* it may be used with the `inject` function to fetch dependencies.
* The test will automatically wait for any asynchronous calls inside the
* injected test function to complete.
*/
function it(name, fn, timeOut) {
if (timeOut === void 0) { timeOut = null; }
return _it(jsmIt, name, fn, timeOut);
}
exports.it = it;
/**
* Wrapper around Jasmine xit (skipped it) function.
* See http://jasmine.github.io/
*
* it may be used with the `inject` function to fetch dependencies.
* The test will automatically wait for any asynchronous calls inside the
* injected test function to complete.
*/
function xit(name, fn, timeOut) {
if (timeOut === void 0) { timeOut = null; }
return _it(jsmXIt, name, fn, timeOut);
}
exports.xit = xit;
/**
* Wrapper around Jasmine iit (focused it) function.
* See http://jasmine.github.io/
*
* it may be used with the `inject` function to fetch dependencies.
* The test will automatically wait for any asynchronous calls inside the
* injected test function to complete.
*/
function iit(name, fn, timeOut) {
if (timeOut === void 0) { timeOut = null; }
return _it(jsmIIt, name, fn, timeOut);
}
exports.iit = iit;
/**
* Wrapper around Jasmine fit (focused it) function.
* See http://jasmine.github.io/
*
* it may be used with the `inject` function to fetch dependencies.
* The test will automatically wait for any asynchronous calls inside the
* injected test function to complete.
*/
function fit(name, fn, timeOut) {
if (timeOut === void 0) { timeOut = null; }
return _it(jsmIIt, name, fn, timeOut);
}
exports.fit = fit;
//# sourceMappingURL=testing.js.map