|
| 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 { DSpaceObject } from '@dspace/core/shared/dspace-object.model'; |
| 9 | +import { TranslateModule } from '@ngx-translate/core'; |
| 10 | +import { of } from 'rxjs'; |
| 11 | + |
| 12 | +import { PdfViewerService } from '../pdf-viewer-service/pdf-viewer-service'; |
| 13 | +import { PdfViewerEnableComponent } from './pdf-viewer-enable.component'; |
| 14 | + |
| 15 | +describe('PdfViewerEnableComponent', () => { |
| 16 | + let comp: PdfViewerEnableComponent; |
| 17 | + let fixture: ComponentFixture<PdfViewerEnableComponent>; |
| 18 | + |
| 19 | + let pdfViewerService: PdfViewerService; |
| 20 | + |
| 21 | + const dso = new DSpaceObject(); |
| 22 | + |
| 23 | + beforeEach(waitForAsync(() => { |
| 24 | + pdfViewerService = jasmine.createSpyObj('pdfViewerService', { |
| 25 | + isViewerEnabled$: of(false), |
| 26 | + viewerAllowedForBitstreamFormat$: of(true), |
| 27 | + }); |
| 28 | + |
| 29 | + TestBed.configureTestingModule({ |
| 30 | + imports: [TranslateModule.forRoot()], |
| 31 | + providers: [ |
| 32 | + { provide: PdfViewerService, useValue: pdfViewerService }, |
| 33 | + ], |
| 34 | + schemas: [NO_ERRORS_SCHEMA], |
| 35 | + }).compileComponents(); |
| 36 | + })); |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + fixture = TestBed.createComponent(PdfViewerEnableComponent); |
| 40 | + comp = fixture.componentInstance; |
| 41 | + comp.dso = dso; |
| 42 | + fixture.detectChanges(); |
| 43 | + }); |
| 44 | + |
| 45 | + |
| 46 | + describe('onInit', () => { |
| 47 | + it('should init the comp', () => { |
| 48 | + expect(comp).toBeTruthy(); |
| 49 | + }); |
| 50 | + it('should initialise observable that check whether the PDF viewer is enabled', (done) => { |
| 51 | + comp.isEnabled$.subscribe((value) => { |
| 52 | + expect(value).toBeFalse(); |
| 53 | + done(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + it('should initialise observable that check whether PDF viewer configuration is allowed', (done) => { |
| 57 | + comp.isViewerConfigAllowed$.subscribe((value) => { |
| 58 | + expect(value).toBeTrue(); |
| 59 | + done(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + describe('update', () => { |
| 64 | + it('should emit the provided boolean as a string value', () => { |
| 65 | + spyOn(comp.onChange, 'emit'); |
| 66 | + |
| 67 | + comp.update(true); |
| 68 | + expect(comp.onChange.emit).toHaveBeenCalledWith('true'); |
| 69 | + |
| 70 | + comp.update(false); |
| 71 | + expect(comp.onChange.emit).toHaveBeenCalledWith('false'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + describe('form', () => { |
| 75 | + it('should display the ui-switch and text span when configuration is allowed', () => { |
| 76 | + const uiSwitch = fixture.debugElement.queryAll(By.css('ui-switch')); |
| 77 | + const span = fixture.debugElement.queryAll(By.css('span')); |
| 78 | + |
| 79 | + expect(uiSwitch.length).toEqual(1); |
| 80 | + expect(span.length).toEqual(1); |
| 81 | + }); |
| 82 | + it('should not display the ui-switch and text span when configuration is not allowed', () => { |
| 83 | + (pdfViewerService.viewerAllowedForBitstreamFormat$ as jasmine.Spy).and.returnValue(of(false)); |
| 84 | + fixture.detectChanges(); |
| 85 | + |
| 86 | + const uiSwitch = fixture.debugElement.queryAll(By.css('ui-switch')); |
| 87 | + const span = fixture.debugElement.queryAll(By.css('span')); |
| 88 | + |
| 89 | + expect(uiSwitch.length).toEqual(1); |
| 90 | + expect(span.length).toEqual(1); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
0 commit comments