Added test for ItemAuthorizationsComponent

This commit is contained in:
Giuseppe Digilio
2020-04-15 16:11:24 +02:00
parent 00f2aa5e1c
commit e7d0c96a26
2 changed files with 204 additions and 9 deletions

View File

@@ -1,9 +1,197 @@
import { ComponentFixture } from '@angular/core/testing'; import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
import { Component, NO_ERRORS_SCHEMA } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { of as observableOf } from 'rxjs';
import { TranslateModule } from '@ngx-translate/core';
import { cold } from 'jasmine-marbles';
import { ItemAuthorizationsComponent } from './item-authorizations.component'; import { ItemAuthorizationsComponent } from './item-authorizations.component';
import { Bitstream } from '../../../core/shared/bitstream.model';
import { Bundle } from '../../../core/shared/bundle.model';
import { createMockRDPaginatedObs } from '../item-bitstreams/item-bitstreams.component.spec';
import { Item } from '../../../core/shared/item.model';
import { LinkService } from '../../../core/cache/builders/link.service';
import { getMockLinkService } from '../../../shared/mocks/mock-link-service';
import { createSuccessfulRemoteDataObject, createTestComponent } from '../../../shared/testing/utils';
import { getMockResourcePolicyService } from '../../../shared/mocks/mock-resource-policy-service';
import { RouterStub } from '../../../shared/testing/router-stub';
import { PaginatedList } from '../../../core/data/paginated-list';
import { PageInfo } from '../../../core/shared/page-info.model';
describe('ItemAuthorizationsComponent', () => { describe('ItemAuthorizationsComponent test suite', () => {
// let comp: ItemAuthorizationsComponent; let comp: ItemAuthorizationsComponent;
// let fixture: ComponentFixture<ItemAuthorizationsComponent>; let compAsAny: any;
let fixture: ComponentFixture<ItemAuthorizationsComponent>;
let de;
let routerStub: any;
const resourcePolicyService: any = getMockResourcePolicyService();
const linkService: any = getMockLinkService();
const bitstream1 = Object.assign(new Bitstream(), {
id: 'bitstream1',
uuid: 'bitstream1'
});
const bitstream2 = Object.assign(new Bitstream(), {
id: 'bitstream2',
uuid: 'bitstream2'
});
const bitstream3 = Object.assign(new Bitstream(), {
id: 'bitstream3',
uuid: 'bitstream3'
});
const bitstream4 = Object.assign(new Bitstream(), {
id: 'bitstream4',
uuid: 'bitstream4'
});
const bundle1 = Object.assign(new Bundle(), {
id: 'bundle1',
uuid: 'bundle1',
_links: {
self: { href: 'bundle1-selflink' }
},
bitstreams: createMockRDPaginatedObs([bitstream1, bitstream2])
});
const bundle2 = Object.assign(new Bundle(), {
id: 'bundle2',
uuid: 'bundle2',
_links: {
self: { href: 'bundle2-selflink' }
},
bitstreams: createMockRDPaginatedObs([bitstream3, bitstream4])
});
const bundles = [bundle1, bundle2];
const bitstreamList1: PaginatedList<Bitstream> = new PaginatedList(new PageInfo(), [bitstream1, bitstream2]);
const bitstreamList2: PaginatedList<Bitstream> = new PaginatedList(new PageInfo(), [bitstream3, bitstream4]);
const item = Object.assign(new Item(), {
uuid: 'item',
id: 'item',
_links: {
self: { href: 'item-selflink' }
},
bundles: createMockRDPaginatedObs([bundle1, bundle2])
});
const routeStub = {
data: observableOf({
item: createSuccessfulRemoteDataObject(item)
})
};
const epersonService = jasmine.createSpyObj('epersonService', {
findByHref: jasmine.createSpy('findByHref'),
});
const groupService = jasmine.createSpyObj('groupService', {
findByHref: jasmine.createSpy('findByHref'),
});
routerStub = Object.assign(new RouterStub(), {
url: `url/edit`
});
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NoopAnimationsModule,
TranslateModule.forRoot()
],
declarations: [
ItemAuthorizationsComponent,
TestComponent
],
providers: [
{ provide: LinkService, useValue: linkService },
{ provide: ActivatedRoute, useValue: routeStub },
ItemAuthorizationsComponent
],
schemas: [
NO_ERRORS_SCHEMA
]
}).compileComponents();
}));
describe('', () => {
let testComp: TestComponent;
let testFixture: ComponentFixture<TestComponent>;
// synchronous beforeEach
beforeEach(() => {
const html = `
<ds-item-authorizations></ds-item-authorizations>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
afterEach(() => {
testFixture.destroy();
});
it('should create ItemAuthorizationsComponent', inject([ItemAuthorizationsComponent], (app: ItemAuthorizationsComponent) => {
expect(app).toBeDefined();
}));
});
describe('', () => {
beforeEach(() => {
fixture = TestBed.createComponent(ItemAuthorizationsComponent);
comp = fixture.componentInstance;
compAsAny = fixture.componentInstance;
linkService.resolveLink.and.callFake((object, link) => object);
fixture.detectChanges();
});
afterEach(() => {
comp = null;
compAsAny = null;
de = null;
fixture.destroy();
});
it('should init bundles and bitstreams map properly', () => {
expect(compAsAny.subs.length).toBe(2);
expect(compAsAny.bundles$.value).toEqual(bundles);
expect(compAsAny.bundleBitstreamsMap.has('bundle1')).toBeTruthy();
expect(compAsAny.bundleBitstreamsMap.has('bundle2')).toBeTruthy();
let bitstreamList = compAsAny.bundleBitstreamsMap.get('bundle1');
expect(bitstreamList).toBeObservable(cold('(a|)', {
a: bitstreamList1
}));
bitstreamList = compAsAny.bundleBitstreamsMap.get('bundle2');
expect(bitstreamList).toBeObservable(cold('(a|)', {
a: bitstreamList2
}));
});
it('should get the item UUID', () => {
expect(comp.getItemUUID()).toBeObservable(cold('(a|)', {
a: item.id
}));
});
it('should get the item\'s bundle', () => {
expect(comp.getItemBundles()).toBeObservable(cold('a', {
a: bundles
}));
});
});
}); });
// declare a test component
@Component({
selector: 'ds-test-cmp',
template: ``
})
class TestComponent {
}

View File

@@ -4,7 +4,6 @@ import { ActivatedRoute } from '@angular/router';
import { BehaviorSubject, Observable, of as observableOf, Subscription } from 'rxjs'; import { BehaviorSubject, Observable, of as observableOf, Subscription } from 'rxjs';
import { catchError, filter, first, flatMap, map, take } from 'rxjs/operators'; import { catchError, filter, first, flatMap, map, take } from 'rxjs/operators';
import { ResourcePolicyService } from '../../../core/resource-policy/resource-policy.service';
import { PaginatedList } from '../../../core/data/paginated-list'; import { PaginatedList } from '../../../core/data/paginated-list';
import { import {
getFirstSucceededRemoteDataPayload, getFirstSucceededRemoteDataPayload,
@@ -35,6 +34,10 @@ interface BundleBitstreamsMapEntry {
*/ */
export class ItemAuthorizationsComponent implements OnInit, OnDestroy { export class ItemAuthorizationsComponent implements OnInit, OnDestroy {
/**
* A map that contains all bitstream of the item's bundles
* @type {Observable<Map<string, Observable<PaginatedList<Bitstream>>>>}
*/
public bundleBitstreamsMap: Map<string, Observable<PaginatedList<Bitstream>>> = new Map<string, Observable<PaginatedList<Bitstream>>>(); public bundleBitstreamsMap: Map<string, Observable<PaginatedList<Bitstream>>> = new Map<string, Observable<PaginatedList<Bitstream>>>();
/** /**
@@ -59,12 +62,10 @@ export class ItemAuthorizationsComponent implements OnInit, OnDestroy {
* Initialize instance variables * Initialize instance variables
* *
* @param {LinkService} linkService * @param {LinkService} linkService
* @param {ResourcePolicyService} resourcePolicyService
* @param {ActivatedRoute} route * @param {ActivatedRoute} route
*/ */
constructor( constructor(
private linkService: LinkService, private linkService: LinkService,
private resourcePolicyService: ResourcePolicyService,
private route: ActivatedRoute private route: ActivatedRoute
) { ) {
} }
@@ -86,7 +87,10 @@ export class ItemAuthorizationsComponent implements OnInit, OnDestroy {
filter((item: Item) => isNotEmpty(item.bundles)), filter((item: Item) => isNotEmpty(item.bundles)),
flatMap((item: Item) => item.bundles), flatMap((item: Item) => item.bundles),
getFirstSucceededRemoteDataWithNotEmptyPayload(), getFirstSucceededRemoteDataWithNotEmptyPayload(),
catchError(() => observableOf(new PaginatedList(null, []))) catchError((error) => {
console.error(error);
return observableOf(new PaginatedList(null, []))
})
); );
this.subs.push( this.subs.push(
@@ -133,7 +137,10 @@ export class ItemAuthorizationsComponent implements OnInit, OnDestroy {
private getBundleBitstreams(bundle: Bundle): Observable<PaginatedList<Bitstream>> { private getBundleBitstreams(bundle: Bundle): Observable<PaginatedList<Bitstream>> {
return bundle.bitstreams.pipe( return bundle.bitstreams.pipe(
getFirstSucceededRemoteDataPayload(), getFirstSucceededRemoteDataPayload(),
catchError(() => observableOf(new PaginatedList(null, []))) catchError((error) => {
console.error(error);
return observableOf(new PaginatedList(null, []))
})
) )
} }