refactor (test/test_lib): Ts'ifying test/test_lib

Translates AtScript files in test/test_lib to TypeScript.

Closes #2183
This commit is contained in:
Ian Riley 2015-05-27 09:53:37 -07:00 committed by Tobias Bosch
parent 8ce0a67c81
commit ebe1e73b1a
4 changed files with 306 additions and 325 deletions

View File

@ -1,284 +0,0 @@
import {
AsyncTestCompleter,
beforeEach,
ddescribe,
describe,
expect,
fakeAsync,
flushMicrotasks,
iit,
inject,
IS_DARTIUM,
it,
Log,
tick,
xit
} from 'angular2/test_lib';
import {TimerWrapper, PromiseWrapper} from 'angular2/src/facade/async';
import {BaseException, global} from 'angular2/src/facade/lang';
import {Parser} from 'angular2/change_detection';
export function main() {
describe('fake async', () => {
it('should run synchronous code', () => {
var ran = false;
fakeAsync(() => {
ran = true;
})();
expect(ran).toEqual(true);
});
it('should pass arguments to the wrapped function', () => {
fakeAsync((foo, bar) => {
expect(foo).toEqual('foo');
expect(bar).toEqual('bar');
})('foo', 'bar');
});
it('should work with inject()', inject([Parser], fakeAsync((parser) => {
expect(parser).toBeAnInstanceOf(Parser);
})));
if (!IS_DARTIUM) {
it('should throw on nested calls', () => {
// TODO(vicb): re-enable once the jasmine patch from zone.js is applied
if (!IS_DARTIUM) return;
expect(() => {
fakeAsync(() => {
fakeAsync(() => null)();
})();
}).toThrowError('fakeAsync() calls can not be nested');
});
}
describe('Promise', () => {
it('should run asynchronous code', fakeAsync(() => {
var thenRan = false;
PromiseWrapper.resolve(null).then((_) => {
thenRan = true;
});
expect(thenRan).toEqual(false);
flushMicrotasks();
expect(thenRan).toEqual(true);
}));
it('should run chained thens', fakeAsync(() => {
var log = new Log();
PromiseWrapper
.resolve(null)
.then((_) => log.add(1))
.then((_) => log.add(2));
expect(log.result()).toEqual('');
flushMicrotasks();
expect(log.result()).toEqual('1; 2');
}));
it('should run Promise created in Promise', fakeAsync(() => {
var log = new Log();
PromiseWrapper
.resolve(null)
.then((_) => {
log.add(1);
PromiseWrapper.resolve(null).then((_) => log.add(2));
});
expect(log.result()).toEqual('');
flushMicrotasks();
expect(log.result()).toEqual('1; 2');
}));
// TODO(vicb): check why this doesn't work in JS - linked to open issues on GH ?
xit('should complain if the test throws an exception during async calls', () => {
expect(() => {
fakeAsync(() => {
PromiseWrapper.resolve(null).then((_) => {
throw new BaseException('async');
});
flushMicrotasks();
})();
}).toThrowError('async');
});
it('should complain if a test throws an exception', () => {
expect(() => {
fakeAsync(() => {
throw new BaseException('sync');
})();
}).toThrowError('sync');
});
});
describe('timers', () => {
it('should run queued zero duration timer on zero tick', fakeAsync(() => {
var ran = false;
TimerWrapper.setTimeout(() => { ran = true }, 0);
expect(ran).toEqual(false);
tick();
expect(ran).toEqual(true);
}));
it('should run queued timer after sufficient clock ticks', fakeAsync(() => {
var ran = false;
TimerWrapper.setTimeout(() => { ran = true; }, 10);
tick(6);
expect(ran).toEqual(false);
tick(6);
expect(ran).toEqual(true);
}));
it('should run queued timer only once', fakeAsync(() => {
var cycles = 0;
TimerWrapper.setTimeout(() => { cycles++; }, 10);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(1);
}));
it('should not run cancelled timer', fakeAsync(() => {
var ran = false;
var id = TimerWrapper.setTimeout(() => { ran = true; }, 10);
TimerWrapper.clearTimeout(id);
tick(10);
expect(ran).toEqual(false);
}));
it('should throw an error on dangling timers', () => {
// TODO(vicb): https://github.com/google/quiver-dart/issues/248
if (IS_DARTIUM) return;
expect(() => {
fakeAsync(() => {
TimerWrapper.setTimeout(() => { }, 10);
})();
}).toThrowError('1 timer(s) still in the queue.');
});
it('should throw an error on dangling periodic timers', () => {
// TODO(vicb): https://github.com/google/quiver-dart/issues/248
if (IS_DARTIUM) return;
expect(() => {
fakeAsync(() => {
TimerWrapper.setInterval(() => { }, 10);
})();
}).toThrowError('1 periodic timer(s) still in the queue.');
});
it('should run periodic timers', fakeAsync(() => {
var cycles = 0;
var id = TimerWrapper.setInterval(() => { cycles++; }, 10);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(2);
tick(10);
expect(cycles).toEqual(3);
TimerWrapper.clearInterval(id);
}));
it('should not run cancelled periodic timer', fakeAsync(() => {
var ran = false;
var id = TimerWrapper.setInterval(() => { ran = true; }, 10);
TimerWrapper.clearInterval(id);
tick(10);
expect(ran).toEqual(false);
}));
it('should be able to cancel periodic timers from a callback', fakeAsync(() => {
var cycles = 0;
var id;
id = TimerWrapper.setInterval(() => {
cycles++;
TimerWrapper.clearInterval(id);
}, 10);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(1);
}));
it('should process microtasks before timers', fakeAsync(() => {
var log = new Log();
PromiseWrapper.resolve(null).then((_) => log.add('microtask'));
TimerWrapper.setTimeout(() => log.add('timer'), 9);
var id = TimerWrapper.setInterval(() => log.add('periodic timer'), 10);
expect(log.result()).toEqual('');
tick(10);
expect(log.result()).toEqual('microtask; timer; periodic timer');
TimerWrapper.clearInterval(id);
}));
it('should process micro-tasks created in timers before next timers', fakeAsync(() => {
var log = new Log();
PromiseWrapper.resolve(null).then((_) => log.add('microtask'));
TimerWrapper.setTimeout(() => {
log.add('timer');
PromiseWrapper.resolve(null).then((_) => log.add('t microtask'));
}, 9);
var id = TimerWrapper.setInterval(() => {
log.add('periodic timer');
PromiseWrapper.resolve(null).then((_) => log.add('pt microtask'));
}, 10);
tick(10);
expect(log.result()).toEqual('microtask; timer; t microtask; periodic timer; pt microtask');
tick(10);
expect(log.result()).toEqual('microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask');
TimerWrapper.clearInterval(id);
}));
});
describe('outside of the fakeAsync zone', () => {
it('calling flushMicrotasks should throw', () => {
expect(() => {
flushMicrotasks();
}).toThrowError('The code should be running in the fakeAsync zone to call this function');
});
it('calling tick should throw', () => {
expect(() => {
tick();
}).toThrowError('The code should be running in the fakeAsync zone to call this function');
});
});
});
}

