import { CommonModule } from '@angular/common'; import { NO_ERRORS_SCHEMA } from '@angular/core'; import { async, ComponentFixture, fakeAsync, inject, TestBed, tick } from '@angular/core/testing'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { BrowserModule, By } from '@angular/platform-browser'; import { Router } from '@angular/router'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { TranslateLoader, TranslateModule } from '@ngx-translate/core'; import { Observable, of as observableOf } from 'rxjs'; import { DSpaceObjectDataService } from '../../../core/data/dspace-object-data.service'; import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service'; import { PaginatedList } from '../../../core/data/paginated-list'; import { RemoteData } from '../../../core/data/remote-data'; import { RequestService } from '../../../core/data/request.service'; import { EPersonDataService } from '../../../core/eperson/eperson-data.service'; import { GroupDataService } from '../../../core/eperson/group-data.service'; import { EPerson } from '../../../core/eperson/models/eperson.model'; import { Group } from '../../../core/eperson/models/group.model'; import { RouteService } from '../../../core/services/route.service'; import { DSpaceObject } from '../../../core/shared/dspace-object.model'; import { PageInfo } from '../../../core/shared/page-info.model'; import { NotificationsService } from '../../../shared/notifications/notifications.service'; import { GroupMock, GroupMock2 } from '../../../shared/testing/group-mock'; import { GroupsRegistryComponent } from './groups-registry.component'; import { EPersonMock, EPersonMock2 } from '../../../shared/testing/eperson.mock'; import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils'; import { TranslateLoaderMock } from '../../../shared/testing/translate-loader.mock'; import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub'; import { routeServiceStub } from '../../../shared/testing/route-service.stub'; import { RouterMock } from '../../../shared/mocks/router.mock'; describe('GroupRegistryComponent', () => { let component: GroupsRegistryComponent; let fixture: ComponentFixture; let ePersonDataServiceStub: any; let groupsDataServiceStub: any; let dsoDataServiceStub: any; let authorizationService: AuthorizationDataService; let mockGroups; let mockEPeople; beforeEach(async(() => { mockGroups = [GroupMock, GroupMock2]; mockEPeople = [EPersonMock, EPersonMock2]; ePersonDataServiceStub = { findAllByHref(href: string): Observable>> { switch (href) { case 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid2/epersons': return createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo({ elementsPerPage: 1, totalElements: 0, totalPages: 0, currentPage: 1 }), [])); case 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid/epersons': return createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo({ elementsPerPage: 1, totalElements: 1, totalPages: 1, currentPage: 1 }), [EPersonMock])); default: return createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo({ elementsPerPage: 1, totalElements: 0, totalPages: 0, currentPage: 1 }), [])); } } }; groupsDataServiceStub = { allGroups: mockGroups, findAllByHref(href: string): Observable>> { switch (href) { case 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid2/groups': return createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo({ elementsPerPage: 1, totalElements: 0, totalPages: 0, currentPage: 1 }), [])); case 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid/groups': return createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo({ elementsPerPage: 1, totalElements: 1, totalPages: 1, currentPage: 1 }), [GroupMock2])); default: return createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo({ elementsPerPage: 1, totalElements: 0, totalPages: 0, currentPage: 1 }), [])); } }, getGroupEditPageRouterLink(group: Group): string { return '/admin/access-control/groups/' + group.id; }, getGroupRegistryRouterLink(): string { return '/admin/access-control/groups'; }, searchGroups(query: string): Observable>> { if (query === '') { return createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo({ elementsPerPage: this.allGroups.length, totalElements: this.allGroups.length, totalPages: 1, currentPage: 1 }), this.allGroups)); } const result = this.allGroups.find((group: Group) => { return (group.id.includes(query)) }); return createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo({ elementsPerPage: [result].length, totalElements: [result].length, totalPages: 1, currentPage: 1 }), [result])); } }; dsoDataServiceStub = { findByHref(href: string): Observable> { return createSuccessfulRemoteDataObject$(undefined); } } authorizationService = jasmine.createSpyObj('authorizationService', { isAuthorized: observableOf(true) }); TestBed.configureTestingModule({ imports: [CommonModule, NgbModule, FormsModule, ReactiveFormsModule, BrowserModule, TranslateModule.forRoot({ loader: { provide: TranslateLoader, useClass: TranslateLoaderMock } }), ], declarations: [GroupsRegistryComponent], providers: [GroupsRegistryComponent, { provide: EPersonDataService, useValue: ePersonDataServiceStub }, { provide: GroupDataService, useValue: groupsDataServiceStub }, { provide: DSpaceObjectDataService, useValue: dsoDataServiceStub }, { provide: NotificationsService, useValue: new NotificationsServiceStub() }, { provide: RouteService, useValue: routeServiceStub }, { provide: Router, useValue: new RouterMock() }, { provide: AuthorizationDataService, useValue: authorizationService }, { provide: RequestService, useValue: jasmine.createSpyObj('requestService', ['removeByHrefSubstring'])} ], schemas: [NO_ERRORS_SCHEMA] }).compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(GroupsRegistryComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create GroupRegistryComponent', inject([GroupsRegistryComponent], (comp: GroupsRegistryComponent) => { expect(comp).toBeDefined(); })); it('should display list of groups', () => { const groupIdsFound = fixture.debugElement.queryAll(By.css('#groups tr td:first-child')); expect(groupIdsFound.length).toEqual(2); mockGroups.map((group: Group) => { expect(groupIdsFound.find((foundEl) => { return (foundEl.nativeElement.textContent.trim() === group.uuid); })).toBeTruthy(); }) }); describe('search', () => { describe('when searching with query', () => { let groupIdsFound; beforeEach(fakeAsync(() => { component.search({ query: GroupMock2.id }); tick(); fixture.detectChanges(); groupIdsFound = fixture.debugElement.queryAll(By.css('#groups tr td:first-child')); })); it('should display search result', () => { expect(groupIdsFound.length).toEqual(1); expect(groupIdsFound.find((foundEl) => { return (foundEl.nativeElement.textContent.trim() === GroupMock2.uuid); })).toBeTruthy(); }); }); }); });