From 2e574ce0bfd4676e5b66a95b552f167cdb255323 Mon Sep 17 00:00:00 2001 From: Marie Verdonck Date: Wed, 18 Mar 2020 13:45:37 +0100 Subject: [PATCH] eperson form tests --- .../epeople-registry.component.html | 4 +- .../epeople-registry.component.spec.ts | 2 +- .../eperson-form.component.spec.ts | 206 ++++++++++++++++++ .../eperson-form/eperson-form.component.ts | 7 +- 4 files changed, 213 insertions(+), 6 deletions(-) create mode 100644 src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts diff --git a/src/app/+admin/admin-access-control/epeople-registry/epeople-registry.component.html b/src/app/+admin/admin-access-control/epeople-registry/epeople-registry.component.html index 33bd5496dc..dd1e8bb62c 100644 --- a/src/app/+admin/admin-access-control/epeople-registry/epeople-registry.component.html +++ b/src/app/+admin/admin-access-control/epeople-registry/epeople-registry.component.html @@ -8,10 +8,10 @@ (cancelForm)="isEPersonFormShown = false">
-
diff --git a/src/app/+admin/admin-access-control/epeople-registry/epeople-registry.component.spec.ts b/src/app/+admin/admin-access-control/epeople-registry/epeople-registry.component.spec.ts index 83499b6e17..8857ae0dd4 100644 --- a/src/app/+admin/admin-access-control/epeople-registry/epeople-registry.component.spec.ts +++ b/src/app/+admin/admin-access-control/epeople-registry/epeople-registry.component.spec.ts @@ -23,7 +23,7 @@ import { NotificationsServiceStub } from '../../../shared/testing/notifications- import { createSuccessfulRemoteDataObject$ } from '../../../shared/testing/utils'; import { EPeopleRegistryComponent } from './epeople-registry.component'; -fdescribe('EPeopleRegistryComponent', () => { +describe('EPeopleRegistryComponent', () => { let component: EPeopleRegistryComponent; let fixture: ComponentFixture; let translateService: TranslateService; diff --git a/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts b/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts new file mode 100644 index 0000000000..df04cd8c2d --- /dev/null +++ b/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts @@ -0,0 +1,206 @@ +import { of as observableOf } from 'rxjs'; +import { CommonModule } from '@angular/common'; +import { NO_ERRORS_SCHEMA } from '@angular/core'; +import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing'; +import { FormsModule, ReactiveFormsModule } from '@angular/forms'; +import { BrowserModule } from '@angular/platform-browser'; +import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; +import { TranslateLoader, TranslateModule, TranslateService } from '@ngx-translate/core'; +import { Observable } from 'rxjs/internal/Observable'; +import { RestResponse } from '../../../../core/cache/response.models'; +import { PaginatedList } from '../../../../core/data/paginated-list'; +import { RemoteData } from '../../../../core/data/remote-data'; +import { FindListOptions } from '../../../../core/data/request.models'; +import { EPersonDataService } from '../../../../core/eperson/eperson-data.service'; +import { EPerson } from '../../../../core/eperson/models/eperson.model'; +import { PageInfo } from '../../../../core/shared/page-info.model'; +import { FormBuilderService } from '../../../../shared/form/builder/form-builder.service'; +import { getMockFormBuilderService } from '../../../../shared/mocks/mock-form-builder-service'; +import { getMockTranslateService } from '../../../../shared/mocks/mock-translate.service'; +import { NotificationsService } from '../../../../shared/notifications/notifications.service'; +import { EPersonMock, EPersonMock2 } from '../../../../shared/testing/eperson-mock'; +import { MockTranslateLoader } from '../../../../shared/testing/mock-translate-loader'; +import { NotificationsServiceStub } from '../../../../shared/testing/notifications-service-stub'; +import { createSuccessfulRemoteDataObject$ } from '../../../../shared/testing/utils'; +import { EPeopleRegistryComponent } from '../epeople-registry.component'; +import { EPersonFormComponent } from './eperson-form.component'; + +describe('EPersonFormComponent', () => { + let component: EPersonFormComponent; + let fixture: ComponentFixture; + let translateService: TranslateService; + let builderService: FormBuilderService; + + const mockEPeople = [EPersonMock, EPersonMock2]; + let ePersonDataServiceStub: any; + + beforeEach(async(() => { + ePersonDataServiceStub = { + activeEPerson: null, + allEpeople: mockEPeople, + getEPeople(): Observable>> { + return createSuccessfulRemoteDataObject$(new PaginatedList(null, this.allEpeople)); + }, + getActiveEPerson(): Observable { + return observableOf(this.activeEPerson); + }, + searchByScope(scope: string, query: string, options: FindListOptions = {}): Observable>> { + if (scope === 'email') { + const result = this.allEpeople.find((ePerson: EPerson) => { + return ePerson.email === query + }); + return createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo(), [result])); + } + if (scope === 'metadata') { + const result = this.allEpeople.find((ePerson: EPerson) => { + return (ePerson.name.includes(query) || ePerson.email.includes(query)) + }); + return createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo(), [result])); + } + return createSuccessfulRemoteDataObject$(new PaginatedList(null, this.allEpeople)); + }, + deleteEPerson(ePerson: EPerson): Observable { + this.allEpeople = this.allEpeople.filter((ePerson2: EPerson) => { + return (ePerson2.uuid !== ePerson.uuid); + }); + return observableOf(true); + }, + create(ePerson: EPerson) { + this.allEpeople = [...this.allEpeople, ePerson] + }, + editEPerson(ePerson: EPerson) { + this.activeEPerson = ePerson; + }, + cancelEditEPerson() { + this.activeEPerson = null; + }, + clearEPersonRequests(): void { + // empty + }, + tryToCreate(ePerson: EPerson): Observable { + this.allEpeople = [...this.allEpeople, ePerson] + return observableOf(new RestResponse(true, 200, 'Success')); + }, + updateEPerson(ePerson: EPerson): Observable { + this.allEpeople.forEach((ePersonInList: EPerson, i: number) => { + if (ePersonInList.id === ePerson.id) { + this.allEpeople[i] = ePerson; + } + }); + return observableOf(new RestResponse(true, 200, 'Success')); + + } + }; + builderService = getMockFormBuilderService(); + translateService = getMockTranslateService(); + TestBed.configureTestingModule({ + imports: [CommonModule, NgbModule, FormsModule, ReactiveFormsModule, BrowserModule, + TranslateModule.forRoot({ + loader: { + provide: TranslateLoader, + useClass: MockTranslateLoader + } + }), + ], + declarations: [EPeopleRegistryComponent, EPersonFormComponent], + providers: [EPersonFormComponent, + { provide: EPersonDataService, useValue: ePersonDataServiceStub }, + { provide: NotificationsService, useValue: new NotificationsServiceStub() }, + { provide: FormBuilderService, useValue: builderService }, + EPeopleRegistryComponent + ], + schemas: [NO_ERRORS_SCHEMA] + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(EPersonFormComponent); + component = fixture.componentInstance; + component.ngOnInit(); + fixture.detectChanges(); + }); + + it('should create EPersonFormComponent', inject([EPersonFormComponent], (comp: EPersonFormComponent) => { + expect(comp).toBeDefined(); + })); + + describe('when submitting the form', () => { + const firstName = 'testName'; + const lastName = 'testLastName'; + const email = 'testEmail@test.com'; + const canLogIn = false; + const requireCertificate = false; + + const expected = Object.assign(new EPerson(), { + metadata: { + 'eperson.firstname': [ + { + value: firstName + } + ], + 'eperson.lastname': [ + { + value: lastName + }, + ], + }, + email: email, + canLogIn: canLogIn, + requireCertificate: requireCertificate, + }); + beforeEach(() => { + spyOn(component.submitForm, 'emit'); + component.firstName.value = firstName; + component.lastName.value = lastName; + component.email.value = email; + component.canLogIn.value = canLogIn; + component.requireCertificate.value = requireCertificate; + }); + describe('without active EPerson', () => { + beforeEach(() => { + spyOn(ePersonDataServiceStub, 'getActiveEPerson').and.returnValue(observableOf(undefined)); + component.onSubmit(); + fixture.detectChanges(); + }); + + it('should emit a new eperson using the correct values', async(() => { + fixture.whenStable().then(() => { + expect(component.submitForm.emit).toHaveBeenCalledWith(expected); + }); + })); + }); + + describe('with an active eperson', () => { + const expectedWithId = Object.assign(new EPerson(), { + metadata: { + 'eperson.firstname': [ + { + value: firstName + } + ], + 'eperson.lastname': [ + { + value: lastName + }, + ], + }, + email: email, + canLogIn: canLogIn, + requireCertificate: requireCertificate, + }); + + beforeEach(() => { + spyOn(ePersonDataServiceStub, 'getActiveEPerson').and.returnValue(observableOf(expectedWithId)); + component.onSubmit(); + fixture.detectChanges(); + }); + + it('should emit the existing eperson using the correct values', async(() => { + fixture.whenStable().then(() => { + expect(component.submitForm.emit).toHaveBeenCalledWith(expectedWithId); + }); + })); + }); + }); + +}); diff --git a/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.ts b/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.ts index 8549cb1f54..d3900aef87 100644 --- a/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.ts +++ b/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.ts @@ -185,9 +185,9 @@ export class EPersonFormComponent implements OnInit, OnDestroy { firstName: eperson != null ? eperson.firstMetadataValue('eperson.firstname') : '', lastName: eperson != null ? eperson.firstMetadataValue('eperson.lastname') : '', email: eperson != null ? eperson.email : '', + canLogIn: eperson != null ? eperson.canLogIn : true, + requireCertificate: eperson != null ? eperson.requireCertificate : false }); - this.formGroup.get('canLogIn').patchValue((eperson != null ? eperson.canLogIn : true)); - this.formGroup.get('requireCertificate').patchValue((eperson != null ? eperson.requireCertificate : false)); })); }); } @@ -198,7 +198,6 @@ export class EPersonFormComponent implements OnInit, OnDestroy { onCancel() { this.epersonService.cancelEditEPerson(); this.cancelForm.emit(); - this.clearFields(); } /** @@ -210,6 +209,7 @@ export class EPersonFormComponent implements OnInit, OnDestroy { onSubmit() { this.epersonService.getActiveEPerson().pipe(take(1)).subscribe( (ePerson: EPerson) => { + console.log('onsubmit ep', ePerson) const values = { metadata: { 'eperson.firstname': [ @@ -241,6 +241,7 @@ export class EPersonFormComponent implements OnInit, OnDestroy { * @param values */ createNewEPerson(values) { + console.log('createNewEPerson(values)', values) const ePersonToCreate = Object.assign(new EPerson(), values); const response = this.epersonService.tryToCreate(ePersonToCreate);