import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; import { Router } from '@angular/router'; import { Observable } from 'rxjs/Rx'; import { APIService } from '../services/api.service'; import { StateService } from '../services/state.service'; import { StoreService } from '../services/store.service'; import { RegisterComponent } from './register.component'; describe('Component: Registration', () => { let fixture: RegisterComponent, router: Router, fb: FormBuilder, state: StateService, api: APIService, store: StoreService; beforeEach(() => { router = jasmine.createSpyObj('router', ['navigateByUrl']); state = jasmine.createSpyObj( 'state', [ 'getNextPageToShow', 'getPrevPageToShow', 'hidePage', 'setLoading' ] ); fb = new FormBuilder(); api = jasmine.createSpyObj('api', ['postLogin', 'postUser']); fixture = new RegisterComponent(api, fb, router, state, store); fixture.ngOnInit(); }); function setForm( firstName, lastName, email, password ) { fixture.regForm = new FormGroup({ firstName: new FormControl(firstName, Validators.required), lastname: new FormControl(lastName, Validators.required), email: new FormControl(email, Validators.required), password: new FormControl(password) }); } describe('#ngOnInit', () => { it('initializes the component', () => { expect(fixture).toBeTruthy(); }); }); describe('#adv', () => { xit('should call the router to move to the next step', () => { fixture.adv(); expect(router.navigateByUrl).toHaveBeenCalled(); }); }); describe('#submit User', () => { it('should not process if form is invalid', () => { let didSubmit = fixture.submitRegistration(); expect(didSubmit).toBe(false); }); }); describe('#formatErrorMessage', () => { it('should return required when errors.required !== undefined', () => { let errors = { required: true }; let res = fixture.switchMessage(errors); expect(res).toBe('is required'); }); it('should return invalid when errors.required === undefined', () => { let errors = { require: undefined }; let res = fixture.switchMessage(errors); expect(res).toBe('is invalid'); }); }); describe('#next', () => { describe('when form is invalid', () => { beforeEach(() => { setForm('Bob', '', 'good@g.com', 'foobar'); }); it('should not call #adv', () => { spyOn(fixture, 'adv'); fixture.submitRegistration(); expect(fixture.adv).not.toHaveBeenCalled(); }); }); describe('when invalid credentials are submitted', () => { beforeEach(() => { setForm('Bob', '', 'good@g.com', 'foobar'); (api.postLogin).and.returnValue(Observable.throw({})); }); it('#adv should not get called', () => { spyOn(fixture, 'adv'); }); }); }); });