|
| 1 | +/** |
| 2 | + * The contents of this file are subject to the license and copyright |
| 3 | + * detailed in the LICENSE and NOTICE files at the root of the source |
| 4 | + * tree and available online at |
| 5 | + * |
| 6 | + * http://www.dspace.org/license/ |
| 7 | + */ |
| 8 | + |
| 9 | +import { AppConfig } from '@dspace/config/app-config.interface'; |
| 10 | +import { LinkService } from '@dspace/core/cache/builders/link.service'; |
| 11 | +import { BitstreamFormatDataService } from '@dspace/core/data/bitstream-format-data.service'; |
| 12 | +import { Bitstream } from '@dspace/core/shared/bitstream.model'; |
| 13 | +import { BitstreamFormat } from '@dspace/core/shared/bitstream-format.model'; |
| 14 | +import { DSpaceObject } from '@dspace/core/shared/dspace-object.model'; |
| 15 | +import { Item } from '@dspace/core/shared/item.model'; |
| 16 | +import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote-data.utils'; |
| 17 | + |
| 18 | +import { PdfViewerService } from './pdf-viewer-service'; |
| 19 | + |
| 20 | +describe('PdfViewerService', () => { |
| 21 | + let service: PdfViewerService; |
| 22 | + |
| 23 | + let bitstreamFormatService: BitstreamFormatDataService; |
| 24 | + let linkService: LinkService; |
| 25 | + |
| 26 | + let appConfig: AppConfig; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + appConfig = { pdfViewer: { enabled: true } } as AppConfig; |
| 30 | + |
| 31 | + bitstreamFormatService = jasmine.createSpyObj('bitstreamFormatDataService', { |
| 32 | + findByBitstream: createSuccessfulRemoteDataObject$({ mimetype: 'application/pdf' } as BitstreamFormat), |
| 33 | + }); |
| 34 | + linkService = jasmine.createSpyObj('linkService', [ |
| 35 | + 'resolveLinkWithoutAttaching', |
| 36 | + ]); |
| 37 | + |
| 38 | + service = new PdfViewerService( |
| 39 | + bitstreamFormatService, |
| 40 | + linkService, |
| 41 | + appConfig, |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('viewerAllowedForBitstreamFormat$', () => { |
| 46 | + let dso: DSpaceObject; |
| 47 | + |
| 48 | + it('should return true if the dso is not a bitstream', (done) => { |
| 49 | + dso = new Item(); |
| 50 | + |
| 51 | + service.viewerAllowedForBitstreamFormat$(dso).subscribe((result: boolean) => { |
| 52 | + expect(result).toBeTrue(); |
| 53 | + done(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should call BitstreamFormatDataService if the dso is a bitstream', (done) => { |
| 58 | + dso = new Bitstream(); |
| 59 | + |
| 60 | + service.viewerAllowedForBitstreamFormat$(dso).subscribe((result: boolean) => { |
| 61 | + expect(bitstreamFormatService.findByBitstream).toHaveBeenCalled(); |
| 62 | + done(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should call return true if the bitstream format is PDF', (done) => { |
| 67 | + dso = new Bitstream(); |
| 68 | + |
| 69 | + service.viewerAllowedForBitstreamFormat$(dso).subscribe((result: boolean) => { |
| 70 | + expect(result).toBeTrue(); |
| 71 | + done(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should call return false if the bitstream format is not PDF', (done) => { |
| 76 | + dso = new Bitstream(); |
| 77 | + (bitstreamFormatService.findByBitstream as jasmine.Spy).and.returnValue( |
| 78 | + createSuccessfulRemoteDataObject$({ mimetype: 'text/plain' } as BitstreamFormat), |
| 79 | + ); |
| 80 | + |
| 81 | + service.viewerAllowedForBitstreamFormat$(dso).subscribe((result: boolean) => { |
| 82 | + expect(result).toBeFalse(); |
| 83 | + done(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('isViewerEnabled$', () => { |
| 89 | + let dso: DSpaceObject; |
| 90 | + let parentDso: DSpaceObject = new DSpaceObject(); |
| 91 | + |
| 92 | + beforeEach(() => { |
| 93 | + (linkService.resolveLinkWithoutAttaching as jasmine.Spy).and.returnValue( |
| 94 | + createSuccessfulRemoteDataObject$(parentDso), |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should return pdfViewer.enabled value if the dso is empty', (done) => { |
| 99 | + dso = new Bitstream(); |
| 100 | + |
| 101 | + service.isViewerEnabled$(dso).subscribe((result: boolean) => { |
| 102 | + expect(result).toBeTrue(); |
| 103 | + done(); |
| 104 | + }); |
| 105 | + |
| 106 | + appConfig.pdfViewer.enabled = false; |
| 107 | + service.isViewerEnabled$(dso).subscribe((result: boolean) => { |
| 108 | + expect(result).toBeFalse(); |
| 109 | + done(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should return true if parent metadata value and pdfViewer.enabled are both true', (done) => { |
| 114 | + dso = new Bitstream(); |
| 115 | + |
| 116 | + spyOn(parentDso, 'firstMetadataValue').and.returnValue('true'); |
| 117 | + service.isViewerEnabled$(dso).subscribe((result: boolean) => { |
| 118 | + expect(result).toBeTrue(); |
| 119 | + done(); |
| 120 | + }); |
| 121 | + |
| 122 | + (parentDso.firstMetadataValue as jasmine.Spy).and.returnValue('false'); |
| 123 | + service.isViewerEnabled$(dso).subscribe((result: boolean) => { |
| 124 | + expect(result).toBeFalse(); |
| 125 | + done(); |
| 126 | + }); |
| 127 | + |
| 128 | + (parentDso.firstMetadataValue as jasmine.Spy).and.returnValue('true'); |
| 129 | + appConfig.pdfViewer.enabled = false;service.isViewerEnabled$(dso).subscribe((result: boolean) => { |
| 130 | + expect(result).toBeFalse(); |
| 131 | + done(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments