2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
describe,
|
|
|
|
it,
|
|
|
|
expect,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
inject,
|
2016-04-28 20:50:03 -04:00
|
|
|
} from '@angular/core/testing/testing_internal';
|
|
|
|
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
|
|
|
import {browserDetection} from '@angular/platform-browser/testing';
|
2016-05-02 01:50:37 -04:00
|
|
|
import {ObservableWrapper, Observable, Subject, EventEmitter, PromiseWrapper} from '../src/async';
|
2015-05-26 20:00:31 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('EventEmitter', () => {
|
2015-10-24 21:48:43 -04:00
|
|
|
var emitter: EventEmitter<any>;
|
2015-05-26 20:00:31 -04:00
|
|
|
|
|
|
|
beforeEach(() => { emitter = new EventEmitter(); });
|
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
it("should call the next callback", inject([AsyncTestCompleter], (async: any /** TODO #9100 */) => {
|
2015-05-26 20:00:31 -04:00
|
|
|
ObservableWrapper.subscribe(emitter, (value) => {
|
|
|
|
expect(value).toEqual(99);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(emitter, 99);
|
2015-05-26 20:00:31 -04:00
|
|
|
}));
|
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
it("should call the throw callback", inject([AsyncTestCompleter], (async: any /** TODO #9100 */) => {
|
2015-05-26 20:00:31 -04:00
|
|
|
ObservableWrapper.subscribe(emitter, (_) => {}, (error) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
expect(error).toEqual("Boom");
|
2015-05-26 20:00:31 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2016-04-12 12:40:37 -04:00
|
|
|
ObservableWrapper.callError(emitter, "Boom");
|
2015-05-26 20:00:31 -04:00
|
|
|
}));
|
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
it("should work when no throw callback is provided", inject([AsyncTestCompleter], (async: any /** TODO #9100 */) => {
|
2015-05-26 20:00:31 -04:00
|
|
|
ObservableWrapper.subscribe(emitter, (_) => {}, (_) => { async.done(); });
|
2016-04-12 12:40:37 -04:00
|
|
|
ObservableWrapper.callError(emitter, "Boom");
|
2015-05-26 20:00:31 -04:00
|
|
|
}));
|
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
it("should call the return callback", inject([AsyncTestCompleter], (async: any /** TODO #9100 */) => {
|
2015-05-26 20:00:31 -04:00
|
|
|
ObservableWrapper.subscribe(emitter, (_) => {}, (_) => {}, () => { async.done(); });
|
|
|
|
|
2015-10-24 21:48:43 -04:00
|
|
|
ObservableWrapper.callComplete(emitter);
|
2015-05-26 20:00:31 -04:00
|
|
|
}));
|
|
|
|
|
2016-05-26 12:34:04 -04:00
|
|
|
it("should subscribe to the wrapper synchronously", () => {
|
2015-05-26 20:00:31 -04:00
|
|
|
var called = false;
|
|
|
|
ObservableWrapper.subscribe(emitter, (value) => { called = true; });
|
|
|
|
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(emitter, 99);
|
2016-05-26 12:34:04 -04:00
|
|
|
expect(called).toBe(true);
|
2015-05-26 20:00:31 -04:00
|
|
|
});
|
|
|
|
|
2015-11-26 09:54:25 -05:00
|
|
|
// Makes Edge to disconnect when running the full unit test campaign
|
|
|
|
// TODO: remove when issue is solved: https://github.com/angular/angular/issues/4756
|
|
|
|
if (!browserDetection.isEdge) {
|
2016-06-08 18:45:15 -04:00
|
|
|
it("delivers next and error events synchronously", inject([AsyncTestCompleter], (async: any /** TODO #9100 */) => {
|
|
|
|
let log: any[] /** TODO #9100 */ = [];
|
2016-04-12 12:40:37 -04:00
|
|
|
ObservableWrapper.subscribe(emitter,
|
|
|
|
(x) => {
|
|
|
|
log.push(x);
|
2016-05-26 12:34:04 -04:00
|
|
|
expect(log).toEqual([1, 2]);
|
2016-04-12 12:40:37 -04:00
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
log.push(err);
|
2016-05-26 12:34:04 -04:00
|
|
|
expect(log).toEqual([1, 2, 3, 4]);
|
2016-04-12 12:40:37 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2015-11-26 09:54:25 -05:00
|
|
|
log.push(1);
|
|
|
|
ObservableWrapper.callEmit(emitter, 2);
|
|
|
|
log.push(3);
|
|
|
|
ObservableWrapper.callError(emitter, 4);
|
|
|
|
log.push(5);
|
|
|
|
}));
|
|
|
|
|
2016-05-26 12:34:04 -04:00
|
|
|
it("delivers next and complete events synchronously", () => {
|
2016-06-08 18:45:15 -04:00
|
|
|
let log: any[] /** TODO #9100 */ = [];
|
2016-04-12 12:40:37 -04:00
|
|
|
ObservableWrapper.subscribe(emitter,
|
|
|
|
(x) => {
|
|
|
|
log.push(x);
|
2016-05-26 12:34:04 -04:00
|
|
|
expect(log).toEqual([1, 2]);
|
2016-04-12 12:40:37 -04:00
|
|
|
},
|
|
|
|
null, () => {
|
|
|
|
log.push(4);
|
2016-05-26 12:34:04 -04:00
|
|
|
expect(log).toEqual([1, 2, 3, 4]);
|
|
|
|
});
|
2015-11-26 09:54:25 -05:00
|
|
|
log.push(1);
|
|
|
|
ObservableWrapper.callEmit(emitter, 2);
|
|
|
|
log.push(3);
|
|
|
|
ObservableWrapper.callComplete(emitter);
|
|
|
|
log.push(5);
|
2016-05-26 12:34:04 -04:00
|
|
|
expect(log).toEqual([1, 2, 3, 4, 5]);
|
|
|
|
});
|
2015-11-26 09:54:25 -05:00
|
|
|
}
|
2015-10-19 17:41:15 -04:00
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
it('delivers events asynchronously when forced to async mode', inject([AsyncTestCompleter], (async: any /** TODO #9100 */) => {
|
2016-05-26 12:34:04 -04:00
|
|
|
var e = new EventEmitter(true);
|
2016-06-08 18:45:15 -04:00
|
|
|
var log: any[] /** TODO #9100 */ = [];
|
2016-05-26 12:34:04 -04:00
|
|
|
ObservableWrapper.subscribe(e, (x) => {
|
|
|
|
log.push(x);
|
|
|
|
expect(log).toEqual([1, 3, 2]);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-19 17:41:15 -04:00
|
|
|
log.push(1);
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(e, 2);
|
2015-10-19 17:41:15 -04:00
|
|
|
log.push(3);
|
2016-05-26 12:34:04 -04:00
|
|
|
|
|
|
|
}));
|
2015-10-19 17:41:15 -04:00
|
|
|
|
|
|
|
it('reports whether it has subscribers', () => {
|
|
|
|
var e = new EventEmitter(false);
|
|
|
|
expect(ObservableWrapper.hasSubscribers(e)).toBe(false);
|
|
|
|
ObservableWrapper.subscribe(e, (_) => {});
|
|
|
|
expect(ObservableWrapper.hasSubscribers(e)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2015-05-26 20:00:31 -04:00
|
|
|
// TODO: vsavkin: add tests cases
|
|
|
|
// should call dispose on the subscription if generator returns {done:true}
|
|
|
|
// should call dispose on the subscription on throw
|
|
|
|
// should call dispose on the subscription on return
|
|
|
|
});
|
2015-05-29 20:09:59 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("ObservableWrapper", () => {
|
2015-10-29 00:49:22 -04:00
|
|
|
|
|
|
|
it('should correctly check isObservable for EventEmitter', () => {
|
|
|
|
var e = new EventEmitter(false);
|
|
|
|
expect(ObservableWrapper.isObservable(e)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly check isObservable for Subject', () => {
|
|
|
|
var e = new Subject();
|
|
|
|
expect(ObservableWrapper.isObservable(e)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2015-10-29 22:57:28 -04:00
|
|
|
it('should subscribe to EventEmitters', () => {
|
|
|
|
let e = new EventEmitter(false);
|
|
|
|
|
|
|
|
ObservableWrapper.subscribe(e, (val) => {});
|
|
|
|
|
|
|
|
ObservableWrapper.callEmit(e, 1);
|
|
|
|
ObservableWrapper.callComplete(e);
|
|
|
|
});
|
|
|
|
|
2015-10-29 00:49:22 -04:00
|
|
|
});
|
|
|
|
|
2015-05-29 20:09:59 -04:00
|
|
|
// See ECMAScript 6 Spec 25.4.4.1
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("PromiseWrapper", () => {
|
|
|
|
describe("#all", () => {
|
2016-06-08 18:45:15 -04:00
|
|
|
it("should combine lists of Promises", inject([AsyncTestCompleter], (async: any /** TODO #9100 */) => {
|
2015-05-29 20:09:59 -04:00
|
|
|
var one = PromiseWrapper.completer();
|
|
|
|
var two = PromiseWrapper.completer();
|
|
|
|
|
|
|
|
var all = PromiseWrapper.all([one.promise, two.promise]);
|
|
|
|
var allCalled = false;
|
|
|
|
|
|
|
|
PromiseWrapper.then(one.promise, (_) => {
|
|
|
|
expect(allCalled).toBe(false);
|
|
|
|
two.resolve('two');
|
2015-06-24 01:02:20 -04:00
|
|
|
return null;
|
2015-05-29 20:09:59 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
PromiseWrapper.then(all, (_) => {
|
|
|
|
allCalled = true;
|
|
|
|
async.done();
|
2015-06-24 01:02:20 -04:00
|
|
|
return null;
|
2015-05-29 20:09:59 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
one.resolve('one');
|
|
|
|
}));
|
|
|
|
|
2015-10-07 12:09:43 -04:00
|
|
|
[null, true, false, 10, 'thing', {}, []].forEach(abruptCompletion => {
|
2015-05-29 20:09:59 -04:00
|
|
|
it(`should treat "${abruptCompletion}" as an "abrupt completion"`,
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([AsyncTestCompleter], (async: any /** TODO #9100 */) => {
|
2015-05-29 20:09:59 -04:00
|
|
|
var one = PromiseWrapper.completer();
|
|
|
|
|
|
|
|
var all = PromiseWrapper.all([one.promise, abruptCompletion]);
|
|
|
|
|
|
|
|
PromiseWrapper.then(all, (val) => {
|
|
|
|
expect(val[1]).toEqual(abruptCompletion);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
one.resolve('one');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|