import { BitstreamFormatsComponent } from './bitstream-formats.component'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { of as observableOf } from 'rxjs'; import { RemoteData } from '../../../core/data/remote-data'; import { PaginatedList } from '../../../core/data/paginated-list'; import { CommonModule } from '@angular/common'; import { RouterTestingModule } from '@angular/router/testing'; import { TranslateModule } from '@ngx-translate/core'; import { By } from '@angular/platform-browser'; import { PaginationComponent } from '../../../shared/pagination/pagination.component'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { EnumKeysPipe } from '../../../shared/utils/enum-keys-pipe'; import { HostWindowService } from '../../../shared/host-window.service'; import { HostWindowServiceStub } from '../../../shared/testing/host-window-service-stub'; import { BitstreamFormatDataService } from '../../../core/data/bitstream-format-data.service'; import { NotificationsService } from '../../../shared/notifications/notifications.service'; import { NotificationsServiceStub } from '../../../shared/testing/notifications-service-stub'; import { BitstreamFormat } from '../../../core/shared/bitstream-format.model'; import { ResourceType } from '../../../core/shared/resource-type'; import { BitstreamFormatSupportLevel } from '../../../core/shared/bitstream-format-support-level'; import { cold, getTestScheduler, hot } from 'jasmine-marbles'; import { TestScheduler } from 'rxjs/testing'; describe('BitstreamFormatsComponent', () => { let comp: BitstreamFormatsComponent; let fixture: ComponentFixture; let bitstreamFormatService; let scheduler: TestScheduler; let notificationsServiceStub; const bitstreamFormat1: BitstreamFormat = { uuid: 'test-uuid-1', id: 'test-uuid-1', shortDescription: 'Unknown', description: 'Unknown data format', mimetype: 'application/octet-stream', supportLevel: BitstreamFormatSupportLevel.Unknown, internal: false, extensions: null, type: ResourceType.BitstreamFormat, self: 'self-link' }; const bitstreamFormat2: BitstreamFormat = { uuid: 'test-uuid-2', id: 'test-uuid-2', shortDescription: 'License', description: 'Item-specific license agreed upon to submission', mimetype: 'text/plain; charset=utf-8', supportLevel: BitstreamFormatSupportLevel.Known, internal: true, extensions: null, type: ResourceType.BitstreamFormat, self: 'self-link' }; const bitstreamFormat3: BitstreamFormat = { uuid: 'test-uuid-3', id: 'test-uuid-3', shortDescription: 'CC License', description: 'Item-specific Creative Commons license agreed upon to submission', mimetype: 'text/html; charset=utf-8', supportLevel: BitstreamFormatSupportLevel.Supported, internal: true, extensions: null, type: ResourceType.BitstreamFormat, self: 'self-link' }; const bitstreamFormat4: BitstreamFormat = { uuid: 'test-uuid-4', id: 'test-uuid-4', shortDescription: 'Adobe PDF', description: 'Adobe Portable Document Format', mimetype: 'application/pdf', supportLevel: BitstreamFormatSupportLevel.Unknown, internal: false, extensions: null, type: ResourceType.BitstreamFormat, self: 'self-link' }; const mockFormatsList: BitstreamFormat[] = [ bitstreamFormat1, bitstreamFormat2, bitstreamFormat3, bitstreamFormat4 ]; const mockFormatsRD = new RemoteData(false, false, true, undefined, new PaginatedList(null, mockFormatsList)); const initAsync = () => { notificationsServiceStub = new NotificationsServiceStub(); scheduler = getTestScheduler(); bitstreamFormatService = jasmine.createSpyObj('bitstreamFormatService', { findAll: observableOf(mockFormatsRD), find: observableOf(new RemoteData(false, false, true, undefined, mockFormatsList[0])), getSelectedBitstreamFormats: hot('a', {a: mockFormatsList}), selectBitstreamFormat: {}, deselectBitstreamFormat: {}, deselectAllBitstreamFormats: {}, delete: observableOf(true), clearBitStreamFormatRequests: observableOf('cleared') }); TestBed.configureTestingModule({ imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()], declarations: [BitstreamFormatsComponent, PaginationComponent, EnumKeysPipe], providers: [ {provide: BitstreamFormatDataService, useValue: bitstreamFormatService}, {provide: HostWindowService, useValue: new HostWindowServiceStub(0)}, {provide: NotificationsService, useValue: notificationsServiceStub} ] }).compileComponents(); }; const initBeforeEach = () => { fixture = TestBed.createComponent(BitstreamFormatsComponent); comp = fixture.componentInstance; fixture.detectChanges(); }; describe('Bitstream format page content', () => { beforeEach(async(initAsync)); beforeEach(initBeforeEach); it('should contain four formats', () => { const tbody: HTMLElement = fixture.debugElement.query(By.css('#formats>tbody')).nativeElement; expect(tbody.children.length).toBe(4); }); it('should contain the correct formats', () => { const unknownName: HTMLElement = fixture.debugElement.query(By.css('#formats tr:nth-child(1) td:nth-child(2)')).nativeElement; expect(unknownName.textContent).toBe('Unknown'); const licenseName: HTMLElement = fixture.debugElement.query(By.css('#formats tr:nth-child(2) td:nth-child(2)')).nativeElement; expect(licenseName.textContent).toBe('License'); const ccLicenseName: HTMLElement = fixture.debugElement.query(By.css('#formats tr:nth-child(3) td:nth-child(2)')).nativeElement; expect(ccLicenseName.textContent).toBe('CC License'); const adobeName: HTMLElement = fixture.debugElement.query(By.css('#formats tr:nth-child(4) td:nth-child(2)')).nativeElement; expect(adobeName.textContent).toBe('Adobe PDF'); }); }); describe('selectBitStreamFormat', () => { beforeEach(async(initAsync)); beforeEach(initBeforeEach); it('should select a bitstreamFormat if it was selected in the event', () => { const event = {target: {checked: true}}; comp.selectBitStreamFormat(bitstreamFormat1, event); expect(bitstreamFormatService.selectBitstreamFormat).toHaveBeenCalledWith(bitstreamFormat1); }); it('should deselect a bitstreamFormat if it is deselected in the event', () => { const event = {target: {checked: false}}; comp.selectBitStreamFormat(bitstreamFormat1, event); expect(bitstreamFormatService.deselectBitstreamFormat).toHaveBeenCalledWith(bitstreamFormat1); }); it('should be called when a user clicks a checkbox', () => { spyOn(comp, 'selectBitStreamFormat'); const unknownFormat = fixture.debugElement.query(By.css('#formats tr:nth-child(1) input')); const event = {target: {checked: true}}; unknownFormat.triggerEventHandler('change', event); expect(comp.selectBitStreamFormat).toHaveBeenCalledWith(bitstreamFormat1, event); }); }); describe('isSelected', () => { beforeEach(async(initAsync)); beforeEach(initBeforeEach); it('should return an observable of true if the provided bistream is in the list returned by the service', () => { const result = comp.isSelected(bitstreamFormat1); expect(result).toBeObservable(cold('b', {b: true})); }); it('should return an observable of false if the provided bistream is not in the list returned by the service', () => { const format = new BitstreamFormat(); format.uuid = 'new'; const result = comp.isSelected(format); expect(result).toBeObservable(cold('b', {b: false})); }); }); describe('deselectAll', () => { beforeEach(async(initAsync)); beforeEach(initBeforeEach); it('should deselect all bitstreamFormats', () => { comp.deselectAll(); expect(bitstreamFormatService.deselectAllBitstreamFormats).toHaveBeenCalled(); }); it('should be called when the deselect all button is clicked', () => { spyOn(comp, 'deselectAll'); const deselectAllButton = fixture.debugElement.query(By.css('button.deselect')); deselectAllButton.triggerEventHandler('click', null); expect(comp.deselectAll).toHaveBeenCalled(); }); }); describe('deleteFormats success', () => { beforeEach(async(() => { notificationsServiceStub = new NotificationsServiceStub(); scheduler = getTestScheduler(); bitstreamFormatService = jasmine.createSpyObj('bitstreamFormatService', { findAll: observableOf(mockFormatsRD), find: observableOf(new RemoteData(false, false, true, undefined, mockFormatsList[0])), getSelectedBitstreamFormats: observableOf(mockFormatsList), selectBitstreamFormat: {}, deselectBitstreamFormat: {}, deselectAllBitstreamFormats: {}, delete: observableOf(true), clearBitStreamFormatRequests: observableOf('cleared') }); TestBed.configureTestingModule({ imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()], declarations: [BitstreamFormatsComponent, PaginationComponent, EnumKeysPipe], providers: [ {provide: BitstreamFormatDataService, useValue: bitstreamFormatService}, {provide: HostWindowService, useValue: new HostWindowServiceStub(0)}, {provide: NotificationsService, useValue: notificationsServiceStub} ] }).compileComponents(); } )); beforeEach(initBeforeEach); it('should clear bitstream formats ', () => { comp.deleteFormats(); expect(bitstreamFormatService.clearBitStreamFormatRequests).toHaveBeenCalled(); expect(bitstreamFormatService.delete).toHaveBeenCalledWith(bitstreamFormat1); expect(bitstreamFormatService.delete).toHaveBeenCalledWith(bitstreamFormat2); expect(bitstreamFormatService.delete).toHaveBeenCalledWith(bitstreamFormat3); expect(bitstreamFormatService.delete).toHaveBeenCalledWith(bitstreamFormat4); expect(notificationsServiceStub.success).toHaveBeenCalledWith('admin.registries.bitstream-formats.delete.success.head', 'admin.registries.bitstream-formats.delete.success.amount'); expect(notificationsServiceStub.error).not.toHaveBeenCalled(); }); }); describe('deleteFormats error', () => { beforeEach(async(() => { notificationsServiceStub = new NotificationsServiceStub(); scheduler = getTestScheduler(); bitstreamFormatService = jasmine.createSpyObj('bitstreamFormatService', { findAll: observableOf(mockFormatsRD), find: observableOf(new RemoteData(false, false, true, undefined, mockFormatsList[0])), getSelectedBitstreamFormats: observableOf(mockFormatsList), selectBitstreamFormat: {}, deselectBitstreamFormat: {}, deselectAllBitstreamFormats: {}, delete: observableOf(false), clearBitStreamFormatRequests: observableOf('cleared') }); TestBed.configureTestingModule({ imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()], declarations: [BitstreamFormatsComponent, PaginationComponent, EnumKeysPipe], providers: [ {provide: BitstreamFormatDataService, useValue: bitstreamFormatService}, {provide: HostWindowService, useValue: new HostWindowServiceStub(0)}, {provide: NotificationsService, useValue: notificationsServiceStub} ] }).compileComponents(); } )); beforeEach(initBeforeEach); it('should clear bitstream formats ', () => { comp.deleteFormats(); expect(bitstreamFormatService.clearBitStreamFormatRequests).toHaveBeenCalled(); expect(bitstreamFormatService.delete).toHaveBeenCalledWith(bitstreamFormat1); expect(bitstreamFormatService.delete).toHaveBeenCalledWith(bitstreamFormat2); expect(bitstreamFormatService.delete).toHaveBeenCalledWith(bitstreamFormat3); expect(bitstreamFormatService.delete).toHaveBeenCalledWith(bitstreamFormat4); expect(notificationsServiceStub.error).toHaveBeenCalledWith('admin.registries.bitstream-formats.delete.failure.head', 'admin.registries.bitstream-formats.delete.failure.amount'); expect(notificationsServiceStub.success).not.toHaveBeenCalled(); }); }); });