本文整理汇总了TypeScript中angular2/testing.fakeAsync函数的典型用法代码示例。如果您正苦于以下问题:TypeScript fakeAsync函数的具体用法?TypeScript fakeAsync怎么用?TypeScript fakeAsync使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fakeAsync函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: describe
describe('Search Service', () => {
beforeEachProviders(() => {
return [BaseRequestOptions, MockBackend, SearchService,
provide(Http, {
useFactory: (backend:ConnectionBackend, defaultOptions:BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions]
}),
];
});
it('should retrieve all search results',
inject([SearchService, MockBackend], fakeAsync((searchService:SearchService, mockBackend:MockBackend) => {
var res:Response;
mockBackend.connections.subscribe(c => {
expect(c.request.url).toBe('shared/data/people.json');
let response = new ResponseOptions({body: '[{"name": "John Elway"}, {"name": "Gary Kubiak"}]'});
c.mockRespond(new Response(response));
});
searchService.getAll().subscribe((response) => {
res = response;
});
tick();
expect(res[0].name).toBe('John Elway');
}))
);
it('should filter by search term',
inject([SearchService, MockBackend], fakeAsync((searchService:SearchService, mockBackend:MockBackend) => {
var res:Response;
mockBackend.connections.subscribe(c => {
expect(c.request.url).toBe('shared/data/people.json');
let response = new ResponseOptions({body: '[{"name": "John Elway"}, {"name": "Gary Kubiak"}]'});
c.mockRespond(new Response(response));
});
searchService.search('john').subscribe((response) => {
res = response;
});
tick();
expect(res[0].name).toBe('John Elway');
}))
);
it('should fetch by id',
inject([SearchService, MockBackend], fakeAsync((searchService:SearchService, mockBackend:MockBackend) => {
var res:Response;
mockBackend.connections.subscribe(c => {
expect(c.request.url).toBe('shared/data/people.json');
let response = new ResponseOptions({body: '[{"id": 1, "name": "John Elway"}, {"id": 2, "name": "Gary Kubiak"}]'});
c.mockRespond(new Response(response));
});
searchService.search('2').subscribe((response) => {
res = response;
});
tick();
expect(res[0].name).toBe('Gary Kubiak');
}))
);
});
示例2: describe
describe('When starting the user app component', () => {
it('should show 2 users',
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
let mockUserService = new MockUserService();
mockUserService.setResponse([{
name: 'John',
username: 'thejohn',
email: 'pepe@gmail.com'
}]);
tcb
.overrideDirective(UserListComponent, UserListItemComponent, MockUserListItemComponent)
.overrideProviders(UserListComponent, [mockUserService.getProvider()])
.createAsync(UserListComponent)
.then((fixture: ComponentFixture) => {
fixture.detectChanges();
tick();
let compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('user-list-item span.email')).toHaveText('Email: pepe@gmail.com');
});
}))
);
it('should call the spy method',
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
let mockUserService = new MockUserService();
mockUserService.setResponse([{
name: 'John',
username: 'thejohn',
email: 'pepe@gmail.com'
}]);
spyOn(mockUserService, 'getAllUsers');
tcb
.overrideDirective(UserListComponent, UserListItemComponent, MockUserListItemComponent)
.overrideProviders(UserListComponent, [mockUserService.getProvider()])
.createAsync(UserListComponent)
.then((fixture: ComponentFixture) => {
fixture.detectChanges();
tick();
expect(mockUserService.getAllUsers).toHaveBeenCalled();
});
}))
);
});
示例3: describe
describe("Test Event Service", () => {
//inject all third Party Mock dependencies and configure providers
beforeEachProviders(() => {
return [
BaseRequestOptions,
MockBackend,
EventsService,
provide(Http, {
useFactory: (backend:ConnectionBackend, defaultOptions:BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions]
}),
]
});
it('retrieves using the event ID',
inject([EventsService, MockBackend], fakeAsync((eventsService, mockBackend) => {
var res;
mockBackend.connections.subscribe(c => {
expect(c.request.url).toBe('https://api.spotify.com/v1/tracks/TRACK_ID');
let response = new ResponseOptions({body: '{"id": 1, "name": "Trip to Paris"}'});
c.mockRespond(new Response(response));
});
eventsService.get(1).subscribe((_res) => {
res = _res;
});
tick();
expect(res.name).toBe('felipe');
}
))
);
});
示例4: describe
describe('and when trying to create a new user', () => {
it('should try to make a POST request to the proper URL',
inject([UserService, MockBackend], fakeAsync((service: UserService, backend: MockBackend) => {
let mockUser: IUser = {
username: 'barretodavid',
email: 'barretollano@gmail.com'
};
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.url).toBe('http://jsonplaceholder.typicode.com/users');
expect(connection.request.method).toBe(RequestMethod.Post);
expect(connection.request.text()).toBe(JSON.stringify(mockUser));
let mockResponseBody: IUser = { id: 11 };
let response = new ResponseOptions({body: JSON.stringify(mockResponseBody)});
connection.mockRespond(new Response(response));
});
service.createUser(mockUser);
}))
);
});
示例5: describe
describe('Home', () => {
it('should change name to "Angular" after 1s', injectAsync([TestComponentBuilder], fakeAsync((tcb) => {
return tcb.createAsync(Home).then((fixture) => {
const { componentInstance } = fixture;
expect(componentInstance.name).toBe('World');
tick(1000);
expect(componentInstance.name).toBe('Angular');
});
})));
it('should set message on button click', injectAsync([TestComponentBuilder], (tcb) => {
return tcb.createAsync(Home).then((fixture) => {
const { componentInstance, nativeElement } = fixture;
expect(componentInstance.messagePreboot).toBeFalsy();
nativeElement.querySelector('#check-preboot').click();
expect(componentInstance.messagePreboot).toBeTruthy();
});
}));
it('should lazy load service', injectAsync([TestComponentBuilder], (tcb) => {
return tcb.createAsync(Home).then((fixture) => {
const { componentInstance, nativeElement } = fixture;
expect(componentInstance.messageLazyLoading).toBeFalsy();
nativeElement.querySelector('#check-lazyloading').click();
expect(componentInstance.messageLazyLoading).toBeFalsy();
return new Promise(resolve => setTimeout(resolve, 100)).then(() => {
expect(componentInstance.messageLazyLoading).toBeTruthy();
});
});
}));
});
示例6: describe
describe('TaLogin Component', () => {
beforeEachProviders((): any[] => []);
it('should login', inject([TestComponentBuilder], fakeAsync((tcb:TestComponentBuilder) => {
let fixture;
tcb
// https://github.com/angular/angular/issues/2835
.overrideTemplate(TaLogin, `
<form>
<input type="text" #username>
<button (click)="loginSubmit(username)">Find user</button>
</form>
`)
.createAsync(TaLogin).then((rootFixture) => {
fixture = rootFixture;
});
tick();
let componentInstance = fixture.componentInstance;
let element = fixture.nativeElement;
let username;
componentInstance.login.subscribe((value) => {
username = value;
});
element.querySelector('input').value = 'cironunes';
element.querySelector('button').click();
tick();
expect(username).toBe('cironunes');
})));
});
示例7: describe
describe('searchTrack', () => {
it('searches type and term',
inject([SpotifyService, MockBackend], fakeAsync((svc, backend) => {
var res;
expectURL(backend, 'https://api.spotify.com/v1/search?q=TERM&type=track');
svc.searchTrack("TERM").subscribe((_res) => {
res = _res;
});
tick();
expect(res.name).toBe('felipe');
}))
);
});
示例8: describe
describe('with fake async', () => {
beforeEachProviders(() => [LoginService, UserService]);
it('should greet (with fakeAsync)', inject([UserService], fakeAsync((service) => {
var greeting;
service.getGreeting().then((value) => {
greeting = value;
});
tick(2000);
expect(greeting).toEqual('Login failure!');
})));
});