eperson-form.component.spec.ts (dspace-angular-dspace-7.0) | : | eperson-form.component.spec.ts (dspace-angular-dspace-7.1) | ||
---|---|---|---|---|
skipping to change at line 31 | skipping to change at line 31 | |||
import { NotificationsServiceStub } from '../../../shared/testing/notifications- service.stub'; | import { NotificationsServiceStub } from '../../../shared/testing/notifications- service.stub'; | |||
import { TranslateLoaderMock } from '../../../shared/mocks/translate-loader.mock '; | import { TranslateLoaderMock } from '../../../shared/mocks/translate-loader.mock '; | |||
import { AuthService } from '../../../core/auth/auth.service'; | import { AuthService } from '../../../core/auth/auth.service'; | |||
import { AuthServiceStub } from '../../../shared/testing/auth-service.stub'; | import { AuthServiceStub } from '../../../shared/testing/auth-service.stub'; | |||
import { AuthorizationDataService } from '../../../core/data/feature-authorizati on/authorization-data.service'; | import { AuthorizationDataService } from '../../../core/data/feature-authorizati on/authorization-data.service'; | |||
import { GroupDataService } from '../../../core/eperson/group-data.service'; | import { GroupDataService } from '../../../core/eperson/group-data.service'; | |||
import { createPaginatedList } from '../../../shared/testing/utils.test'; | import { createPaginatedList } from '../../../shared/testing/utils.test'; | |||
import { RequestService } from '../../../core/data/request.service'; | import { RequestService } from '../../../core/data/request.service'; | |||
import { PaginationService } from '../../../core/pagination/pagination.service'; | import { PaginationService } from '../../../core/pagination/pagination.service'; | |||
import { PaginationServiceStub } from '../../../shared/testing/pagination-servic e.stub'; | import { PaginationServiceStub } from '../../../shared/testing/pagination-servic e.stub'; | |||
import { FormArray, FormControl, FormGroup,Validators, NG_VALIDATORS, NG_ASYNC_V | ||||
ALIDATORS } from '@angular/forms'; | ||||
import { ValidateEmailNotTaken } from './validators/email-taken.validator'; | ||||
describe('EPersonFormComponent', () => { | describe('EPersonFormComponent', () => { | |||
let component: EPersonFormComponent; | let component: EPersonFormComponent; | |||
let fixture: ComponentFixture<EPersonFormComponent>; | let fixture: ComponentFixture<EPersonFormComponent>; | |||
let builderService: FormBuilderService; | let builderService: FormBuilderService; | |||
let mockEPeople; | let mockEPeople; | |||
let ePersonDataServiceStub: any; | let ePersonDataServiceStub: any; | |||
let authService: AuthServiceStub; | let authService: AuthServiceStub; | |||
let authorizationService: AuthorizationDataService; | let authorizationService: AuthorizationDataService; | |||
skipping to change at line 100 | skipping to change at line 102 | |||
clearEPersonRequests(): void { | clearEPersonRequests(): void { | |||
// empty | // empty | |||
}, | }, | |||
updateEPerson(ePerson: EPerson): Observable<RemoteData<EPerson>> { | updateEPerson(ePerson: EPerson): Observable<RemoteData<EPerson>> { | |||
this.allEpeople.forEach((ePersonInList: EPerson, i: number) => { | this.allEpeople.forEach((ePersonInList: EPerson, i: number) => { | |||
if (ePersonInList.id === ePerson.id) { | if (ePersonInList.id === ePerson.id) { | |||
this.allEpeople[i] = ePerson; | this.allEpeople[i] = ePerson; | |||
} | } | |||
}); | }); | |||
return createSuccessfulRemoteDataObject$(ePerson); | return createSuccessfulRemoteDataObject$(ePerson); | |||
}, | ||||
getEPersonByEmail(email): Observable<RemoteData<EPerson>> { | ||||
return createSuccessfulRemoteDataObject$(null); | ||||
} | } | |||
}; | }; | |||
builderService = getMockFormBuilderService(); | builderService = Object.assign(getMockFormBuilderService(),{ | |||
createFormGroup(formModel, options = null) { | ||||
const controls = {}; | ||||
formModel.forEach( model => { | ||||
model.parent = parent; | ||||
const controlModel = model; | ||||
const controlState = { value: controlModel.value, disabled: controlM | ||||
odel.disabled }; | ||||
const controlOptions = this.createAbstractControlOptions(controlMode | ||||
l.validators, controlModel.asyncValidators, controlModel.updateOn); | ||||
controls[model.id] = new FormControl(controlState, controlOptions); | ||||
}); | ||||
return new FormGroup(controls, options); | ||||
}, | ||||
createAbstractControlOptions(validatorsConfig = null, asyncValidatorsConfi | ||||
g = null, updateOn = null) { | ||||
return { | ||||
validators: validatorsConfig !== null ? this.getValidators(validator | ||||
sConfig) : null, | ||||
}; | ||||
}, | ||||
getValidators(validatorsConfig) { | ||||
return this.getValidatorFns(validatorsConfig); | ||||
}, | ||||
getValidatorFns(validatorsConfig, validatorsToken = this._NG_VALIDATORS) { | ||||
let validatorFns = []; | ||||
if (this.isObject(validatorsConfig)) { | ||||
validatorFns = Object.keys(validatorsConfig).map(validatorConfigKey | ||||
=> { | ||||
const validatorConfigValue = validatorsConfig[validatorConfigKey | ||||
]; | ||||
if (this.isValidatorDescriptor(validatorConfigValue)) { | ||||
const descriptor = validatorConfigValue; | ||||
return this.getValidatorFn(descriptor.name, descriptor.args, | ||||
validatorsToken); | ||||
} | ||||
return this.getValidatorFn(validatorConfigKey, validatorConfigVa | ||||
lue, validatorsToken); | ||||
}); | ||||
} | ||||
return validatorFns; | ||||
}, | ||||
getValidatorFn(validatorName, validatorArgs = null, validatorsToken = this | ||||
._NG_VALIDATORS) { | ||||
let validatorFn; | ||||
if (Validators.hasOwnProperty(validatorName)) { // Built-in Angular Vali | ||||
dators | ||||
validatorFn = Validators[validatorName]; | ||||
} else { // Custom Validators | ||||
if (this._DYNAMIC_VALIDATORS && this._DYNAMIC_VALIDATORS.has(validat | ||||
orName)) { | ||||
validatorFn = this._DYNAMIC_VALIDATORS.get(validatorName); | ||||
} else if (validatorsToken) { | ||||
validatorFn = validatorsToken.find(validator => validator.name = | ||||
== validatorName); | ||||
} | ||||
} | ||||
if (validatorFn === undefined) { // throw when no validator could be res | ||||
olved | ||||
throw new Error(`validator '${validatorName}' is not provided via NG | ||||
_VALIDATORS, NG_ASYNC_VALIDATORS or DYNAMIC_FORM_VALIDATORS`); | ||||
} | ||||
if (validatorArgs !== null) { | ||||
return validatorFn(validatorArgs); | ||||
} | ||||
return validatorFn; | ||||
}, | ||||
isValidatorDescriptor(value) { | ||||
if (this.isObject(value)) { | ||||
return value.hasOwnProperty('name') && value.hasOwnProperty('args' | ||||
); | ||||
} | ||||
return false; | ||||
}, | ||||
isObject(value) { | ||||
return typeof value === 'object' && value !== null; | ||||
} | ||||
}); | ||||
authService = new AuthServiceStub(); | authService = new AuthServiceStub(); | |||
authorizationService = jasmine.createSpyObj('authorizationService', { | authorizationService = jasmine.createSpyObj('authorizationService', { | |||
isAuthorized: observableOf(true) | isAuthorized: observableOf(true), | |||
}); | }); | |||
groupsDataService = jasmine.createSpyObj('groupsDataService', { | groupsDataService = jasmine.createSpyObj('groupsDataService', { | |||
findAllByHref: createSuccessfulRemoteDataObject$(createPaginatedList([])), | findAllByHref: createSuccessfulRemoteDataObject$(createPaginatedList([])), | |||
getGroupRegistryRouterLink: '' | getGroupRegistryRouterLink: '' | |||
}); | }); | |||
paginationService = new PaginationServiceStub(); | paginationService = new PaginationServiceStub(); | |||
TestBed.configureTestingModule({ | TestBed.configureTestingModule({ | |||
imports: [CommonModule, NgbModule, FormsModule, ReactiveFormsModule, Brows erModule, | imports: [CommonModule, NgbModule, FormsModule, ReactiveFormsModule, Brows erModule, | |||
TranslateModule.forRoot({ | TranslateModule.forRoot({ | |||
skipping to change at line 147 | skipping to change at line 215 | |||
beforeEach(() => { | beforeEach(() => { | |||
fixture = TestBed.createComponent(EPersonFormComponent); | fixture = TestBed.createComponent(EPersonFormComponent); | |||
component = fixture.componentInstance; | component = fixture.componentInstance; | |||
fixture.detectChanges(); | fixture.detectChanges(); | |||
}); | }); | |||
it('should create EPersonFormComponent', () => { | it('should create EPersonFormComponent', () => { | |||
expect(component).toBeDefined(); | expect(component).toBeDefined(); | |||
}); | }); | |||
describe('check form validation', () => { | ||||
let firstName; | ||||
let lastName; | ||||
let email; | ||||
let canLogIn; | ||||
let requireCertificate; | ||||
let expected; | ||||
beforeEach(() => { | ||||
firstName = 'testName'; | ||||
lastName = 'testLastName'; | ||||
email = 'testEmail@test.com'; | ||||
canLogIn = false; | ||||
requireCertificate = false; | ||||
expected = Object.assign(new EPerson(), { | ||||
metadata: { | ||||
'eperson.firstname': [ | ||||
{ | ||||
value: firstName | ||||
} | ||||
], | ||||
'eperson.lastname': [ | ||||
{ | ||||
value: lastName | ||||
}, | ||||
], | ||||
}, | ||||
email: email, | ||||
canLogIn: canLogIn, | ||||
requireCertificate: requireCertificate, | ||||
}); | ||||
spyOn(component.submitForm, 'emit'); | ||||
component.canLogIn.value = canLogIn; | ||||
component.requireCertificate.value = requireCertificate; | ||||
fixture.detectChanges(); | ||||
component.initialisePage(); | ||||
fixture.detectChanges(); | ||||
}); | ||||
describe('firstName, lastName and email should be required', () => { | ||||
it('form should be invalid because the firstName is required', waitForAsyn | ||||
c(() => { | ||||
fixture.whenStable().then(() => { | ||||
expect(component.formGroup.controls.firstName.valid).toBeFalse(); | ||||
expect(component.formGroup.controls.firstName.errors.required).toBeTru | ||||
e(); | ||||
}); | ||||
})); | ||||
it('form should be invalid because the lastName is required', waitForAsync | ||||
(() => { | ||||
fixture.whenStable().then(() => { | ||||
expect(component.formGroup.controls.lastName.valid).toBeFalse(); | ||||
expect(component.formGroup.controls.lastName.errors.required).toBeTrue | ||||
(); | ||||
}); | ||||
})); | ||||
it('form should be invalid because the email is required', waitForAsync(() | ||||
=> { | ||||
fixture.whenStable().then(() => { | ||||
expect(component.formGroup.controls.email.valid).toBeFalse(); | ||||
expect(component.formGroup.controls.email.errors.required).toBeTrue(); | ||||
}); | ||||
})); | ||||
}); | ||||
describe('after inserting information firstName,lastName and email not requi | ||||
red', () => { | ||||
beforeEach(() => { | ||||
component.formGroup.controls.firstName.setValue('test'); | ||||
component.formGroup.controls.lastName.setValue('test'); | ||||
component.formGroup.controls.email.setValue('test@test.com'); | ||||
fixture.detectChanges(); | ||||
}); | ||||
it('firstName should be valid because the firstName is set', waitForAsync( | ||||
() => { | ||||
fixture.whenStable().then(() => { | ||||
expect(component.formGroup.controls.firstName.valid).toBeTrue(); | ||||
expect(component.formGroup.controls.firstName.errors).toBeNull(); | ||||
}); | ||||
})); | ||||
it('lastName should be valid because the lastName is set', waitForAsync(() | ||||
=> { | ||||
fixture.whenStable().then(() => { | ||||
expect(component.formGroup.controls.lastName.valid).toBeTrue(); | ||||
expect(component.formGroup.controls.lastName.errors).toBeNull(); | ||||
}); | ||||
})); | ||||
it('email should be valid because the email is set', waitForAsync(() => { | ||||
fixture.whenStable().then(() => { | ||||
expect(component.formGroup.controls.email.valid).toBeTrue(); | ||||
expect(component.formGroup.controls.email.errors).toBeNull(); | ||||
}); | ||||
})); | ||||
}); | ||||
describe('after inserting email wrong should show pattern validation error', | ||||
() => { | ||||
beforeEach(() => { | ||||
component.formGroup.controls.email.setValue('test@test'); | ||||
fixture.detectChanges(); | ||||
}); | ||||
it('email should not be valid because the email pattern', waitForAsync(() | ||||
=> { | ||||
fixture.whenStable().then(() => { | ||||
expect(component.formGroup.controls.email.valid).toBeFalse(); | ||||
expect(component.formGroup.controls.email.errors.pattern).toBeTruthy() | ||||
; | ||||
}); | ||||
})); | ||||
}); | ||||
describe('after already utilized email', () => { | ||||
beforeEach(() => { | ||||
const ePersonServiceWithEperson = Object.assign(ePersonDataServiceStub,{ | ||||
getEPersonByEmail(): Observable<RemoteData<EPerson>> { | ||||
return createSuccessfulRemoteDataObject$(EPersonMock); | ||||
} | ||||
}); | ||||
component.formGroup.controls.email.setValue('test@test.com'); | ||||
component.formGroup.controls.email.setAsyncValidators(ValidateEmailNotTa | ||||
ken.createValidator(ePersonServiceWithEperson)); | ||||
fixture.detectChanges(); | ||||
}); | ||||
it('email should not be valid because email is already taken', waitForAsyn | ||||
c(() => { | ||||
fixture.whenStable().then(() => { | ||||
expect(component.formGroup.controls.email.valid).toBeFalse(); | ||||
expect(component.formGroup.controls.email.errors.emailTaken).toBeTruth | ||||
y(); | ||||
}); | ||||
})); | ||||
}); | ||||
}); | ||||
describe('when submitting the form', () => { | describe('when submitting the form', () => { | |||
let firstName; | let firstName; | |||
let lastName; | let lastName; | |||
let email; | let email; | |||
let canLogIn; | let canLogIn; | |||
let requireCertificate; | let requireCertificate; | |||
let expected; | let expected; | |||
beforeEach(() => { | beforeEach(() => { | |||
firstName = 'testName'; | firstName = 'testName'; | |||
End of changes. 5 change blocks. | ||||
2 lines changed or deleted | 222 lines changed or added |