View File

@ -0,0 +1,261 @@
import {
AsyncTestCompleter,
beforeEach,
ddescribe,
describe,
expect,
fakeAsync,
flushMicrotasks,
iit,
inject,
IS_DARTIUM,
it,
Log,
tick,
xit
} from 'angular2/test_lib';
import {TimerWrapper, PromiseWrapper} from 'angular2/src/facade/async';
import {BaseException, global} from 'angular2/src/facade/lang';
import {Parser} from 'angular2/change_detection';
export function main() {
describe('fake async', () => {
it('should run synchronous code', () => {
var ran = false;
fakeAsync(() => { ran = true; })();
expect(ran).toEqual(true);
});
it('should pass arguments to the wrapped function', () => {
fakeAsync((foo, bar) => {
expect(foo).toEqual('foo');
expect(bar).toEqual('bar');
})('foo', 'bar');
});
it('should work with inject()',
inject([Parser], fakeAsync((parser) => { expect(parser).toBeAnInstanceOf(Parser); })));
if (!IS_DARTIUM) {
it('should throw on nested calls', () => {
// TODO(vicb): re-enable once the jasmine patch from zone.js is applied
if (!IS_DARTIUM) return;
expect(() => { fakeAsync(() => { fakeAsync(() => null)(); })(); })
.toThrowError('fakeAsync() calls can not be nested');
});
}
describe('Promise', () => {
it('should run asynchronous code', fakeAsync(() => {
var thenRan = false;
PromiseWrapper.resolve(null).then((_) => { thenRan = true; });
expect(thenRan).toEqual(false);
flushMicrotasks();
expect(thenRan).toEqual(true);
}));
it('should run chained thens', fakeAsync(() => {
var log = new Log();
PromiseWrapper.resolve(null).then((_) => log.add(1)).then((_) => log.add(2));
expect(log.result()).toEqual('');
flushMicrotasks();
expect(log.result()).toEqual('1; 2');
}));
it('should run Promise created in Promise', fakeAsync(() => {
var log = new Log();
PromiseWrapper.resolve(null).then((_) => {
log.add(1);
PromiseWrapper.resolve(null).then((_) => log.add(2));
});
expect(log.result()).toEqual('');
flushMicrotasks();
expect(log.result()).toEqual('1; 2');
}));
// TODO(vicb): check why this doesn't work in JS - linked to open issues on GH ?
xit('should complain if the test throws an exception during async calls', () => {
expect(() => {
fakeAsync(() => {
PromiseWrapper.resolve(null).then((_) => { throw new BaseException('async'); });
flushMicrotasks();
})();
}).toThrowError('async');
});
it('should complain if a test throws an exception', () => {
expect(() => { fakeAsync(() => { throw new BaseException('sync'); })(); })
.toThrowError('sync');
});
});
describe('timers', () => {
it('should run queued zero duration timer on zero tick', fakeAsync(() => {
var ran = false;
TimerWrapper.setTimeout(() => {ran = true}, 0);
expect(ran).toEqual(false);
tick();
expect(ran).toEqual(true);
}));
it('should run queued timer after sufficient clock ticks', fakeAsync(() => {
var ran = false;
TimerWrapper.setTimeout(() => { ran = true; }, 10);
tick(6);
expect(ran).toEqual(false);
tick(6);
expect(ran).toEqual(true);
}));
it('should run queued timer only once', fakeAsync(() => {
var cycles = 0;
TimerWrapper.setTimeout(() => { cycles++; }, 10);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(1);
}));
it('should not run cancelled timer', fakeAsync(() => {
var ran = false;
var id = TimerWrapper.setTimeout(() => { ran = true; }, 10);
TimerWrapper.clearTimeout(id);
tick(10);
expect(ran).toEqual(false);
}));
it('should throw an error on dangling timers', () => {
// TODO(vicb): https://github.com/google/quiver-dart/issues/248
if (IS_DARTIUM) return;
expect(() => { fakeAsync(() => { TimerWrapper.setTimeout(() => {}, 10); })(); })
.toThrowError('1 timer(s) still in the queue.');
});
it('should throw an error on dangling periodic timers', () => {
// TODO(vicb): https://github.com/google/quiver-dart/issues/248
if (IS_DARTIUM) return;
expect(() => { fakeAsync(() => { TimerWrapper.setInterval(() => {}, 10); })(); })
.toThrowError('1 periodic timer(s) still in the queue.');
});
it('should run periodic timers', fakeAsync(() => {
var cycles = 0;
var id = TimerWrapper.setInterval(() => { cycles++; }, 10);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(2);
tick(10);
expect(cycles).toEqual(3);
TimerWrapper.clearInterval(id);
}));
it('should not run cancelled periodic timer', fakeAsync(() => {
var ran = false;
var id = TimerWrapper.setInterval(() => { ran = true; }, 10);
TimerWrapper.clearInterval(id);
tick(10);
expect(ran).toEqual(false);
}));
it('should be able to cancel periodic timers from a callback', fakeAsync(() => {
var cycles = 0;
var id;
id = TimerWrapper.setInterval(() => {
cycles++;
TimerWrapper.clearInterval(id);
}, 10);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(1);
}));
it('should process microtasks before timers', fakeAsync(() => {
var log = new Log();
PromiseWrapper.resolve(null).then((_) => log.add('microtask'));
TimerWrapper.setTimeout(() => log.add('timer'), 9);
var id = TimerWrapper.setInterval(() => log.add('periodic timer'), 10);
expect(log.result()).toEqual('');
tick(10);
expect(log.result()).toEqual('microtask; timer; periodic timer');
TimerWrapper.clearInterval(id);
}));
it('should process micro-tasks created in timers before next timers', fakeAsync(() => {
var log = new Log();
PromiseWrapper.resolve(null).then((_) => log.add('microtask'));
TimerWrapper.setTimeout(() => {
log.add('timer');
PromiseWrapper.resolve(null).then((_) => log.add('t microtask'));
}, 9);
var id = TimerWrapper.setInterval(() => {
log.add('periodic timer');
PromiseWrapper.resolve(null).then((_) => log.add('pt microtask'));
}, 10);
tick(10);
expect(log.result())
.toEqual('microtask; timer; t microtask; periodic timer; pt microtask');
tick(10);
expect(log.result())
.toEqual(
'microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask');
TimerWrapper.clearInterval(id);
}));
});
describe('outside of the fakeAsync zone', () => {
it('calling flushMicrotasks should throw', () => {
expect(() => { flushMicrotasks(); })
.toThrowError('The code should be running in the fakeAsync zone to call this function');
});
it('calling tick should throw', () => {
expect(() => { tick(); })
.toThrowError('The code should be running in the fakeAsync zone to call this function');
});
});
});
}

