forked from ordercloud-api/ngx-shopper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split up shared-register component into register and me-update orderc…
- Loading branch information
Crhistian Ramirez
committed
Nov 19, 2018
1 parent
4dd152b
commit 83149f6
Showing
20 changed files
with
373 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
119 changes: 119 additions & 0 deletions
119
src/UI/Buyer/src/app/auth/containers/register/register.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { RegisterComponent } from '@app-buyer/auth/containers/register/register.component'; | ||
import { ReactiveFormsModule } from '@angular/forms'; | ||
import { OcMeService, OcTokenService, MeUser } from '@ordercloud/angular-sdk'; | ||
import { CookieModule } from 'ngx-cookie'; | ||
import { | ||
applicationConfiguration, | ||
AppConfig, | ||
} from '@app-buyer/config/app.config'; | ||
import { InjectionToken, NO_ERRORS_SCHEMA } from '@angular/core'; | ||
import { Router } from '@angular/router'; | ||
import { ToastrService } from 'ngx-toastr'; | ||
import { AppStateService, AppFormErrorService } from '@app-buyer/shared'; | ||
import { of, Subject } from 'rxjs'; | ||
|
||
describe('RegisterComponent', () => { | ||
let component: RegisterComponent; | ||
let fixture: ComponentFixture<RegisterComponent>; | ||
|
||
const appStateService = { userSubject: new Subject<any>() }; | ||
const ocMeService = { | ||
Register: jasmine.createSpy('Register').and.returnValue(of(null)), | ||
}; | ||
const toastrService = { success: jasmine.createSpy('success') }; | ||
const tokenService = { | ||
GetAccess: jasmine.createSpy('GetAccess').and.returnValue('mockToken'), | ||
}; | ||
const router = { navigate: jasmine.createSpy('navigate') }; | ||
const formErrorService = { | ||
hasRequiredError: jasmine.createSpy('hasRequiredError'), | ||
hasInvalidEmailError: jasmine.createSpy('hasInvalidEmailError'), | ||
hasPasswordMismatchError: jasmine.createSpy('hasPasswordMismatchError'), | ||
hasStrongPasswordError: jasmine.createSpy('hasStrongPasswordError'), | ||
displayFormErrors: jasmine.createSpy('displayFormErrors'), | ||
hasPatternError: jasmine.createSpy('hasPatternError'), | ||
}; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [RegisterComponent], | ||
imports: [ReactiveFormsModule, CookieModule.forRoot()], | ||
providers: [ | ||
{ provide: AppFormErrorService, useValue: formErrorService }, | ||
{ provide: Router, useValue: router }, | ||
{ provide: OcTokenService, useValue: tokenService }, | ||
{ provide: OcMeService, useValue: ocMeService }, | ||
{ provide: AppStateService, useValue: appStateService }, | ||
{ provide: ToastrService, useValue: toastrService }, | ||
{ | ||
provide: applicationConfiguration, | ||
useValue: new InjectionToken<AppConfig>('app.config'), | ||
}, | ||
], | ||
schemas: [NO_ERRORS_SCHEMA], | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(RegisterComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
describe('ngOnInit', () => { | ||
beforeEach(() => { | ||
spyOn(component as any, 'setForm'); | ||
}); | ||
it('should call setForm', () => { | ||
component.ngOnInit(); | ||
expect(component['setForm']).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('setForm', () => { | ||
it('should initialize form', () => { | ||
component['setForm'](); | ||
expect(component.form.value).toEqual({ | ||
Username: '', | ||
FirstName: '', | ||
LastName: '', | ||
Email: '', | ||
Phone: '', | ||
Password: '', | ||
ConfirmPassword: '', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('onSubmit', () => { | ||
it('should call displayFormErrors if form is invalid', () => { | ||
component.form.controls.FirstName.setValue(''); | ||
component['onSubmit'](); | ||
expect(formErrorService.displayFormErrors).toHaveBeenCalled(); | ||
}); | ||
it('should call meService.Register', () => { | ||
component.form.controls.Username.setValue('crhistianr'); | ||
component.form.controls.FirstName.setValue('Crhistian'); | ||
component.form.controls.LastName.setValue('Ramirez'); | ||
component.form.controls.Email.setValue( | ||
'crhistian-rawks@my-little-pony.com' | ||
); | ||
component.form.controls.Phone.setValue('555-555-5555'); | ||
component.form.controls.Password.setValue('easyguess123'); | ||
component.form.controls.ConfirmPassword.setValue('easyguess123'); | ||
component['onSubmit'](); | ||
const mockMe = <MeUser>component.form.value; | ||
mockMe.Active = true; | ||
expect(ocMeService.Register).toHaveBeenCalledWith('mockToken', mockMe); | ||
}); | ||
it('should navigate to login', () => { | ||
component['onSubmit'](); | ||
expect(router.navigate).toHaveBeenCalledWith(['/login']); | ||
}); | ||
}); | ||
}); |
97 changes: 97 additions & 0 deletions
97
src/UI/Buyer/src/app/auth/containers/register/register.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Component, OnInit, Inject, OnDestroy } from '@angular/core'; | ||
import { FormGroup, FormBuilder, Validators } from '@angular/forms'; | ||
import { Router } from '@angular/router'; | ||
import { ToastrService } from 'ngx-toastr'; | ||
import { OcMeService, OcTokenService, MeUser } from '@ordercloud/angular-sdk'; | ||
import { | ||
applicationConfiguration, | ||
AppConfig, | ||
} from '@app-buyer/config/app.config'; | ||
import { AppFormErrorService } from '@app-buyer/shared/services/form-error/form-error.service'; | ||
import { AppMatchFieldsValidator } from '@app-buyer/shared/validators/match-fields/match-fields.validator'; | ||
import { RegexService } from '@app-buyer/shared/services/regex/regex.service'; | ||
|
||
@Component({ | ||
selector: 'auth-register', | ||
templateUrl: './register.component.html', | ||
styleUrls: ['./register.component.scss'], | ||
}) | ||
export class RegisterComponent implements OnInit, OnDestroy { | ||
form: FormGroup; | ||
me: MeUser; | ||
alive = true; | ||
|
||
constructor( | ||
private formBuilder: FormBuilder, | ||
private formErrorService: AppFormErrorService, | ||
private ocMeService: OcMeService, | ||
private ocTokenService: OcTokenService, | ||
private router: Router, | ||
private toastrService: ToastrService, | ||
private regexService: RegexService, | ||
@Inject(applicationConfiguration) protected appConfig: AppConfig | ||
) {} | ||
|
||
ngOnInit() { | ||
this.setForm(); | ||
} | ||
|
||
private setForm() { | ||
this.form = this.formBuilder.group( | ||
{ | ||
Username: ['', Validators.required], | ||
FirstName: [ | ||
'', | ||
[ | ||
Validators.required, | ||
Validators.pattern(this.regexService.HumanName), | ||
], | ||
], | ||
LastName: [ | ||
'', | ||
[ | ||
Validators.required, | ||
Validators.pattern(this.regexService.HumanName), | ||
], | ||
], | ||
Email: ['', [Validators.required, Validators.email]], | ||
Phone: ['', Validators.pattern(this.regexService.Phone)], | ||
Password: ['', [Validators.required, Validators.minLength(8)]], | ||
ConfirmPassword: ['', [Validators.required, Validators.minLength(8)]], | ||
}, | ||
{ | ||
validator: AppMatchFieldsValidator('Password', 'ConfirmPassword'), | ||
} | ||
); | ||
} | ||
|
||
onSubmit() { | ||
if (this.form.status === 'INVALID') { | ||
return this.formErrorService.displayFormErrors(this.form); | ||
} | ||
|
||
const me = <MeUser>this.form.value; | ||
me.Active = true; | ||
|
||
this.ocMeService | ||
.Register(this.ocTokenService.GetAccess(), me) | ||
.subscribe(() => { | ||
this.toastrService.success('New User Created'); | ||
this.router.navigate(['/login']); | ||
}); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.alive = false; | ||
} | ||
|
||
// control display of error messages | ||
protected hasRequiredError = (controlName: string): boolean => | ||
this.formErrorService.hasRequiredError(controlName, this.form); | ||
protected hasEmailError = (): boolean => | ||
this.formErrorService.hasInvalidEmailError(this.form.get('Email')); | ||
protected hasPatternError = (controlName: string) => | ||
this.formErrorService.hasPatternError(controlName, this.form); | ||
protected passwordMismatchError = (): boolean => | ||
this.formErrorService.hasPasswordMismatchError(this.form); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.