"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/app/item-page/field-components/collections/collections.component.spec.ts" between
dspace-angular-dspace-7.0.tar.gz and dspace-angular-dspace-7.1.tar.gz

About: dspace-angular is the Angular-based frontend of DSpace, a digital repository system to capture, store, index, preserve and redistribute an organization’s research material in digital formats.

collections.component.spec.ts  (dspace-angular-dspace-7.0):collections.component.spec.ts  (dspace-angular-dspace-7.1)
skipping to change at line 12 skipping to change at line 12
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { By } from '@angular/platform-browser'; import { By } from '@angular/platform-browser';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { RemoteDataBuildService } from '../../../core/cache/builders/remote-data -build.service'; import { RemoteDataBuildService } from '../../../core/cache/builders/remote-data -build.service';
import { CollectionDataService } from '../../../core/data/collection-data.servic e'; import { CollectionDataService } from '../../../core/data/collection-data.servic e';
import { Collection } from '../../../core/shared/collection.model'; import { Collection } from '../../../core/shared/collection.model';
import { Item } from '../../../core/shared/item.model'; import { Item } from '../../../core/shared/item.model';
import { getMockRemoteDataBuildService } from '../../../shared/mocks/remote-data -build.service.mock'; import { getMockRemoteDataBuildService } from '../../../shared/mocks/remote-data -build.service.mock';
import { createFailedRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils'; import { createFailedRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils';
import { CollectionsComponent } from './collections.component'; import { CollectionsComponent } from './collections.component';
import { FindListOptions } from '../../../core/data/request.models';
import { buildPaginatedList, PaginatedList } from '../../../core/data/paginated-
list.model';
import { PageInfo } from '../../../core/shared/page-info.model';
const createMockCollection = (id: string) => Object.assign(new Collection(), {
id: id,
name: `collection-${id}`,
});
let collectionsComponent: CollectionsComponent; const mockItem: Item = new Item();
let fixture: ComponentFixture<CollectionsComponent>;
let collectionDataServiceStub; describe('CollectionsComponent', () => {
let collectionDataService;
const mockCollection1: Collection = Object.assign(new Collection(), { let mockCollection1: Collection;
metadata: { let mockCollection2: Collection;
'dc.description.abstract': [ let mockCollection3: Collection;
{ let mockCollection4: Collection;
language: 'en_US',
value: 'Short description'
}
]
},
_links: {
self: { href: 'collection-selflink' }
}
});
const succeededMockItem: Item = Object.assign(new Item(), {owningCollection: cre let component: CollectionsComponent;
ateSuccessfulRemoteDataObject$(mockCollection1)}); let fixture: ComponentFixture<CollectionsComponent>;
const failedMockItem: Item = Object.assign(new Item(), {owningCollection: create
FailedRemoteDataObject$('error', 500)});
describe('CollectionsComponent', () => {
collectionDataServiceStub = {
findOwningCollectionFor(item: Item) {
if (item === succeededMockItem) {
return createSuccessfulRemoteDataObject$(mockCollection1);
} else {
return createFailedRemoteDataObject$('error', 500);
}
}
};
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {
collectionDataService = jasmine.createSpyObj([
'findOwningCollectionFor',
'findMappedCollectionsFor',
]);
mockCollection1 = createMockCollection('c1');
mockCollection2 = createMockCollection('c2');
mockCollection3 = createMockCollection('c3');
mockCollection4 = createMockCollection('c4');
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [TranslateModule.forRoot()], imports: [TranslateModule.forRoot()],
declarations: [ CollectionsComponent ], declarations: [ CollectionsComponent ],
providers: [ providers: [
{ provide: RemoteDataBuildService, useValue: getMockRemoteDataBuildServi ce()}, { provide: RemoteDataBuildService, useValue: getMockRemoteDataBuildServi ce()},
{ provide: CollectionDataService, useValue: collectionDataServiceStub }, { provide: CollectionDataService, useValue: collectionDataService },
], ],
schemas: [ NO_ERRORS_SCHEMA ] schemas: [ NO_ERRORS_SCHEMA ]
}).overrideComponent(CollectionsComponent, { }).overrideComponent(CollectionsComponent, {
set: { changeDetection: ChangeDetectionStrategy.Default } set: { changeDetection: ChangeDetectionStrategy.Default }
}).compileComponents(); }).compileComponents();
})); }));
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {
fixture = TestBed.createComponent(CollectionsComponent); fixture = TestBed.createComponent(CollectionsComponent);
collectionsComponent = fixture.componentInstance; component = fixture.componentInstance;
collectionsComponent.label = 'test.test'; component.item = mockItem;
collectionsComponent.separator = '<br/>'; component.label = 'test.test';
component.separator = '<br/>';
component.pageSize = 2;
})); }));
describe('When the requested item request has succeeded', () => { describe('when the item has only an owning collection', () => {
let mockPage1: PaginatedList<Collection>;
beforeEach(() => {
mockPage1 = buildPaginatedList(Object.assign(new PageInfo(), {
currentPage: 1,
elementsPerPage: 2,
totalPages: 0,
totalElements: 0,
}), []);
collectionDataService.findOwningCollectionFor.and.returnValue(createSucces
sfulRemoteDataObject$(mockCollection1));
collectionDataService.findMappedCollectionsFor.and.returnValue(createSucce
ssfulRemoteDataObject$(mockPage1));
fixture.detectChanges();
});
it('should display the owning collection', () => {
const collectionFields = fixture.debugElement.queryAll(By.css('ds-metadata
-field-wrapper div.collections a'));
const loadMoreBtn = fixture.debugElement.query(By.css('ds-metadata-field-w
rapper .load-more-btn'));
expect(collectionDataService.findOwningCollectionFor).toHaveBeenCalledOnce
With(mockItem);
expect(collectionDataService.findMappedCollectionsFor).toHaveBeenCalledOnc
eWith(mockItem, Object.assign(new FindListOptions(), {
elementsPerPage: 2,
currentPage: 1,
}));
expect(collectionFields.length).toBe(1);
expect(collectionFields[0].nativeElement.textContent).toEqual('collection-
c1');
expect(component.lastPage$.getValue()).toBe(1);
expect(component.hasMore$.getValue()).toBe(false);
expect(component.isLoading$.getValue()).toBe(false);
expect(loadMoreBtn).toBeNull();
});
});
describe('when the item has an owning collection and one mapped collection', (
) => {
let mockPage1: PaginatedList<Collection>;
beforeEach(() => { beforeEach(() => {
collectionsComponent.item = succeededMockItem; mockPage1 = buildPaginatedList(Object.assign(new PageInfo(), {
currentPage: 1,
elementsPerPage: 2,
totalPages: 1,
totalElements: 1,
}), [mockCollection2]);
collectionDataService.findOwningCollectionFor.and.returnValue(createSucces
sfulRemoteDataObject$(mockCollection1));
collectionDataService.findMappedCollectionsFor.and.returnValue(createSucce
ssfulRemoteDataObject$(mockPage1));
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should show the collection', () => { it('should display the owning collection and the mapped collection', () => {
const collectionField = fixture.debugElement.query(By.css('ds-metadata-fie const collectionFields = fixture.debugElement.queryAll(By.css('ds-metadata
ld-wrapper div.collections')); -field-wrapper div.collections a'));
expect(collectionField).not.toBeNull(); const loadMoreBtn = fixture.debugElement.query(By.css('ds-metadata-field-w
rapper .load-more-btn'));
expect(collectionDataService.findOwningCollectionFor).toHaveBeenCalledOnce
With(mockItem);
expect(collectionDataService.findMappedCollectionsFor).toHaveBeenCalledOnc
eWith(mockItem, Object.assign(new FindListOptions(), {
elementsPerPage: 2,
currentPage: 1,
}));
expect(collectionFields.length).toBe(2);
expect(collectionFields[0].nativeElement.textContent).toEqual('collection-
c1');
expect(collectionFields[1].nativeElement.textContent).toEqual('collection-
c2');
expect(component.lastPage$.getValue()).toBe(1);
expect(component.hasMore$.getValue()).toBe(false);
expect(component.isLoading$.getValue()).toBe(false);
expect(loadMoreBtn).toBeNull();
}); });
}); });
describe('When the requested item request has failed', () => { describe('when the item has an owning collection and multiple mapped collectio
ns', () => {
let mockPage1: PaginatedList<Collection>;
let mockPage2: PaginatedList<Collection>;
beforeEach(() => { beforeEach(() => {
collectionsComponent.item = failedMockItem; mockPage1 = buildPaginatedList(Object.assign(new PageInfo(), {
currentPage: 1,
elementsPerPage: 2,
totalPages: 2,
totalElements: 3,
}), [mockCollection2, mockCollection3]);
mockPage2 = buildPaginatedList(Object.assign(new PageInfo(), {
currentPage: 2,
elementsPerPage: 2,
totalPages: 2,
totalElements: 1,
}), [mockCollection4]);
collectionDataService.findOwningCollectionFor.and.returnValue(createSucces
sfulRemoteDataObject$(mockCollection1));
collectionDataService.findMappedCollectionsFor.and.returnValues(
createSuccessfulRemoteDataObject$(mockPage1),
createSuccessfulRemoteDataObject$(mockPage2),
);
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should not show the collection', () => { it('should display the owning collection, two mapped collections and a load
const collectionField = fixture.debugElement.query(By.css('ds-metadata-fie more button', () => {
ld-wrapper div.collections')); const collectionFields = fixture.debugElement.queryAll(By.css('ds-metadata
expect(collectionField).toBeNull(); -field-wrapper div.collections a'));
const loadMoreBtn = fixture.debugElement.query(By.css('ds-metadata-field-w
rapper .load-more-btn'));
expect(collectionDataService.findOwningCollectionFor).toHaveBeenCalledOnce
With(mockItem);
expect(collectionDataService.findMappedCollectionsFor).toHaveBeenCalledOnc
eWith(mockItem, Object.assign(new FindListOptions(), {
elementsPerPage: 2,
currentPage: 1,
}));
expect(collectionFields.length).toBe(3);
expect(collectionFields[0].nativeElement.textContent).toEqual('collection-
c1');
expect(collectionFields[1].nativeElement.textContent).toEqual('collection-
c2');
expect(collectionFields[2].nativeElement.textContent).toEqual('collection-
c3');
expect(component.lastPage$.getValue()).toBe(1);
expect(component.hasMore$.getValue()).toBe(true);
expect(component.isLoading$.getValue()).toBe(false);
expect(loadMoreBtn).toBeTruthy();
});
describe('when the load more button is clicked', () => {
beforeEach(() => {
const loadMoreBtn = fixture.debugElement.query(By.css('ds-metadata-field
-wrapper .load-more-btn'));
loadMoreBtn.nativeElement.click();
fixture.detectChanges();
});
it('should display the owning collection and three mapped collections', ()
=> {
const collectionFields = fixture.debugElement.queryAll(By.css('ds-metada
ta-field-wrapper div.collections a'));
const loadMoreBtn = fixture.debugElement.query(By.css('ds-metadata-field
-wrapper .load-more-btn'));
expect(collectionDataService.findOwningCollectionFor).toHaveBeenCalledOn
ceWith(mockItem);
expect(collectionDataService.findMappedCollectionsFor).toHaveBeenCalledT
imes(2);
expect(collectionDataService.findMappedCollectionsFor).toHaveBeenCalledW
ith(mockItem, Object.assign(new FindListOptions(), {
elementsPerPage: 2,
currentPage: 1,
}));
expect(collectionDataService.findMappedCollectionsFor).toHaveBeenCalledW
ith(mockItem, Object.assign(new FindListOptions(), {
elementsPerPage: 2,
currentPage: 2,
}));
expect(collectionFields.length).toBe(4);
expect(collectionFields[0].nativeElement.textContent).toEqual('collectio
n-c1');
expect(collectionFields[1].nativeElement.textContent).toEqual('collectio
n-c2');
expect(collectionFields[2].nativeElement.textContent).toEqual('collectio
n-c3');
expect(collectionFields[3].nativeElement.textContent).toEqual('collectio
n-c4');
expect(component.lastPage$.getValue()).toBe(2);
expect(component.hasMore$.getValue()).toBe(false);
expect(component.isLoading$.getValue()).toBe(false);
expect(loadMoreBtn).toBeNull();
});
}); });
}); });
describe('when the request for the owning collection fails', () => {
let mockPage1: PaginatedList<Collection>;
beforeEach(() => {
mockPage1 = buildPaginatedList(Object.assign(new PageInfo(), {
currentPage: 1,
elementsPerPage: 2,
totalPages: 1,
totalElements: 1,
}), [mockCollection2]);
collectionDataService.findOwningCollectionFor.and.returnValue(createFailed
RemoteDataObject$());
collectionDataService.findMappedCollectionsFor.and.returnValue(createSucce
ssfulRemoteDataObject$(mockPage1));
fixture.detectChanges();
});
it('should display the mapped collection only', () => {
const collectionFields = fixture.debugElement.queryAll(By.css('ds-metadata
-field-wrapper div.collections a'));
const loadMoreBtn = fixture.debugElement.query(By.css('ds-metadata-field-w
rapper .load-more-btn'));
expect(collectionDataService.findOwningCollectionFor).toHaveBeenCalledOnce
With(mockItem);
expect(collectionDataService.findMappedCollectionsFor).toHaveBeenCalledOnc
eWith(mockItem, Object.assign(new FindListOptions(), {
elementsPerPage: 2,
currentPage: 1,
}));
expect(collectionFields.length).toBe(1);
expect(collectionFields[0].nativeElement.textContent).toEqual('collection-
c2');
expect(component.lastPage$.getValue()).toBe(1);
expect(component.hasMore$.getValue()).toBe(false);
expect(component.isLoading$.getValue()).toBe(false);
expect(loadMoreBtn).toBeNull();
});
});
describe('when the request for the mapped collections fails', () => {
beforeEach(() => {
collectionDataService.findOwningCollectionFor.and.returnValue(createSucces
sfulRemoteDataObject$(mockCollection1));
collectionDataService.findMappedCollectionsFor.and.returnValue(createFaile
dRemoteDataObject$());
fixture.detectChanges();
});
it('should display the owning collection only', () => {
const collectionFields = fixture.debugElement.queryAll(By.css('ds-metadata
-field-wrapper div.collections a'));
const loadMoreBtn = fixture.debugElement.query(By.css('ds-metadata-field-w
rapper .load-more-btn'));
expect(collectionDataService.findOwningCollectionFor).toHaveBeenCalledOnce
With(mockItem);
expect(collectionDataService.findMappedCollectionsFor).toHaveBeenCalledOnc
eWith(mockItem, Object.assign(new FindListOptions(), {
elementsPerPage: 2,
currentPage: 1,
}));
expect(collectionFields.length).toBe(1);
expect(collectionFields[0].nativeElement.textContent).toEqual('collection-
c1');
expect(component.lastPage$.getValue()).toBe(0);
expect(component.hasMore$.getValue()).toBe(true);
expect(component.isLoading$.getValue()).toBe(false);
expect(loadMoreBtn).toBeTruthy();
});
});
describe('when both requests fail', () => {
beforeEach(() => {
collectionDataService.findOwningCollectionFor.and.returnValue(createFailed
RemoteDataObject$());
collectionDataService.findMappedCollectionsFor.and.returnValue(createFaile
dRemoteDataObject$());
fixture.detectChanges();
});
it('should display no collections', () => {
const collectionFields = fixture.debugElement.queryAll(By.css('ds-metadata
-field-wrapper div.collections a'));
const loadMoreBtn = fixture.debugElement.query(By.css('ds-metadata-field-w
rapper .load-more-btn'));
expect(collectionDataService.findOwningCollectionFor).toHaveBeenCalledOnce
With(mockItem);
expect(collectionDataService.findMappedCollectionsFor).toHaveBeenCalledOnc
eWith(mockItem, Object.assign(new FindListOptions(), {
elementsPerPage: 2,
currentPage: 1,
}));
expect(collectionFields.length).toBe(0);
expect(component.lastPage$.getValue()).toBe(0);
expect(component.hasMore$.getValue()).toBe(true);
expect(component.isLoading$.getValue()).toBe(false);
expect(loadMoreBtn).toBeTruthy();
});
});
}); });
 End of changes. 16 change blocks. 
47 lines changed or deleted 332 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)