View File

@ -1,25 +1,31 @@
import {describe, it, iit, ddescribe, expect, tick, async, SpyObject, beforeEach, proxy, containsRegexp} from 'angular2/test_lib';
import {
describe,
it,
iit,
ddescribe,
expect,
tick,
SpyObject,
beforeEach,
proxy,
containsRegexp
} from 'angular2/test_lib';
import {MapWrapper} from 'angular2/src/facade/collection';
import {IMPLEMENTS, RegExpWrapper} from 'angular2/src/facade/lang';
class TestObj {
prop;
constructor(prop) {
this.prop = prop;
}
someFunc():number {
return -1;
}
someComplexFunc(a) {
return a;
}
constructor(prop) { this.prop = prop; }
someFunc(): number { return -1; }
someComplexFunc(a) { return a; }
}
@proxy
@IMPLEMENTS(TestObj)
class SpyTestObj extends SpyObject {
constructor(){super(TestObj);}
noSuchMethod(m){return super.noSuchMethod(m)}
constructor() { super(TestObj); }
noSuchMethod(m) { return super.noSuchMethod(m) }
}
@ -27,9 +33,9 @@ export function main() {
describe('test_lib', () => {
describe('equality', () => {
it('should structurally compare objects', () => {
var expected = new TestObj(new TestObj({'one' : [1,2]}));
var actual = new TestObj(new TestObj({'one' : [1,2]}));
var falseActual = new TestObj(new TestObj({'one' : [1,3]}));
var expected = new TestObj(new TestObj({'one': [1, 2]}));
var actual = new TestObj(new TestObj({'one': [1, 2]}));
var falseActual = new TestObj(new TestObj({'one': [1, 3]}));
expect(actual).toEqual(expected);
expect(falseActual).not.toEqual(expected);
@ -43,57 +49,56 @@ export function main() {
});
it('should detect equality for same content', () => {
expect(MapWrapper.createFromStringMap({'a': 1})).toEqual(MapWrapper.createFromStringMap({'a': 1}));
expect(MapWrapper.createFromStringMap({'a': 1}))
.toEqual(MapWrapper.createFromStringMap({'a': 1}));
});
it('should detect missing entries', () => {
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({}));
expect(MapWrapper.createFromStringMap({'a': 1}))
.not.toEqual(MapWrapper.createFromStringMap({}));
});
it('should detect different values', () => {
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({'a': 2}));
expect(MapWrapper.createFromStringMap({'a': 1}))
.not.toEqual(MapWrapper.createFromStringMap({'a': 2}));
});
it('should detect additional entries', () => {
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({'a': 1, 'b': 1}));
expect(MapWrapper.createFromStringMap({'a': 1}))
.not.toEqual(MapWrapper.createFromStringMap({'a': 1, 'b': 1}));
});
});
describe("spy objects", () => {
var spyObj;
beforeEach(() => {
spyObj = new SpyTestObj();
});
beforeEach(() => { spyObj = <any>new SpyTestObj(); });
it("should pass the runtime check", () => {
var t:TestObj = spyObj;
var t: TestObj = spyObj;
expect(t).toBeDefined();
});
it("should return a new spy func with no calls", () => {
expect(spyObj.spy("someFunc")).not.toHaveBeenCalled();
});
it("should return a new spy func with no calls",
() => { expect(spyObj.spy("someFunc")).not.toHaveBeenCalled(); });
it("should record function calls", () => {
spyObj.spy("someFunc").andCallFake((a,b) => {
return a + b
});
spyObj.spy("someFunc").andCallFake((a, b) => {return a + b});
expect(spyObj.someFunc(1,2)).toEqual(3);
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
expect(spyObj.someFunc(1, 2)).toEqual(3);
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1, 2);
});
it("should match multiple function calls", () => {
spyObj.someFunc(1,2);
spyObj.someFunc(3,4);
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(3,4);
spyObj.someFunc(1, 2);
spyObj.someFunc(3, 4);
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1, 2);
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(3, 4);
});
it("should match null arguments", () => {
spyObj.someFunc(null, "hello");
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(null,"hello");
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(null, "hello");
});
it("should match using deep equality", () => {
@ -102,15 +107,14 @@ export function main() {
});
it("should support stubs", () => {
var s = SpyObject.stub({"a":1}, {"b":2});
var s = SpyObject.stub({"a": 1}, {"b": 2});
expect(s.a()).toEqual(1);
expect(s.b()).toEqual(2);
});
it('should create spys for all methods', () => {
expect(() => spyObj.someFunc()).not.toThrow();
});
it('should create spys for all methods',
() => { expect(() => spyObj.someFunc()).not.toThrow(); });
it('should create a default spy that does not fail for numbers', () => {
// Need to return null instead of undefined so that rtts assert does

View File

@ -22,7 +22,7 @@ module.exports = function makeNodeTree(destinationPath) {
exclude: [
// the following code and tests are not compatible with CJS/node environment
'angular2/test/core/zone/**',
'angular2/test/test_lib/fake_async_spec.js',
'angular2/test/test_lib/fake_async_spec.ts',
'angular2/test/services/xhr_impl_spec.ts'
]
});