|
| 1 | +import { NO_ERRORS_SCHEMA } from '@angular/core'; |
| 2 | +import { |
| 3 | + ComponentFixture, |
| 4 | + TestBed, |
| 5 | + waitForAsync, |
| 6 | +} from '@angular/core/testing'; |
| 7 | +import { By } from '@angular/platform-browser'; |
| 8 | +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 9 | +import { ActivatedRoute } from '@angular/router'; |
| 10 | +import { APP_CONFIG } from '@dspace/config/app-config.interface'; |
| 11 | +import { BitstreamDataService } from '@dspace/core/data/bitstream-data.service'; |
| 12 | +import { BitstreamFormatDataService } from '@dspace/core/data/bitstream-format-data.service'; |
| 13 | +import { LocaleService } from '@dspace/core/locale/locale.service'; |
| 14 | +import { NotificationsService } from '@dspace/core/notification-system/notifications.service'; |
| 15 | +import { PaginationService } from '@dspace/core/pagination/pagination.service'; |
| 16 | +import { Bitstream } from '@dspace/core/shared/bitstream.model'; |
| 17 | +import { BitstreamFormat } from '@dspace/core/shared/bitstream-format.model'; |
| 18 | +import { Item } from '@dspace/core/shared/item.model'; |
| 19 | +import { ActivatedRouteStub } from '@dspace/core/testing/active-router.stub'; |
| 20 | +import { NotificationsServiceStub } from '@dspace/core/testing/notifications-service.stub'; |
| 21 | +import { PaginationServiceStub } from '@dspace/core/testing/pagination-service.stub'; |
| 22 | +import { TranslateLoaderMock } from '@dspace/core/testing/translate-loader.mock'; |
| 23 | +import { createPaginatedList } from '@dspace/core/testing/utils.test'; |
| 24 | +import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote-data.utils'; |
| 25 | +import { XSRFService } from '@dspace/core/xsrf/xsrf.service'; |
| 26 | +import { provideMockStore } from '@ngrx/store/testing'; |
| 27 | +import { |
| 28 | + TranslateLoader, |
| 29 | + TranslateModule, |
| 30 | +} from '@ngx-translate/core'; |
| 31 | +import { of } from 'rxjs'; |
| 32 | + |
| 33 | +import { environment } from '../../../../../environments/environment'; |
| 34 | +import { ThemedFileDownloadLinkComponent } from '../../../../shared/file-download-link/themed-file-download-link.component'; |
| 35 | +import { PaginationComponent } from '../../../../shared/pagination/pagination.component'; |
| 36 | +import { SearchConfigurationService } from '../../../../shared/search/search-configuration.service'; |
| 37 | +import { getMockThemeService } from '../../../../shared/theme-support/test/theme-service.mock'; |
| 38 | +import { ThemeService } from '../../../../shared/theme-support/theme.service'; |
| 39 | +import { FileSizePipe } from '../../../../shared/utils/file-size-pipe'; |
| 40 | +import { VarDirective } from '../../../../shared/utils/var.directive'; |
| 41 | +import { ExtendedFileSectionComponent } from './extended-file-section.component'; |
| 42 | + |
| 43 | +describe('ExtendedFileSectionComponent', () => { |
| 44 | + let component: ExtendedFileSectionComponent; |
| 45 | + let fixture: ComponentFixture<ExtendedFileSectionComponent>; |
| 46 | + let localeService: any; |
| 47 | + const languageList = ['en;q=1', 'de;q=0.8']; |
| 48 | + const mockLocaleService = jasmine.createSpyObj('LocaleService', { |
| 49 | + getCurrentLanguageCode: jasmine.createSpy('getCurrentLanguageCode'), |
| 50 | + getLanguageCodeList: of(languageList), |
| 51 | + }); |
| 52 | + |
| 53 | + const paginationServiceStub = new PaginationServiceStub(); |
| 54 | + |
| 55 | + const mockItem = Object.assign(new Item(), { |
| 56 | + id: 'test-item-id', |
| 57 | + uuid: 'test-item-id', |
| 58 | + _links: { |
| 59 | + self: { href: 'test-item-selflink' }, |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + const mockBitstream = Object.assign(new Bitstream(), { |
| 64 | + id: 'test-bitstream-id', |
| 65 | + uuid: 'test-bitstream-id', |
| 66 | + name: 'test-bitstream.pdf', |
| 67 | + sizeBytes: 1024, |
| 68 | + _links: { |
| 69 | + self: { href: 'test-bitstream-selflink' }, |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + const mockBitstreamFormat = Object.assign(new BitstreamFormat(), { |
| 74 | + resourceType: 'testResourceType', |
| 75 | + shortDescription: 'testShortDescription', |
| 76 | + description: 'testDescription', |
| 77 | + mimetype: 'test/mimeType', |
| 78 | + }); |
| 79 | + |
| 80 | + const paginatedList = createPaginatedList([mockBitstream]); |
| 81 | + |
| 82 | + paginatedList.pageInfo.elementsPerPage = environment.item.bitstream.pageSize; |
| 83 | + |
| 84 | + const bitstreamDataService = jasmine.createSpyObj('bitstreamDataService', { |
| 85 | + findAllByItemAndBundleName: createSuccessfulRemoteDataObject$(paginatedList), |
| 86 | + }); |
| 87 | + |
| 88 | + const bitstreamFormatDataService = jasmine.createSpyObj('bitstreamFormatDataService', { |
| 89 | + findByBitstream: createSuccessfulRemoteDataObject$(mockBitstreamFormat), |
| 90 | + }); |
| 91 | + |
| 92 | + beforeEach(waitForAsync(() => { |
| 93 | + |
| 94 | + TestBed.configureTestingModule({ |
| 95 | + imports: [ |
| 96 | + TranslateModule.forRoot({ |
| 97 | + loader: { |
| 98 | + provide: TranslateLoader, |
| 99 | + useClass: TranslateLoaderMock, |
| 100 | + }, |
| 101 | + }), |
| 102 | + BrowserAnimationsModule, |
| 103 | + ExtendedFileSectionComponent, |
| 104 | + VarDirective, |
| 105 | + FileSizePipe, |
| 106 | + ], |
| 107 | + providers: [ |
| 108 | + provideMockStore(), |
| 109 | + { provide: XSRFService, useValue: {} }, |
| 110 | + { provide: BitstreamDataService, useValue: bitstreamDataService }, |
| 111 | + { provide: NotificationsService, useValue: new NotificationsServiceStub() }, |
| 112 | + { provide: BitstreamFormatDataService, useValue: bitstreamFormatDataService }, |
| 113 | + { provide: ThemeService, useValue: getMockThemeService() }, |
| 114 | + { provide: SearchConfigurationService, useValue: jasmine.createSpyObj(['getCurrentConfiguration']) }, |
| 115 | + { provide: PaginationService, useValue: paginationServiceStub }, |
| 116 | + { provide: ActivatedRoute, useValue: new ActivatedRouteStub() }, |
| 117 | + { provide: LocaleService, useValue: mockLocaleService }, |
| 118 | + { provide: APP_CONFIG, useValue: environment }, |
| 119 | + ], |
| 120 | + schemas: [NO_ERRORS_SCHEMA], |
| 121 | + }).overrideComponent(ExtendedFileSectionComponent, { |
| 122 | + remove: { |
| 123 | + imports: [ |
| 124 | + PaginationComponent, |
| 125 | + ThemedFileDownloadLinkComponent, |
| 126 | + ], |
| 127 | + }, |
| 128 | + }).compileComponents(); |
| 129 | + })); |
| 130 | + |
| 131 | + beforeEach(waitForAsync(() => { |
| 132 | + localeService = TestBed.inject(LocaleService); |
| 133 | + localeService.getCurrentLanguageCode.and.returnValue(of('en')); |
| 134 | + fixture = TestBed.createComponent(ExtendedFileSectionComponent); |
| 135 | + component = fixture.componentInstance; |
| 136 | + component.item = mockItem; |
| 137 | + fixture.detectChanges(); |
| 138 | + })); |
| 139 | + |
| 140 | + it('should create', () => { |
| 141 | + expect(component).toBeTruthy(); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should set pageSize from appConfig', () => { |
| 145 | + expect(component.pageSize).toEqual(environment.item.bitstream.pageSize); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('when the extended file section gets loaded with bitstreams available', () => { |
| 149 | + it('should contain a list with bitstream', () => { |
| 150 | + const fileSection = fixture.debugElement.queryAll(By.css('.file-section-entry')); |
| 151 | + expect(fileSection.length).toEqual(1); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments