forked from hazza/dspace-angular
181 lines
6.4 KiB
TypeScript
181 lines
6.4 KiB
TypeScript
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
|
|
import { TranslateModule } from '@ngx-translate/core';
|
|
import { NO_ERRORS_SCHEMA } from '@angular/core';
|
|
import { By } from '@angular/platform-browser';
|
|
import { RouterTestingModule } from '@angular/router/testing';
|
|
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
|
|
|
|
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
|
|
|
|
import { CommunityPageSubCollectionListComponent } from './community-page-sub-collection-list.component';
|
|
import { Community } from '../../core/shared/community.model';
|
|
import { SharedModule } from '../../shared/shared.module';
|
|
import { CollectionDataService } from '../../core/data/collection-data.service';
|
|
import { FindListOptions } from '../../core/data/request.models';
|
|
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
|
|
import { buildPaginatedList } from '../../core/data/paginated-list.model';
|
|
import { PageInfo } from '../../core/shared/page-info.model';
|
|
import { HostWindowService } from '../../shared/host-window.service';
|
|
import { HostWindowServiceStub } from '../../shared/testing/host-window-service.stub';
|
|
import { SelectableListService } from '../../shared/object-list/selectable-list/selectable-list.service';
|
|
import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model';
|
|
import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model';
|
|
import { of as observableOf } from 'rxjs';
|
|
import { PaginationService } from '../../core/pagination/pagination.service';
|
|
import { getMockThemeService } from '../../shared/mocks/theme-service.mock';
|
|
import { ThemeService } from '../../shared/theme-support/theme.service';
|
|
|
|
describe('CommunityPageSubCollectionList Component', () => {
|
|
let comp: CommunityPageSubCollectionListComponent;
|
|
let fixture: ComponentFixture<CommunityPageSubCollectionListComponent>;
|
|
let collectionDataServiceStub: any;
|
|
let themeService;
|
|
let subCollList = [];
|
|
|
|
const collections = [Object.assign(new Community(), {
|
|
id: '123456789-1',
|
|
metadata: {
|
|
'dc.title': [
|
|
{ language: 'en_US', value: 'Collection 1' }
|
|
]
|
|
}
|
|
}),
|
|
Object.assign(new Community(), {
|
|
id: '123456789-2',
|
|
metadata: {
|
|
'dc.title': [
|
|
{ language: 'en_US', value: 'Collection 2' }
|
|
]
|
|
}
|
|
}),
|
|
Object.assign(new Community(), {
|
|
id: '123456789-3',
|
|
metadata: {
|
|
'dc.title': [
|
|
{ language: 'en_US', value: 'Collection 3' }
|
|
]
|
|
}
|
|
}),
|
|
Object.assign(new Community(), {
|
|
id: '123456789-4',
|
|
metadata: {
|
|
'dc.title': [
|
|
{ language: 'en_US', value: 'Collection 4' }
|
|
]
|
|
}
|
|
}),
|
|
Object.assign(new Community(), {
|
|
id: '123456789-5',
|
|
metadata: {
|
|
'dc.title': [
|
|
{ language: 'en_US', value: 'Collection 5' }
|
|
]
|
|
}
|
|
}),
|
|
Object.assign(new Community(), {
|
|
id: '123456789-6',
|
|
metadata: {
|
|
'dc.title': [
|
|
{ language: 'en_US', value: 'Collection 6' }
|
|
]
|
|
}
|
|
}),
|
|
Object.assign(new Community(), {
|
|
id: '123456789-7',
|
|
metadata: {
|
|
'dc.title': [
|
|
{ language: 'en_US', value: 'Collection 7' }
|
|
]
|
|
}
|
|
})
|
|
];
|
|
|
|
const mockCommunity = Object.assign(new Community(), {
|
|
id: '123456789',
|
|
metadata: {
|
|
'dc.title': [
|
|
{ language: 'en_US', value: 'Test title' }
|
|
]
|
|
}
|
|
});
|
|
|
|
collectionDataServiceStub = {
|
|
findByParent(parentUUID: string, options: FindListOptions = {}) {
|
|
let currentPage = options.currentPage;
|
|
let elementsPerPage = options.elementsPerPage;
|
|
if (currentPage === undefined) {
|
|
currentPage = 1;
|
|
}
|
|
elementsPerPage = 5;
|
|
const startPageIndex = (currentPage - 1) * elementsPerPage;
|
|
let endPageIndex = (currentPage * elementsPerPage);
|
|
if (endPageIndex > subCollList.length) {
|
|
endPageIndex = subCollList.length;
|
|
}
|
|
return createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), subCollList.slice(startPageIndex, endPageIndex)));
|
|
|
|
}
|
|
};
|
|
|
|
const pagination = Object.assign(new PaginationComponentOptions(), { currentPage: 1, pageSize: 20 });
|
|
const sort = new SortOptions('score', SortDirection.DESC);
|
|
const findlistOptions = Object.assign(new FindListOptions(), { currentPage: 1, elementsPerPage: 20 });
|
|
const paginationService = jasmine.createSpyObj('PaginationService', {
|
|
getCurrentPagination: observableOf(pagination),
|
|
getCurrentSort: observableOf(sort),
|
|
getFindListOptions: observableOf(findlistOptions),
|
|
resetPage: {},
|
|
updateRouteWithUrl: {}
|
|
});
|
|
|
|
themeService = getMockThemeService();
|
|
|
|
beforeEach(waitForAsync(() => {
|
|
TestBed.configureTestingModule({
|
|
imports: [
|
|
TranslateModule.forRoot(),
|
|
SharedModule,
|
|
RouterTestingModule.withRoutes([]),
|
|
NgbModule,
|
|
NoopAnimationsModule
|
|
],
|
|
declarations: [CommunityPageSubCollectionListComponent],
|
|
providers: [
|
|
{ provide: CollectionDataService, useValue: collectionDataServiceStub },
|
|
{ provide: HostWindowService, useValue: new HostWindowServiceStub(0) },
|
|
{ provide: PaginationService, useValue: paginationService },
|
|
{ provide: SelectableListService, useValue: {} },
|
|
{ provide: ThemeService, useValue: themeService },
|
|
],
|
|
schemas: [NO_ERRORS_SCHEMA]
|
|
}).compileComponents();
|
|
}));
|
|
|
|
beforeEach(() => {
|
|
fixture = TestBed.createComponent(CommunityPageSubCollectionListComponent);
|
|
comp = fixture.componentInstance;
|
|
comp.community = mockCommunity;
|
|
});
|
|
|
|
it('should display a list of collections', () => {
|
|
subCollList = collections;
|
|
fixture.detectChanges();
|
|
|
|
const collList = fixture.debugElement.queryAll(By.css('li'));
|
|
expect(collList.length).toEqual(5);
|
|
expect(collList[0].nativeElement.textContent).toContain('Collection 1');
|
|
expect(collList[1].nativeElement.textContent).toContain('Collection 2');
|
|
expect(collList[2].nativeElement.textContent).toContain('Collection 3');
|
|
expect(collList[3].nativeElement.textContent).toContain('Collection 4');
|
|
expect(collList[4].nativeElement.textContent).toContain('Collection 5');
|
|
});
|
|
|
|
it('should not display the header when list of collections is empty', () => {
|
|
subCollList = [];
|
|
fixture.detectChanges();
|
|
|
|
const subComHead = fixture.debugElement.queryAll(By.css('h2'));
|
|
expect(subComHead.length).toEqual(0);
|
|
});
|
|
});
|