本文整理汇总了TypeScript中angular2/testing_internal.fakeAsync函数的典型用法代码示例。如果您正苦于以下问题:TypeScript fakeAsync函数的具体用法?TypeScript fakeAsync怎么用?TypeScript fakeAsync使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fakeAsync函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: describe
describe("composeAsync", () => {
function asyncValidator(expected, response) {
return (c) => {
var emitter = new EventEmitter();
var res = c.value != expected ? response : null;
PromiseWrapper.scheduleMicrotask(() => {
ObservableWrapper.callEmit(emitter, res);
// this is required because of a bug in ObservableWrapper
// where callComplete can fire before callEmit
// remove this one the bug is fixed
TimerWrapper.setTimeout(() => { ObservableWrapper.callComplete(emitter); }, 0);
});
return emitter;
};
}
it("should return null when given null",
() => { expect(Validators.composeAsync(null)).toEqual(null); });
it("should collect errors from all the validators", fakeAsync(() => {
var c = Validators.composeAsync([
asyncValidator("expected", {"one": true}),
asyncValidator("expected", {"two": true})
]);
var value = null;
c(new Control("invalid")).then(v => value = v);
tick(1);
expect(value).toEqual({"one": true, "two": true});
}));
it("should return null when no errors", fakeAsync(() => {
var c = Validators.composeAsync([asyncValidator("expected", {"one": true})]);
var value = null;
c(new Control("expected")).then(v => value = v);
tick(1);
expect(value).toEqual(null);
}));
it("should ignore nulls", fakeAsync(() => {
var c = Validators.composeAsync([asyncValidator("expected", {"one": true}), null]);
var value = null;
c(new Control("invalid")).then(v => value = v);
tick(1);
expect(value).toEqual({"one": true});
}));
});
示例2: describe
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');
});
});
示例3: it
it('should flush microtasks before returning', () => {
var thenRan = false;
fakeAsync(() => { PromiseWrapper.resolve(null).then(_ => { thenRan = true; }); })();
expect(thenRan).toEqual(true);
});
示例4: describe
describe('CachedXHR', () => {
var xhr: CachedXHR;
function createCachedXHR(): CachedXHR {
setTemplateCache({'test.html': '<div>Hello</div>'});
return new CachedXHR();
}
beforeEachProviders(() => [
provide(UrlResolver, {useClass: TestUrlResolver}),
provide(XHR, {useFactory: createCachedXHR})
]);
it('should throw exception if $templateCache is not found', () => {
setTemplateCache(null);
expect(() => { xhr = new CachedXHR(); })
.toThrowErrorWith('CachedXHR: Template cache was not found in $templateCache.');
});
it('should resolve the Promise with the cached file content on success',
inject([AsyncTestCompleter], (async) => {
setTemplateCache({'test.html': '<div>Hello</div>'});
xhr = new CachedXHR();
xhr.get('test.html')
.then((text) => {
expect(text).toEqual('<div>Hello</div>');
async.done();
});
}));
it('should reject the Promise on failure', inject([AsyncTestCompleter], (async) => {
xhr = new CachedXHR();
xhr.get('unknown.html')
.then((text) => { throw new BaseException('Not expected to succeed.'); })
.catch((error) => { async.done(); });
}));
it('should allow fakeAsync Tests to load components with templateUrl synchronously',
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
let fixture: ComponentFixture;
tcb.createAsync(TestComponent).then((f) => { fixture = f; });
// This should initialize the fixture.
tick();
expect(fixture.debugElement.children[0].nativeElement).toHaveText('Hello');
})));
});
示例5: expect
expect(() => {
fakeAsync(() => {
PromiseWrapper.resolve(null).then((_) => { throw new BaseException('async'); });
flushMicrotasks();
})();
}).toThrowError('async');
示例6: expect
expect(() => {
fakeAsync(() => { TimerWrapper.setInterval(() => {}, 10); })();
}).toThrowError('1 periodic timer(s) still in the queue.');