2015-08-04 14:55:21 -04:00
|
|
|
import {
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
expect,
|
|
|
|
beforeEach,
|
|
|
|
afterEach,
|
|
|
|
AsyncTestCompleter,
|
|
|
|
inject,
|
2015-08-24 09:41:36 -04:00
|
|
|
browserDetection
|
2015-10-13 03:29:13 -04:00
|
|
|
} from 'angular2/testing_internal';
|
2015-11-05 17:58:24 -05:00
|
|
|
import {SpyChangeDetectorRef} from '../spies';
|
2015-08-04 14:55:21 -04:00
|
|
|
|
2015-11-06 20:34:07 -05:00
|
|
|
import {isBlank} from 'angular2/src/facade/lang';
|
2015-11-18 18:55:43 -05:00
|
|
|
import {AsyncPipe} from 'angular2/common';
|
|
|
|
import {WrappedValue} from 'angular2/core';
|
2015-08-04 14:55:21 -04:00
|
|
|
import {
|
|
|
|
EventEmitter,
|
|
|
|
ObservableWrapper,
|
|
|
|
PromiseWrapper,
|
|
|
|
TimerWrapper
|
2015-11-06 20:34:07 -05:00
|
|
|
} from 'angular2/src/facade/async';
|
2015-11-19 18:09:34 -05:00
|
|
|
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
2015-08-04 14:55:21 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe("AsyncPipe", () => {
|
|
|
|
|
|
|
|
describe('Observable', () => {
|
|
|
|
var emitter;
|
|
|
|
var pipe;
|
|
|
|
var ref;
|
|
|
|
var message = new Object();
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
emitter = new EventEmitter();
|
|
|
|
ref = new SpyChangeDetectorRef();
|
|
|
|
pipe = new AsyncPipe(ref);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("transform", () => {
|
|
|
|
it("should return null when subscribing to an observable",
|
|
|
|
() => { expect(pipe.transform(emitter)).toBe(null); });
|
|
|
|
|
|
|
|
it("should return the latest available value wrapped",
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
pipe.transform(emitter);
|
|
|
|
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(emitter, message);
|
2015-08-04 14:55:21 -04:00
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
|
|
|
expect(pipe.transform(emitter)).toEqual(new WrappedValue(message));
|
|
|
|
async.done();
|
|
|
|
}, 0)
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it("should return same value when nothing has changed since the last call",
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
pipe.transform(emitter);
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(emitter, message);
|
2015-08-04 14:55:21 -04:00
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
|
|
|
pipe.transform(emitter);
|
|
|
|
expect(pipe.transform(emitter)).toBe(message);
|
|
|
|
async.done();
|
|
|
|
}, 0)
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should dispose of the existing subscription when subscribing to a new observable",
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
pipe.transform(emitter);
|
|
|
|
|
|
|
|
var newEmitter = new EventEmitter();
|
|
|
|
expect(pipe.transform(newEmitter)).toBe(null);
|
|
|
|
|
|
|
|
// this should not affect the pipe
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(emitter, message);
|
2015-08-04 14:55:21 -04:00
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
|
|
|
expect(pipe.transform(newEmitter)).toBe(null);
|
|
|
|
async.done();
|
|
|
|
}, 0)
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should request a change detection check upon receiving a new value",
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
pipe.transform(emitter);
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(emitter, message);
|
2015-08-04 14:55:21 -04:00
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
2015-08-28 13:08:18 -04:00
|
|
|
expect(ref.spy('markForCheck')).toHaveBeenCalled();
|
2015-08-04 14:55:21 -04:00
|
|
|
async.done();
|
|
|
|
}, 0)
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("onDestroy", () => {
|
|
|
|
it("should do nothing when no subscription",
|
|
|
|
() => { expect(() => pipe.onDestroy()).not.toThrow(); });
|
|
|
|
|
|
|
|
it("should dispose of the existing subscription", inject([AsyncTestCompleter], (async) => {
|
|
|
|
pipe.transform(emitter);
|
|
|
|
pipe.onDestroy();
|
|
|
|
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(emitter, message);
|
2015-08-04 14:55:21 -04:00
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
|
|
|
expect(pipe.transform(emitter)).toBe(null);
|
|
|
|
async.done();
|
|
|
|
}, 0)
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Promise", () => {
|
|
|
|
var message = new Object();
|
|
|
|
var pipe;
|
|
|
|
var completer;
|
|
|
|
var ref;
|
|
|
|
// adds longer timers for passing tests in IE
|
2015-08-24 09:41:36 -04:00
|
|
|
var timer = (!isBlank(DOM) && browserDetection.isIE) ? 50 : 0;
|
2015-08-04 14:55:21 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
completer = PromiseWrapper.completer();
|
|
|
|
ref = new SpyChangeDetectorRef();
|
|
|
|
pipe = new AsyncPipe(ref);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("transform", () => {
|
|
|
|
it("should return null when subscribing to a promise",
|
|
|
|
() => { expect(pipe.transform(completer.promise)).toBe(null); });
|
|
|
|
|
|
|
|
it("should return the latest available value", inject([AsyncTestCompleter], (async) => {
|
|
|
|
pipe.transform(completer.promise);
|
|
|
|
|
|
|
|
completer.resolve(message);
|
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
|
|
|
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
|
|
|
|
async.done();
|
|
|
|
}, timer)
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should return unwrapped value when nothing has changed since the last call",
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
pipe.transform(completer.promise);
|
|
|
|
completer.resolve(message);
|
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
|
|
|
pipe.transform(completer.promise);
|
|
|
|
expect(pipe.transform(completer.promise)).toBe(message);
|
|
|
|
async.done();
|
|
|
|
}, timer)
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should dispose of the existing subscription when subscribing to a new promise",
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
pipe.transform(completer.promise);
|
|
|
|
|
|
|
|
var newCompleter = PromiseWrapper.completer();
|
|
|
|
expect(pipe.transform(newCompleter.promise)).toBe(null);
|
|
|
|
|
|
|
|
// this should not affect the pipe, so it should return WrappedValue
|
|
|
|
completer.resolve(message);
|
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
|
|
|
expect(pipe.transform(newCompleter.promise)).toBe(null);
|
|
|
|
async.done();
|
|
|
|
}, timer)
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should request a change detection check upon receiving a new value",
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
pipe.transform(completer.promise);
|
|
|
|
completer.resolve(message);
|
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
2015-08-28 13:08:18 -04:00
|
|
|
expect(ref.spy('markForCheck')).toHaveBeenCalled();
|
2015-08-04 14:55:21 -04:00
|
|
|
async.done();
|
|
|
|
}, timer)
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe("onDestroy", () => {
|
|
|
|
it("should do nothing when no source",
|
|
|
|
() => { expect(() => pipe.onDestroy()).not.toThrow(); });
|
|
|
|
|
|
|
|
it("should dispose of the existing source", inject([AsyncTestCompleter], (async) => {
|
|
|
|
pipe.transform(completer.promise);
|
|
|
|
expect(pipe.transform(completer.promise)).toBe(null);
|
|
|
|
completer.resolve(message)
|
|
|
|
|
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
|
|
|
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
|
|
|
|
pipe.onDestroy();
|
|
|
|
expect(pipe.transform(completer.promise)).toBe(null);
|
|
|
|
async.done();
|
|
|
|
}, timer);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('null', () => {
|
|
|
|
it('should return null when given null', () => {
|
|
|
|
var pipe = new AsyncPipe(null);
|
|
|
|
expect(pipe.transform(null, [])).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('other types', () => {
|
|
|
|
it('should throw when given an invalid object', () => {
|
|
|
|
var pipe = new AsyncPipe(null);
|
|
|
|
expect(() => pipe.transform(<any>"some bogus object", [])).toThrowError();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|