Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <reference path="../../../../typings/main/ambient/jasmine/index.d.ts" />
- import {
- it,
- describe,
- expect,
- inject,
- beforeEach,
- beforeEachProviders,
- MockApplicationRef
- } from 'angular2/testing';
- import {
- RouteRegistry,
- SpyLocation,
- RootRouter,
- Router,
- RouteParams,
- ROUTER_PROVIDERS,
- APP_BASE_HREF,
- ROUTER_PRIMARY_COMPONENT
- } from 'angular2/router';
- import {
- ApplicationRef,
- provide
- } from 'angular2/core';
- import { MockApplicationRef } from 'angular2/src/mock/mock_application_ref';
- import {AppComponent} from '../../app.component';
- import {DetailComponent} from '../../components/index';
- import {Contact} from '../../interfaces/index';
- import {ContactsMock} from '../../mocks/index';
- import {ContactService} from '../../services/index';
- export function provideMockRouter():any[] {
- return [
- RouteRegistry,
- provide(Location, {useClass: SpyLocation}),
- provide(ROUTER_PRIMARY_COMPONENT, {useValue: AppComponent}),
- provide(Router, {useClass: RootRouter}),
- ];
- }
- let fillContacts = (service: ContactService) => {
- service.createContact(ContactsMock[0]);
- service.createContact(ContactsMock[1]);
- service.createContact(ContactsMock[2]);
- }
- describe('Detail Component', () => {
- let detail: DetailComponent;
- let contactService: ContactService;
- let routeParams: RouteParams;
- let router: Router;
- beforeEachProviders(() => [
- provide(ContactService, { useClass: ContactService }),
- provide(ApplicationRef, { useClass: MockApplicationRef }),
- provide(RouteParams, { useValue: new RouteParams({ contactId: '1' }) }),
- provideMockRouter()
- ]);
- // beforeEachProviders(() => [ContactService, Router]);
- beforeEach(inject([ContactService, Router, RouteParams], (_contactService: ContactService, _router: Router, _routeParams: RouteParams) => {
- console.log(_router);
- contactService = _contactService;
- router = _router;
- routeParams = _routeParams;
- detail = new DetailComponent(contactService, routeParams, router);
- }));
- it('creates a new contact', (done) => {
- spyOn(router, 'navigate').and.callThrough();
- detail.contact = ContactsMock[0];
- detail.createContact();
- contactService.getContacts().then(data => {
- expect(data.length).toEqual(1);
- detail.contact = ContactsMock[1];
- detail.createContact();
- contactService.getContacts().then(data2 => {
- expect(data2.length).toEqual(2);
- done();
- });
- });
- });
- it('should modify a contact', (done) => {
- spyOn(router, 'navigate').and.callThrough();
- console.log(detail.router);
- detail.contact = ContactsMock[0];
- detail.createContact();
- // await detail.call();
- expect(router.navigate).toHaveBeenCalledWith(['List']);
- expect(detail.contact.email).toEqual("juanlucha@gmail.com");
- detail.contact.email = "pepitoperez@gmail.com";
- detail.modifyContact();
- // await detail.call();
- expect(router.navigate).toHaveBeenCalledWith(['List']);
- expect(detail.contact.email).toEqual("pepitoperez@gmail.com");
- detail.contact = ContactsMock[1];
- detail.createContact();
- // await detail.call();
- expect(router.navigate).toHaveBeenCalledWith(['List']);
- expect(detail.contact.email).toEqual("pacocabrero@gmail.com");
- contactService.getContact(ContactsMock[0].id).then(data => {
- expect(data.email).toEqual("pepitoperez@gmail.com");
- done();
- });
- });
- it('should delete a contact', () => {
- detail.contact = ContactsMock[2];
- detail.createContact();
- expect(detail.contact.email).toEqual("pedromartos@gmail.com");
- contactService.getContacts().then(data => {
- expect(data.length).toEqual(1);
- detail.deleteContact();
- contactService.getContacts().then(data2 => {
- expect(data2.length).toEqual(0);
- });
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement