|
| 1 | +import { ComponentFixture, TestBed, fakeAsync } from '@angular/core/testing'; |
| 2 | +import { CollectionPageComponent } from './collection-page.component'; |
| 3 | +import { ActivatedRoute, Router } from '@angular/router'; |
| 4 | +import { of } from 'rxjs'; |
| 5 | +import { CollectionDataService } from '../core/data/collection-data.service'; |
| 6 | +import { AuthService } from '../core/auth/auth.service'; |
| 7 | +import { PaginationService } from '../core/pagination/pagination.service'; |
| 8 | +import { AuthorizationDataService } from '../core/data/feature-authorization/authorization-data.service'; |
| 9 | +import { DSONameService } from '../core/breadcrumbs/dso-name.service'; |
| 10 | +import { APP_CONFIG } from '../../../src/config/app-config.interface'; |
| 11 | +import { PLATFORM_ID } from '@angular/core'; |
| 12 | +import { ActivatedRouteStub } from '../shared/testing/active-router.stub'; |
| 13 | +import { RouterStub } from '../shared/testing/router.stub'; |
| 14 | +import { environment } from 'src/environments/environment.test'; |
| 15 | +import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils'; |
| 16 | +import { Collection } from '../core/shared/collection.model'; |
| 17 | +import { SearchService } from '../core/shared/search/search.service'; |
| 18 | +import { By } from '@angular/platform-browser'; |
| 19 | +import { FormsModule } from '@angular/forms'; |
| 20 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 21 | +import { TranslateModule } from '@ngx-translate/core'; |
| 22 | +import { VarDirective } from '../shared/utils/var.directive'; |
| 23 | +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 24 | +import { Bitstream } from '../core/shared/bitstream.model'; |
| 25 | + |
| 26 | +describe('CollectionPageComponent', () => { |
| 27 | + let component: CollectionPageComponent; |
| 28 | + let compAsAny: any; |
| 29 | + let fixture: ComponentFixture<CollectionPageComponent>; |
| 30 | + |
| 31 | + let collectionDataServiceSpy: jasmine.SpyObj<CollectionDataService>; |
| 32 | + let authServiceSpy: jasmine.SpyObj<AuthService>; |
| 33 | + let paginationServiceSpy: jasmine.SpyObj<PaginationService>; |
| 34 | + let authorizationDataServiceSpy: jasmine.SpyObj<AuthorizationDataService>; |
| 35 | + let dsoNameServiceSpy: jasmine.SpyObj<DSONameService>; |
| 36 | + let searchServiceSpy: jasmine.SpyObj<SearchService>; |
| 37 | + let aroute = new ActivatedRouteStub(); |
| 38 | + let router = new RouterStub(); |
| 39 | + |
| 40 | + const collection = Object.assign(new Collection(), {}); |
| 41 | + |
| 42 | + beforeEach(async () => { |
| 43 | + authServiceSpy = jasmine.createSpyObj('AuthService', ['isAuthenticated']); |
| 44 | + paginationServiceSpy = jasmine.createSpyObj('PaginationService', ['getCurrentPagination', 'getCurrentSort', 'clearPagination']); |
| 45 | + authorizationDataServiceSpy = jasmine.createSpyObj('AuthorizationDataService', ['isAuthorized']); |
| 46 | + collectionDataServiceSpy = jasmine.createSpyObj('CollectionDataService', ['findById', 'getAuthorizedCollection']); |
| 47 | + searchServiceSpy = jasmine.createSpyObj('SearchService', ['search']); |
| 48 | + dsoNameServiceSpy = jasmine.createSpyObj('DSONameService', ['getName']); |
| 49 | + |
| 50 | + await TestBed.configureTestingModule({ |
| 51 | + imports: [RouterTestingModule, FormsModule, TranslateModule.forRoot(), BrowserAnimationsModule], |
| 52 | + declarations: [CollectionPageComponent, VarDirective], |
| 53 | + providers: [ |
| 54 | + { provide: ActivatedRoute, useValue: aroute }, |
| 55 | + { provide: Router, useValue: router }, |
| 56 | + { provide: CollectionDataService, useValue: collectionDataServiceSpy }, |
| 57 | + { provide: AuthService, useValue: authServiceSpy }, |
| 58 | + { provide: PaginationService, useValue: paginationServiceSpy }, |
| 59 | + { provide: AuthorizationDataService, useValue: authorizationDataServiceSpy }, |
| 60 | + { provide: DSONameService, useValue: dsoNameServiceSpy }, |
| 61 | + { provide: SearchService, useValue: searchServiceSpy }, |
| 62 | + { provide: APP_CONFIG, useValue: environment }, |
| 63 | + { provide: PLATFORM_ID, useValue: 'browser' }, |
| 64 | + ] |
| 65 | + }) |
| 66 | + .compileComponents(); |
| 67 | + }); |
| 68 | + |
| 69 | + beforeEach(() => { |
| 70 | + fixture = TestBed.createComponent(CollectionPageComponent); |
| 71 | + component = fixture.componentInstance; |
| 72 | + compAsAny = component as any; |
| 73 | + component.collectionRD$ = createSuccessfulRemoteDataObject$(Object.assign(new Collection(), { |
| 74 | + name: 'Test Collection', |
| 75 | + })); |
| 76 | + fixture.detectChanges(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should create', () => { |
| 80 | + expect(component).toBeTruthy(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should initialize the component', () => { |
| 84 | + const routeData = { |
| 85 | + dso: createSuccessfulRemoteDataObject$(collection), |
| 86 | + }; |
| 87 | + Object.defineProperty(TestBed.inject(ActivatedRoute), 'data', { |
| 88 | + get: () => of(routeData), |
| 89 | + }); |
| 90 | + authorizationDataServiceSpy.isAuthorized.and.returnValue(of(true)); |
| 91 | + component.ngOnInit(); |
| 92 | + |
| 93 | + expect(component.collectionRD$).toBeDefined(); |
| 94 | + expect(component.logoRD$).toBeDefined(); |
| 95 | + expect(component.isCollectionAdmin$).toBeDefined(); |
| 96 | + expect(compAsAny.paginationChanges$).toBeDefined(); |
| 97 | + expect(component.itemRD$).toBeDefined(); |
| 98 | + expect(component.collectionPageRoute$).toBeDefined(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should display collection name', fakeAsync(() => { |
| 102 | + component.collectionRD$ = createSuccessfulRemoteDataObject$(Object.assign(new Collection(), { |
| 103 | + name: 'Test Collection', |
| 104 | + })); |
| 105 | + fixture.detectChanges(); |
| 106 | + fixture.whenStable().then(() => { |
| 107 | + const collectionNameElement = fixture.debugElement.query(By.css('ds-comcol-page-header')).nativeElement; |
| 108 | + expect(collectionNameElement.textContent.trim()).toBe('Test Collection'); |
| 109 | + }); |
| 110 | + })); |
| 111 | + |
| 112 | + it('should display collection logo if available', () => { |
| 113 | + component.collectionRD$ = createSuccessfulRemoteDataObject$(Object.assign(new Collection(), { |
| 114 | + name: 'Test Collection', |
| 115 | + })); |
| 116 | + component.logoRD$ = createSuccessfulRemoteDataObject$(Object.assign(new Bitstream(), { |
| 117 | + name: 'Test Logo', |
| 118 | + })); |
| 119 | + fixture.detectChanges(); |
| 120 | + |
| 121 | + fixture.whenStable().then(() => { |
| 122 | + const logoElement = fixture.debugElement.query(By.css('ds-comcol-page-logo')).nativeElement; |
| 123 | + expect(logoElement).toBeTruthy(); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should not display collection logo if not available', () => { |
| 128 | + component.collectionRD$ = createSuccessfulRemoteDataObject$(Object.assign(new Collection(), { |
| 129 | + name: 'Test Collection', |
| 130 | + })); |
| 131 | + component.logoRD$ = of({ hasSucceeded: false, payload: null }) as any; |
| 132 | + fixture.detectChanges(); |
| 133 | + |
| 134 | + fixture.whenStable().then(() => { |
| 135 | + const logoElement = fixture.debugElement.query(By.css('ds-comcol-page-logo')); |
| 136 | + expect(logoElement).toBeNull(); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should clear pagination on ngOnDestroy', () => { |
| 141 | + component.ngOnDestroy(); |
| 142 | + expect(paginationServiceSpy.clearPagination).toHaveBeenCalledWith(component.paginationConfig.id); |
| 143 | + }); |
| 144 | +}); |
0 commit comments