From 6d0571a3080b444b6bbf5049b22f75c78af9acf6 Mon Sep 17 00:00:00 2001 From: VitoAlbano Date: Mon, 10 Mar 2025 17:54:41 +0000 Subject: [PATCH] [ci:force][AAE-30877] - fixing core pdf test --- .../common/services/download.service.spec.ts | 10 +- .../notification-history.component.spec.ts | 7 +- .../services/notification.service.spec.ts | 8 +- .../pdf-viewer/pdf-viewer.component.spec.ts | 764 +++++++++--------- .../pdf-viewer/pdf-viewer.component.ts | 2 +- 5 files changed, 375 insertions(+), 416 deletions(-) diff --git a/lib/core/src/lib/common/services/download.service.spec.ts b/lib/core/src/lib/common/services/download.service.spec.ts index 7394301289..ad79900d88 100644 --- a/lib/core/src/lib/common/services/download.service.spec.ts +++ b/lib/core/src/lib/common/services/download.service.spec.ts @@ -26,15 +26,13 @@ describe('DownloadService', () => { describe('Download blob', () => { it('Should use native msSaveOrOpenBlob if the browser is IE', () => { - const navigatorAny: any = window.navigator; - - // eslint-disable-next-line no-underscore-dangle - navigatorAny.__defineGetter__('msSaveOrOpenBlob', (result) => { - expect(result).toBeUndefined(); - }); + const mockNavigator = jasmine.createSpyObj(['msSaveOrOpenBlob']); + spyOnProperty(window, 'navigator', 'get').and.returnValue(mockNavigator); const blob = new Blob([''], { type: 'text/html' }); service.downloadBlob(blob, 'test_ie'); + + expect(mockNavigator.msSaveOrOpenBlob).toHaveBeenCalledOnceWith(blob, 'test_ie'); }); }); }); diff --git a/lib/core/src/lib/notifications/components/notification-history.component.spec.ts b/lib/core/src/lib/notifications/components/notification-history.component.spec.ts index 5e1524b934..4ae6f1599f 100644 --- a/lib/core/src/lib/notifications/components/notification-history.component.spec.ts +++ b/lib/core/src/lib/notifications/components/notification-history.component.spec.ts @@ -16,13 +16,16 @@ */ import { ComponentFixture, inject, TestBed } from '@angular/core/testing'; -import { CoreTestingModule } from '../../testing/core.testing.module'; import { NotificationHistoryComponent } from './notification-history.component'; import { OverlayContainer } from '@angular/cdk/overlay'; import { NotificationService } from '../services/notification.service'; import { StorageService } from '../../common/services/storage.service'; import { NOTIFICATION_TYPE, NotificationModel } from '../models/notification.model'; import { UnitTestingUtils } from '../../testing/unit-testing-utils'; +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; +import { NoopTranslateModule } from '../../testing/noop-translate.module'; +import { NoopAuthModule } from '../../testing/noop-auth.module'; +import { MatIconTestingModule } from '@angular/material/icon/testing'; describe('Notification History Component', () => { let fixture: ComponentFixture; @@ -40,7 +43,7 @@ describe('Notification History Component', () => { beforeEach(() => { TestBed.configureTestingModule({ - imports: [CoreTestingModule, NotificationHistoryComponent] + imports: [NoopAnimationsModule, NoopTranslateModule, NoopAuthModule, NotificationHistoryComponent, MatIconTestingModule] }); fixture = TestBed.createComponent(NotificationHistoryComponent); component = fixture.componentInstance; diff --git a/lib/core/src/lib/notifications/services/notification.service.spec.ts b/lib/core/src/lib/notifications/services/notification.service.spec.ts index 694525c501..c4eabd1f24 100644 --- a/lib/core/src/lib/notifications/services/notification.service.spec.ts +++ b/lib/core/src/lib/notifications/services/notification.service.spec.ts @@ -131,15 +131,15 @@ describe('NotificationService', () => { it('should open a message notification bar', async () => { fixture.componentInstance.sendMessage(); fixture.detectChanges(); - - expect(await testingUtils.checkIfMatSnackbarExists()).toBe(true); + const isLoaded = await testingUtils.checkIfMatSnackbarExists(); + expect(isLoaded).toBe(true); }); it('should open a message notification bar without custom configuration', async () => { fixture.componentInstance.sendMessageWithoutConfig(); fixture.detectChanges(); - - expect(await testingUtils.checkIfMatSnackbarExists()).toBe(true); + const isLoaded = await testingUtils.checkIfMatSnackbarExists(); + expect(isLoaded).toBe(true); }); it('should open a message notification bar with custom configuration', async () => { diff --git a/lib/core/src/lib/viewer/components/pdf-viewer/pdf-viewer.component.spec.ts b/lib/core/src/lib/viewer/components/pdf-viewer/pdf-viewer.component.spec.ts index 836afdbe4c..9251e19c32 100644 --- a/lib/core/src/lib/viewer/components/pdf-viewer/pdf-viewer.component.spec.ts +++ b/lib/core/src/lib/viewer/components/pdf-viewer/pdf-viewer.component.spec.ts @@ -21,7 +21,6 @@ import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testin import { MatDialog } from '@angular/material/dialog'; import { By } from '@angular/platform-browser'; import { firstValueFrom, of } from 'rxjs'; -import { take } from 'rxjs/operators'; import { AppConfigService } from '../../../app-config'; import { EventMock } from '../../../mock'; import { NoopAuthModule, NoopTranslateModule, UnitTestingUtils } from '../../../testing'; @@ -32,12 +31,12 @@ import { AnnotationMode } from 'pdfjs-dist'; declare const pdfjsLib: any; -@Component({ - selector: 'adf-test-dialog-component', - standalone: true, - template: '' -}) -class TestDialogComponent {} +// @Component({ +// selector: 'adf-test-dialog-component', +// standalone: true, +// template: '' +// }) +// class TestDialogComponent {} @Component({ selector: 'adf-url-test-component', @@ -117,7 +116,7 @@ describe('Test PdfViewer component', () => { beforeEach(async () => { TestBed.configureTestingModule({ - imports: [NoopAuthModule, NoopTranslateModule, UrlTestComponent, TestDialogComponent, UrlTestPasswordComponent, BlobTestComponent], + imports: [NoopAuthModule, NoopTranslateModule, PdfViewerComponent], providers: [ { provide: MatDialog, @@ -144,350 +143,10 @@ describe('Test PdfViewer component', () => { fixture.destroy(); }); - describe('User interaction', () => { - let fixtureUrlTestComponent: ComponentFixture; - let componentUrlTestComponent: UrlTestComponent; - let elementUrlTestComponent: HTMLElement; - - beforeEach((done) => { - fixtureUrlTestComponent = TestBed.createComponent(UrlTestComponent); - componentUrlTestComponent = fixtureUrlTestComponent.componentInstance; - elementUrlTestComponent = fixtureUrlTestComponent.nativeElement; - testingUtils.setDebugElement(fixtureUrlTestComponent.debugElement); - const appConfig: AppConfigService = TestBed.inject(AppConfigService); - appConfig.config['adf-viewer.pdf-viewer-scaling'] = 10; - - fixtureUrlTestComponent.detectChanges(); - - componentUrlTestComponent.pdfViewerComponent.rendered.pipe(take(1)).subscribe(() => { - done(); - }); - }); - - afterEach(() => { - document.body.removeChild(elementUrlTestComponent); - }); - - it('should init the viewer with annotation mode disabled', fakeAsync(() => { - fixtureUrlTestComponent.detectChanges(); - tick(400); - expect(componentUrlTestComponent.pdfViewerComponent.pdfViewer.annotationEditorMode).toBe(AnnotationMode.DISABLE); - })); - - it('should Total number of pages be loaded', fakeAsync(() => { - fixtureUrlTestComponent.detectChanges(); - tick(400); - expect(componentUrlTestComponent.pdfViewerComponent.totalPages).toBe(6); - })); - - it('should nextPage move to the next page', fakeAsync(() => { - testingUtils.clickByCSS('#viewer-next-page-button'); - - fixtureUrlTestComponent.detectChanges(); - tick(400); - - expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(2); - })); - - it('should event RIGHT_ARROW keyboard change pages', fakeAsync(() => { - fixtureUrlTestComponent.whenStable(); - fixtureUrlTestComponent.detectChanges(); - EventMock.keyDown(RIGHT_ARROW); - - tick(250); - - expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(2); - })); - - it('should event LEFT_ARROW keyboard change pages', (done) => { - component.inputPage('2'); - - fixtureUrlTestComponent.detectChanges(); - - fixtureUrlTestComponent.whenStable().then(() => { - EventMock.keyDown(LEFT_ARROW); - - fixtureUrlTestComponent.detectChanges(); - - fixtureUrlTestComponent.whenStable().then(() => { - expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(1); - done(); - }); - }); - }, 55000); - - it('should previous page move to the previous page', (done) => { - testingUtils.clickByCSS('#viewer-next-page-button'); - testingUtils.clickByCSS('#viewer-next-page-button'); - testingUtils.clickByCSS('#viewer-previous-page-button'); - fixtureUrlTestComponent.detectChanges(); - - fixtureUrlTestComponent.whenStable().then(() => { - expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(2); - done(); - }); - }, 55000); - - it('should previous page not move to the previous page if is page 1', (done) => { - component.previousPage(); - fixtureUrlTestComponent.detectChanges(); - - fixtureUrlTestComponent.whenStable().then(() => { - expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(1); - done(); - }); - }, 55000); - - it('should Input page move to the inserted page', (done) => { - componentUrlTestComponent.pdfViewerComponent.inputPage('2'); - fixtureUrlTestComponent.detectChanges(); - - fixtureUrlTestComponent.whenStable().then(() => { - expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(2); - done(); - }); - }, 55000); - - describe('Zoom', () => { - it('should zoom in increment the scale value', async () => { - spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); - - const zoomBefore = componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScaleValue; - testingUtils.clickByCSS('#viewer-zoom-in-button'); - fixtureUrlTestComponent.detectChanges(); - await fixtureUrlTestComponent.whenRenderingDone(); - - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); - const currentZoom = componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScaleValue; - expect(zoomBefore < currentZoom).toBe(true); - }); - - it('should zoom out decrement the scale value', async () => { - spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); - testingUtils.clickByCSS('#viewer-zoom-in-button'); - fixtureUrlTestComponent.detectChanges(); - const zoomBefore = componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScaleValue; - - testingUtils.clickByCSS('#viewer-zoom-out-button'); - fixtureUrlTestComponent.detectChanges(); - await fixtureUrlTestComponent.whenRenderingDone(); - - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); - const currentZoom = componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScaleValue; - expect(zoomBefore > currentZoom).toBe(true); - }); - - it('should it-in button toggle page-fit and auto scale mode', fakeAsync(() => { - spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); - tick(250); - - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('init'); - testingUtils.clickByCSS('#viewer-scale-page-button'); - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('page-fit'); - testingUtils.clickByCSS('#viewer-scale-page-button'); - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); - testingUtils.clickByCSS('#viewer-scale-page-button'); - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('page-fit'); - }), 300); - }); - - describe('Resize interaction', () => { - it('should resize event trigger setScaleUpdatePages', (done) => { - spyOn(componentUrlTestComponent.pdfViewerComponent, 'onResize'); - EventMock.resizeMobileView(); - - fixtureUrlTestComponent.whenStable().then(() => { - expect(componentUrlTestComponent.pdfViewerComponent.onResize).toHaveBeenCalled(); - done(); - }); - }, 55000); - }); - - describe('Thumbnails', () => { - it('should have own context', (done) => { - fixtureUrlTestComponent.detectChanges(); - - fixtureUrlTestComponent.whenStable().then(() => { - expect(componentUrlTestComponent.pdfViewerComponent.pdfThumbnailsContext.viewer).not.toBeNull(); - done(); - }); - }, 55000); - - it('should open thumbnails panel', (done) => { - expect(testingUtils.getByCSS('.adf-pdf-viewer__thumbnails')).toBeNull(); - - componentUrlTestComponent.pdfViewerComponent.toggleThumbnails(); - - fixtureUrlTestComponent.detectChanges(); - - fixtureUrlTestComponent.whenStable().then(() => { - expect(testingUtils.getByCSS('.adf-pdf-viewer__thumbnails')).not.toBeNull(); - done(); - }); - }, 55000); - - it('should not render PdfThumbListComponent during initialization of new pdfViewer', () => { - componentUrlTestComponent.pdfViewerComponent.toggleThumbnails(); - componentUrlTestComponent.urlFile = 'file.pdf'; - fixtureUrlTestComponent.detectChanges(); - expect(fixtureUrlTestComponent.debugElement.query(By.directive(PdfThumbListComponent))).toBeNull(); - }); - }); - - describe('Viewer events', () => { - it('should react on the emit of pageChange event', (done) => { - fixtureUrlTestComponent.detectChanges(); - fixtureUrlTestComponent.whenStable().then(() => { - const args = { - pageNumber: 6, - source: { - container: document.getElementById(`${componentUrlTestComponent.pdfViewerComponent.randomPdfId}-viewer-pdf-viewer`) - } - }; - - /* cspell:disable-next-line */ - componentUrlTestComponent.pdfViewerComponent.pdfViewer.eventBus.dispatch('pagechanging', args); - fixtureUrlTestComponent.detectChanges(); - - fixtureUrlTestComponent.whenStable().then(() => { - expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(6); - expect(componentUrlTestComponent.pdfViewerComponent.page).toBe(6); - done(); - }); - }); - }, 55000); - - it('should react on the emit of pagesLoaded event', (done) => { - fixtureUrlTestComponent.detectChanges(); - fixtureUrlTestComponent.whenStable().then(() => { - expect(componentUrlTestComponent.pdfViewerComponent.isPanelDisabled).toBeFalsy(); - - const args = { - pagesCount: 10, - source: { - container: document.getElementById(`${componentUrlTestComponent.pdfViewerComponent.randomPdfId}-viewer-pdf-viewer`) - } - }; - - /* cspell:disable-next-line */ - componentUrlTestComponent.pdfViewerComponent.pdfViewer.eventBus.dispatch('pagesloaded', args); - fixtureUrlTestComponent.detectChanges(); - - fixtureUrlTestComponent.whenStable().then(() => { - expect(componentUrlTestComponent.pdfViewerComponent.isPanelDisabled).toBe(false); - done(); - }); - }); - }, 55000); - }); - }); - it('should Loader be present', () => { expect(testingUtils.getByCSS('.adf-loader-container')).not.toBeNull(); }); - describe('Zoom customization', () => { - describe('custom value', () => { - let fixtureUrlTestComponent: ComponentFixture; - let componentUrlTestComponent: UrlTestComponent; - let elementUrlTestComponent: HTMLElement; - - beforeEach((done) => { - const appConfig: AppConfigService = TestBed.inject(AppConfigService); - appConfig.config['adf-viewer.pdf-viewer-scaling'] = 80; - - fixtureUrlTestComponent = TestBed.createComponent(UrlTestComponent); - componentUrlTestComponent = fixtureUrlTestComponent.componentInstance; - elementUrlTestComponent = fixtureUrlTestComponent.nativeElement; - - fixtureUrlTestComponent.detectChanges(); - - componentUrlTestComponent.pdfViewerComponent.rendered.pipe(take(1)).subscribe(() => { - done(); - }); - }); - - afterEach(() => { - document.body.removeChild(elementUrlTestComponent); - }); - - it('should use the custom zoom if it is present in the app.config', async () => { - spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); - - fixtureUrlTestComponent.detectChanges(); - await fixtureUrlTestComponent.whenStable(); - - expect(componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScale).toBe(0.8); - }); - }); - - describe('less than the minimum allowed value', () => { - let fixtureUrlTestComponent: ComponentFixture; - let componentUrlTestComponent: UrlTestComponent; - let elementUrlTestComponent: HTMLElement; - - beforeEach((done) => { - const appConfig: AppConfigService = TestBed.inject(AppConfigService); - appConfig.config['adf-viewer.pdf-viewer-scaling'] = 10; - - fixtureUrlTestComponent = TestBed.createComponent(UrlTestComponent); - componentUrlTestComponent = fixtureUrlTestComponent.componentInstance; - elementUrlTestComponent = fixtureUrlTestComponent.nativeElement; - - fixtureUrlTestComponent.detectChanges(); - - componentUrlTestComponent.pdfViewerComponent.rendered.pipe(take(1)).subscribe(() => { - done(); - }); - }); - - afterEach(() => { - document.body.removeChild(elementUrlTestComponent); - }); - - it('should use the minimum scale zoom if the value given in app.config is less than the minimum allowed scale', (done) => { - spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); - - fixtureUrlTestComponent.detectChanges(); - - fixtureUrlTestComponent.whenStable().then(() => { - expect(componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScaleValue).toBe('0.25'); - done(); - }); - }); - }); - - describe('greater than the maximum allowed value', () => { - let fixtureUrlTestComponent: ComponentFixture; - let componentUrlTestComponent: UrlTestComponent; - let elementUrlTestComponent: HTMLElement; - - beforeEach(() => { - const appConfig: AppConfigService = TestBed.inject(AppConfigService); - appConfig.config['adf-viewer.pdf-viewer-scaling'] = 55555; - - fixtureUrlTestComponent = TestBed.createComponent(UrlTestComponent); - componentUrlTestComponent = fixtureUrlTestComponent.componentInstance; - elementUrlTestComponent = fixtureUrlTestComponent.nativeElement; - - fixtureUrlTestComponent.detectChanges(); - }); - - afterEach(() => { - document.body.removeChild(elementUrlTestComponent); - }); - - it('should use the maximum scale zoom if the value given in app.config is greater than the maximum allowed scale', async () => { - await firstValueFrom(componentUrlTestComponent.pdfViewerComponent.rendered) - spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); - - fixtureUrlTestComponent.detectChanges(); - await fixtureUrlTestComponent.whenStable(); - expect(componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScale).toBe(10); - }); - }); - }); - describe('Required values', () => { it('should thrown an error If urlFile is not present', () => { change = new SimpleChange(null, null, true); @@ -524,39 +183,32 @@ describe('Test PdfViewer component', () => { fixture.destroy(); }); - it('should Canvas be present', (done) => { + it('should Canvas be present', async () => { fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); - fixtureUrlTestComponent.whenStable().then(() => { - expect(testingUtils.getByCSS('.adf-pdfViewer')).not.toBeNull(); - expect(testingUtils.getByCSS('.adf-viewer-pdf-viewer')).not.toBeNull(); - done(); - }); - }, 55000); + expect(testingUtils.getByCSS('.adf-pdfViewer')).not.toBeNull(); + expect(testingUtils.getByCSS('.adf-viewer-pdf-viewer')).not.toBeNull(); + }); - it('should Input Page elements be present', (done) => { + it('should Input Page elements be present', async () => { fixtureUrlTestComponent.detectChanges(); - fixtureUrlTestComponent.whenStable().then(() => { - /* cspell:disable-next-line */ - expect(testingUtils.getByCSS('.viewer-pagenumber-input')).toBeDefined(); - expect(testingUtils.getByCSS('.viewer-total-pages')).toBeDefined(); + await fixtureUrlTestComponent.whenStable(); + expect(testingUtils.getByCSS('.viewer-pagenumber-input')).toBeDefined(); + expect(testingUtils.getByCSS('.viewer-total-pages')).toBeDefined(); - expect(testingUtils.getByCSS('#viewer-previous-page-button')).not.toBeNull(); - expect(testingUtils.getByCSS('#viewer-next-page-button')).not.toBeNull(); - done(); - }); - }, 55000); + expect(testingUtils.getByCSS('#viewer-previous-page-button')).not.toBeNull(); + expect(testingUtils.getByCSS('#viewer-next-page-button')).not.toBeNull(); + }); - it('should Toolbar be hide if showToolbar is false', (done) => { + it('should Toolbar be hide if showToolbar is false', async () => { component.showToolbar = false; fixtureUrlTestComponent.detectChanges(); - fixtureUrlTestComponent.whenStable().then(() => { - expect(testingUtils.getByCSS('.viewer-toolbar-command')).toBeNull(); - expect(testingUtils.getByCSS('.viewer-toolbar-pagination')).toBeNull(); - done(); - }); - }, 55000); + await fixtureUrlTestComponent.whenStable(); + expect(testingUtils.getByCSS('.viewer-toolbar-command')).toBeNull(); + expect(testingUtils.getByCSS('.viewer-toolbar-pagination')).toBeNull(); + }); }); describe('View with blob file', () => { @@ -577,50 +229,41 @@ describe('Test PdfViewer component', () => { fixture.destroy(); }); - it('should Canvas be present', () => { + it('should Canvas be present', async () => { fixtureBlobTestComponent.detectChanges(); + await fixtureBlobTestComponent.whenStable(); - fixtureBlobTestComponent.whenStable().then(() => { - expect(testingUtils.getByCSS('.adf-pdfViewer')).not.toBeNull(); - expect(testingUtils.getByCSS('.adf-viewer-pdf-viewer')).not.toBeNull(); - }); + expect(testingUtils.getByCSS('.adf-pdfViewer')).not.toBeNull(); + expect(testingUtils.getByCSS('.adf-viewer-pdf-viewer')).not.toBeNull(); }); - it('should Next an Previous Buttons be present', (done) => { + it('should Next an Previous Buttons be present', async () => { fixtureBlobTestComponent.detectChanges(); + await fixtureBlobTestComponent.whenStable(); - fixtureBlobTestComponent.whenStable().then(() => { - expect(testingUtils.getByCSS('#viewer-previous-page-button')).not.toBeNull(); - expect(testingUtils.getByCSS('#viewer-next-page-button')).not.toBeNull(); - done(); - }); - }, 55000); + expect(testingUtils.getByCSS('#viewer-previous-page-button')).not.toBeNull(); + expect(testingUtils.getByCSS('#viewer-next-page-button')).not.toBeNull(); + }); - it('should Input Page elements be present', (done) => { + it('should Input Page elements be present', async () => { fixtureBlobTestComponent.detectChanges(); + await fixtureBlobTestComponent.whenStable(); + /* cspell:disable-next-line */ + expect(testingUtils.getByCSS('.adf-viewer-pagenumber-input')).toBeDefined(); + expect(testingUtils.getByCSS('.adf-viewer-total-pages')).toBeDefined(); + expect(testingUtils.getByCSS('#viewer-previous-page-button')).not.toBeNull(); + expect(testingUtils.getByCSS('#viewer-next-page-button')).not.toBeNull(); + }); - fixtureBlobTestComponent.whenStable().then(() => { - /* cspell:disable-next-line */ - expect(testingUtils.getByCSS('.adf-viewer-pagenumber-input')).toBeDefined(); - expect(testingUtils.getByCSS('.adf-viewer-total-pages')).toBeDefined(); - - expect(testingUtils.getByCSS('#viewer-previous-page-button')).not.toBeNull(); - expect(testingUtils.getByCSS('#viewer-next-page-button')).not.toBeNull(); - done(); - }); - }, 55000); - - it('should Toolbar be hide if showToolbar is false', (done) => { + it('should Toolbar be hide if showToolbar is false', async () => { fixtureBlobTestComponent.componentInstance.pdfViewerComponent.showToolbar = false; fixtureBlobTestComponent.detectChanges(); + await fixtureBlobTestComponent.whenStable(); - fixtureBlobTestComponent.whenStable().then(() => { - expect(testingUtils.getByCSS('.viewer-toolbar-command')).toBeNull(); - expect(testingUtils.getByCSS('.viewer-toolbar-pagination')).toBeNull(); - done(); - }); - }, 55000); + expect(testingUtils.getByCSS('.viewer-toolbar-command')).toBeNull(); + expect(testingUtils.getByCSS('.viewer-toolbar-pagination')).toBeNull(); + }); }); describe('Password protection dialog', () => { @@ -717,3 +360,318 @@ describe('Test PdfViewer component', () => { }); }); }); + +describe('Test PdfViewer - Zoom customization', () => { + let fixtureUrlTestComponent: ComponentFixture; + let componentUrlTestComponent: UrlTestComponent; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [NoopAuthModule, NoopTranslateModule, UrlTestComponent, PdfViewerComponent], + providers: [ + { + provide: MatDialog, + useValue: { + open: () => {} + } + }, + RenderingQueueServices + ] + }); + + fixtureUrlTestComponent = TestBed.createComponent(UrlTestComponent); + componentUrlTestComponent = fixtureUrlTestComponent.componentInstance; + }); + + afterEach(() => { + fixtureUrlTestComponent.destroy(); + }); + + describe('custom value', () => { + beforeEach(async () => { + const appConfig: AppConfigService = TestBed.inject(AppConfigService); + appConfig.config['adf-viewer.pdf-viewer-scaling'] = 80; + + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + await firstValueFrom(componentUrlTestComponent.pdfViewerComponent.rendered); + }); + + it('should use the custom zoom if it is present in the app.config', async () => { + spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); + + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + + expect(componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScale).toBe(0.8); + }); + }); + + describe('less than the minimum allowed value', () => { + beforeEach(async () => { + const appConfig: AppConfigService = TestBed.inject(AppConfigService); + appConfig.config['adf-viewer.pdf-viewer-scaling'] = 10; + + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + await firstValueFrom(componentUrlTestComponent.pdfViewerComponent.rendered); + }); + + it('should use the minimum scale zoom if the value given in app.config is less than the minimum allowed scale', async () => { + spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); + + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + + expect(componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScaleValue).toBe('0.25'); + }); + }); + + describe('greater than the maximum allowed value', () => { + beforeEach(async () => { + const appConfig: AppConfigService = TestBed.inject(AppConfigService); + appConfig.config['adf-viewer.pdf-viewer-scaling'] = 5555; + + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + await firstValueFrom(componentUrlTestComponent.pdfViewerComponent.rendered); + }); + + it('should use the maximum scale zoom if the value given in app.config is greater than the maximum allowed scale', async () => { + await firstValueFrom(componentUrlTestComponent.pdfViewerComponent.rendered); + spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); + + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + expect(componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScale).toBe(10); + }); + }); +}); + +describe('Test PdfViewer - User interaction', () => { + let fixtureUrlTestComponent: ComponentFixture; + let componentUrlTestComponent: UrlTestComponent; + // let elementUrlTestComponent: HTMLElement; + let testingUtils: UnitTestingUtils; + + beforeEach(async () => { + TestBed.configureTestingModule({ + imports: [NoopAuthModule, NoopTranslateModule, UrlTestComponent, PdfViewerComponent], + providers: [ + { + provide: MatDialog, + useValue: { + open: () => {} + } + }, + RenderingQueueServices + ] + }); + + fixtureUrlTestComponent = TestBed.createComponent(UrlTestComponent); + testingUtils = new UnitTestingUtils(fixtureUrlTestComponent.debugElement); + const appConfig: AppConfigService = TestBed.inject(AppConfigService); + appConfig.config['adf-viewer.pdf-viewer-scaling'] = 10; + + componentUrlTestComponent = fixtureUrlTestComponent.componentInstance; + + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + await firstValueFrom(componentUrlTestComponent.pdfViewerComponent.rendered); + }); + + afterEach(() => { + fixtureUrlTestComponent.destroy(); + }); + + it('should init the viewer with annotation mode disabled', async () => { + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + expect(componentUrlTestComponent.pdfViewerComponent.pdfViewer.annotationEditorMode).toBe(AnnotationMode.DISABLE); + }); + + it('should Total number of pages be loaded', fakeAsync(() => { + fixtureUrlTestComponent.detectChanges(); + tick(400); + expect(componentUrlTestComponent.pdfViewerComponent.totalPages).toBe(6); + })); + + it('should nextPage move to the next page', fakeAsync(() => { + testingUtils.clickByCSS('#viewer-next-page-button'); + + fixtureUrlTestComponent.detectChanges(); + tick(400); + + expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(2); + })); + + it('should event RIGHT_ARROW keyboard change pages', fakeAsync(() => { + fixtureUrlTestComponent.whenStable(); + fixtureUrlTestComponent.detectChanges(); + EventMock.keyDown(RIGHT_ARROW); + + tick(250); + + expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(2); + })); + + it('should event LEFT_ARROW keyboard change pages', async () => { + componentUrlTestComponent.pdfViewerComponent.inputPage('2'); + + fixtureUrlTestComponent.detectChanges(); + + await fixtureUrlTestComponent.whenStable(); + EventMock.keyDown(LEFT_ARROW); + + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + + expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(1); + }); + + it('should previous page move to the previous page', async () => { + testingUtils.clickByCSS('#viewer-next-page-button'); + testingUtils.clickByCSS('#viewer-next-page-button'); + testingUtils.clickByCSS('#viewer-previous-page-button'); + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + + expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(2); + }); + + it('should previous page not move to the previous page if is page 1', async () => { + componentUrlTestComponent.pdfViewerComponent.previousPage(); + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + + expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(1); + }); + + it('should Input page move to the inserted page', async () => { + componentUrlTestComponent.pdfViewerComponent.inputPage('2'); + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + + expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(2); + }); + + describe('Zoom', () => { + it('should zoom in increment the scale value', async () => { + spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); + + const zoomBefore = componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScaleValue; + testingUtils.clickByCSS('#viewer-zoom-in-button'); + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenRenderingDone(); + + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); + const currentZoom = componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScaleValue; + expect(zoomBefore < currentZoom).toBe(true); + }); + + it('should zoom out decrement the scale value', async () => { + spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); + testingUtils.clickByCSS('#viewer-zoom-in-button'); + fixtureUrlTestComponent.detectChanges(); + const zoomBefore = componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScaleValue; + + testingUtils.clickByCSS('#viewer-zoom-out-button'); + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenRenderingDone(); + + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); + const currentZoom = componentUrlTestComponent.pdfViewerComponent.pdfViewer.currentScaleValue; + expect(zoomBefore > currentZoom).toBe(true); + }); + + it('should it-in button toggle page-fit and auto scale mode', fakeAsync(() => { + spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => {}); + tick(250); + + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('init'); + testingUtils.clickByCSS('#viewer-scale-page-button'); + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('page-fit'); + testingUtils.clickByCSS('#viewer-scale-page-button'); + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); + testingUtils.clickByCSS('#viewer-scale-page-button'); + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('page-fit'); + }), 300); + }); + + describe('Resize interaction', () => { + it('should resize event trigger setScaleUpdatePages', async () => { + spyOn(componentUrlTestComponent.pdfViewerComponent, 'onResize'); + EventMock.resizeMobileView(); + await fixtureUrlTestComponent.whenStable(); + + expect(componentUrlTestComponent.pdfViewerComponent.onResize).toHaveBeenCalled(); + }); + }); + + describe('Thumbnails', () => { + it('should have own context', async () => { + fixtureUrlTestComponent.detectChanges(); + + await fixtureUrlTestComponent.whenStable(); + expect(componentUrlTestComponent.pdfViewerComponent.pdfThumbnailsContext.viewer).not.toBeNull(); + }); + + it('should open thumbnails panel', async () => { + expect(testingUtils.getByCSS('.adf-pdf-viewer__thumbnails')).toBeNull(); + + componentUrlTestComponent.pdfViewerComponent.toggleThumbnails(); + + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + expect(testingUtils.getByCSS('.adf-pdf-viewer__thumbnails')).not.toBeNull(); + }); + + it('should not render PdfThumbListComponent during initialization of new pdfViewer', () => { + componentUrlTestComponent.pdfViewerComponent.toggleThumbnails(); + componentUrlTestComponent.urlFile = 'file.pdf'; + fixtureUrlTestComponent.detectChanges(); + expect(fixtureUrlTestComponent.debugElement.query(By.directive(PdfThumbListComponent))).toBeNull(); + }); + }); + + describe('Viewer events', () => { + it('should react on the emit of pageChange event', async () => { + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + const args = { + pageNumber: 6, + source: { + container: document.getElementById(`${componentUrlTestComponent.pdfViewerComponent.randomPdfId}-viewer-pdf-viewer`) + } + }; + + /* cspell:disable-next-line */ + componentUrlTestComponent.pdfViewerComponent.pdfViewer.eventBus.dispatch('pagechanging', args); + fixtureUrlTestComponent.detectChanges(); + + await fixtureUrlTestComponent.whenStable(); + expect(componentUrlTestComponent.pdfViewerComponent.displayPage).toBe(6); + expect(componentUrlTestComponent.pdfViewerComponent.page).toBe(6); + }); + + it('should react on the emit of pagesLoaded event', async () => { + fixtureUrlTestComponent.detectChanges(); + await fixtureUrlTestComponent.whenStable(); + expect(componentUrlTestComponent.pdfViewerComponent.isPanelDisabled).toBeFalsy(); + + const args = { + pagesCount: 10, + source: { + container: document.getElementById(`${componentUrlTestComponent.pdfViewerComponent.randomPdfId}-viewer-pdf-viewer`) + } + }; + + /* cspell:disable-next-line */ + componentUrlTestComponent.pdfViewerComponent.pdfViewer.eventBus.dispatch('pagesloaded', args); + fixtureUrlTestComponent.detectChanges(); + + await fixtureUrlTestComponent.whenStable(); + expect(componentUrlTestComponent.pdfViewerComponent.isPanelDisabled).toBe(false); + }); + }); +}); diff --git a/lib/core/src/lib/viewer/components/pdf-viewer/pdf-viewer.component.ts b/lib/core/src/lib/viewer/components/pdf-viewer/pdf-viewer.component.ts index c431cafb94..64ea7223d7 100644 --- a/lib/core/src/lib/viewer/components/pdf-viewer/pdf-viewer.component.ts +++ b/lib/core/src/lib/viewer/components/pdf-viewer/pdf-viewer.component.ts @@ -213,7 +213,7 @@ export class PdfViewerComponent implements OnChanges, OnDestroy { } executePdf(pdfOptions: any) { - pdfjsLib.GlobalWorkerOptions.workerSrc = 'pdf.worker.min.js'; + pdfjsLib.GlobalWorkerOptions.workerSrc = 'pdf.worker.min.mjs'; this.loadingTask = pdfjsLib.getDocument(pdfOptions);