From ad15cdf028ac55809bf2d5d386ac373e977c5ceb Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Tue, 2 Apr 2019 00:24:31 +0100 Subject: [PATCH] fix unit test --- .../card-view-textitem.component.spec.ts | 23 +- .../auth-guard-sso-role.service.spec.ts | 60 +++-- .../components/pdfViewer.component.spec.ts | 246 +++++++++--------- .../components/task-header.component.spec.ts | 14 - 4 files changed, 164 insertions(+), 179 deletions(-) diff --git a/lib/core/card-view/components/card-view-textitem/card-view-textitem.component.spec.ts b/lib/core/card-view/components/card-view-textitem/card-view-textitem.component.spec.ts index 399856ec56..2c0c2d424c 100644 --- a/lib/core/card-view/components/card-view-textitem/card-view-textitem.component.spec.ts +++ b/lib/core/card-view/components/card-view-textitem/card-view-textitem.component.spec.ts @@ -157,37 +157,18 @@ describe('CardViewTextItemComponent', () => { expect(value.nativeElement.innerText.trim()).toBe('FAKE-DEFAULT-KEY'); }); - it('should render the edit icon in case of clickable true and editable true', () => { + it('should not render the edit icon in case of clickable true but edit false', () => { component.property = new CardViewTextItemModel({ label: 'Text label', value: '', key: 'textkey', default: 'FAKE-DEFAULT-KEY', - clickable: true, - editable: true, - icon: 'FAKE-ICON' - }); - fixture.detectChanges(); - - const value = fixture.debugElement.query(By.css(`[data-automation-id="card-textitem-edit-icon-${component.property.icon}"]`)); - expect(value).not.toBeNull(); - expect(value.nativeElement.innerText.trim()).toBe('FAKE-ICON'); - }); - - it('should not render the edit icon in case of clickable true and icon defined', () => { - component.property = new CardViewTextItemModel({ - label: 'Text label', - value: '', - key: 'textkey', - default: 'FAKE-DEFAULT-KEY', - clickable: true, - icon: 'FAKE-ICON' + clickable: true }); fixture.detectChanges(); const value = fixture.debugElement.query(By.css(`[data-automation-id="card-textitem-edit-icon-${component.property.icon}"]`)); expect(value).toBeNull(); - expect(value.nativeElement.innerText.trim()).toBe('FAKE-ICON'); }); it('should not render the edit icon in case of clickable true and icon undefined', () => { diff --git a/lib/core/services/auth-guard-sso-role.service.spec.ts b/lib/core/services/auth-guard-sso-role.service.spec.ts index a687428898..942afc09bf 100644 --- a/lib/core/services/auth-guard-sso-role.service.spec.ts +++ b/lib/core/services/auth-guard-sso-role.service.spec.ts @@ -68,7 +68,7 @@ describe('Auth Guard SSO role service', () => { spyOn(routerService, 'navigate').and.stub(); const router: ActivatedRouteSnapshot = new ActivatedRouteSnapshot(); - router.data = { 'roles': ['role1', 'role2']}; + router.data = { 'roles': ['role1', 'role2'] }; expect(authGuard.canActivate(router, null)).toBeTruthy(); expect(routerService.navigate).not.toHaveBeenCalled(); @@ -85,7 +85,7 @@ describe('Auth Guard SSO role service', () => { it('Should canActivate return false if the realm_access is not present', async(() => { spyOn(storageService, 'getItem').and.returnValue('my-access_token'); - spyOn(jwtHelperService, 'decodeToken').and.returnValue({ }); + spyOn(jwtHelperService, 'decodeToken').and.returnValue({}); const router: ActivatedRouteSnapshot = new ActivatedRouteSnapshot(); @@ -94,11 +94,11 @@ describe('Auth Guard SSO role service', () => { it('Should redirect to the redirectURL if canActivate is false and redirectUrl is in data', async(() => { spyOn(storageService, 'getItem').and.returnValue('my-access_token'); - spyOn(jwtHelperService, 'decodeToken').and.returnValue({ }); + spyOn(jwtHelperService, 'decodeToken').and.returnValue({}); spyOn(routerService, 'navigate').and.stub(); const router: ActivatedRouteSnapshot = new ActivatedRouteSnapshot(); - router.data = { 'roles': ['role1', 'role2'], 'redirectUrl': 'no-role-url'}; + router.data = { 'roles': ['role1', 'role2'], 'redirectUrl': 'no-role-url' }; expect(authGuard.canActivate(router, null)).toBeFalsy(); expect(routerService.navigate).toHaveBeenCalledWith(['/no-role-url']); @@ -106,32 +106,34 @@ describe('Auth Guard SSO role service', () => { it('Should not redirect if canActivate is false and redirectUrl is not in data', async(() => { spyOn(storageService, 'getItem').and.returnValue('my-access_token'); - spyOn(jwtHelperService, 'decodeToken').and.returnValue({ }); + spyOn(jwtHelperService, 'decodeToken').and.returnValue({}); spyOn(routerService, 'navigate').and.stub(); const router: ActivatedRouteSnapshot = new ActivatedRouteSnapshot(); - router.data = { 'roles': ['role1', 'role2']}; + router.data = { 'roles': ['role1', 'role2'] }; expect(authGuard.canActivate(router, null)).toBeFalsy(); expect(routerService.navigate).not.toHaveBeenCalled(); })); - it('Should canActivate be false hasRealm is true and hasClientRol is false', () => { + it('Should canActivate be false hasRealm is true and hasClientRole is false', () => { const route: ActivatedRouteSnapshot = new ActivatedRouteSnapshot(); - spyOn(this, 'hasRealmRoles').and.returnValue(true); - spyOn(this, 'hasRealmRolesForClientRole').and.returnValue(false); + spyOn(authGuard, 'hasRealmRoles').and.returnValue(true); + spyOn(authGuard, 'hasRealmRolesForClientRole').and.returnValue(false); + route.params = { appName: 'fakeapp' }; route.data = { 'clientRoles': ['appName'], 'roles': ['role1', 'role2'] }; expect(authGuard.canActivate(route, null)).toBeFalsy(); }); - it('Should canActivate be false hasRealm is false and hasClientRol is true', () => { + it('Should canActivate be false if hasRealm is false and hasClientRole is true', () => { const route: ActivatedRouteSnapshot = new ActivatedRouteSnapshot(); - spyOn(this, 'hasRealmRoles').and.returnValue(false); - spyOn(this, 'hasRealmRolesForClientRole').and.returnValue(true); + spyOn(authGuard, 'hasRealmRoles').and.returnValue(false); + spyOn(authGuard, 'hasRealmRolesForClientRole').and.returnValue(true); - route.data = { 'clientRoles': ['appName'], 'roles': ['role1', 'role2'] }; + route.params = { appName: 'fakeapp' }; + route.data = { 'clientRoles': ['fakeapp'], 'roles': ['role1', 'role2'] }; expect(authGuard.canActivate(route, null)).toBeFalsy(); }); @@ -142,10 +144,10 @@ describe('Auth Guard SSO role service', () => { spyOn(jwtHelperService, 'decodeToken').and.returnValue({ 'realm_access': { roles: ['role1'] }, - 'resource_access': { fakeapp: { roles: ['role2'] }} + 'resource_access': { fakeapp: { roles: ['role2'] } } }); - route.params = {appName: 'fakeapp'}; + route.params = { appName: 'fakeapp' }; route.data = { 'clientRoles': ['appName'], 'roles': ['role1', 'role2'] }; expect(authGuard.canActivate(route, null)).toBeTruthy(); @@ -157,10 +159,10 @@ describe('Auth Guard SSO role service', () => { spyOn(jwtHelperService, 'decodeToken').and.returnValue({ 'realm_access': { roles: ['role1'] }, - 'resource_access': { fakeapp: { roles: ['role3'] }} + 'resource_access': { fakeapp: { roles: ['role3'] } } }); - route.params = {appName: 'fakeapp'}; + route.params = { appName: 'fakeapp' }; route.data = { 'clientRoles': ['appName'], 'roles': ['role1', 'role2'] }; expect(authGuard.canActivate(route, null)).toBeFalsy(); @@ -172,10 +174,11 @@ describe('Auth Guard SSO role service', () => { spyOn(storageService, 'getItem').and.returnValue('my-access_token'); spyOn(jwtHelperService, 'decodeToken').and.returnValue( - {'resource_access': { fakeapp: { roles: ['role1'] } } - }); + { + 'resource_access': { fakeapp: { roles: ['role1'] } } + }); - const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1'] ); + const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1']); expect(result).toBeTruthy(); }); @@ -183,18 +186,20 @@ describe('Auth Guard SSO role service', () => { spyOn(storageService, 'getItem').and.returnValue('my-access_token'); spyOn(jwtHelperService, 'decodeToken').and.returnValue( - {'resource_access': { fakeapp: { roles: ['role1'] } } - }); + { + 'resource_access': { fakeapp: { roles: ['role1'] } } + }); - const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2'] ); + const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2']); expect(result).toBeTruthy(); }); it('Should be false if the resource_access does not contain the role', () => { spyOn(storageService, 'getItem').and.returnValue('my-access_token'); spyOn(jwtHelperService, 'decodeToken').and.returnValue( - {'resource_access': { fakeapp: { roles: ['role3'] } } - }); + { + 'resource_access': { fakeapp: { roles: ['role3'] } } + }); const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2']); expect(result).toBeFalsy(); }); @@ -202,8 +207,9 @@ describe('Auth Guard SSO role service', () => { it('Should be false if the resource_access does not contain the client role related to the app', () => { spyOn(storageService, 'getItem').and.returnValue('my-access_token'); spyOn(jwtHelperService, 'decodeToken').and.returnValue( - {'resource_access': { anotherfakeapp: { roles: ['role1'] } } - }); + { + 'resource_access': { anotherfakeapp: { roles: ['role1'] } } + }); const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2']); expect(result).toBeFalsy(); }); diff --git a/lib/core/viewer/components/pdfViewer.component.spec.ts b/lib/core/viewer/components/pdfViewer.component.spec.ts index 47f8681232..ae84a6424c 100644 --- a/lib/core/viewer/components/pdfViewer.component.spec.ts +++ b/lib/core/viewer/components/pdfViewer.component.spec.ts @@ -323,6 +323,73 @@ describe('Test PdfViewer component', () => { }, 5000); }); + describe('Password protection dialog', () => { + + let fixtureUrlTestPasswordComponent: ComponentFixture; + let componentUrlTestPasswordComponent: UrlTestPasswordComponent; + + beforeEach((done) => { + fixtureUrlTestPasswordComponent = TestBed.createComponent(UrlTestPasswordComponent); + componentUrlTestPasswordComponent = fixtureUrlTestPasswordComponent.componentInstance; + + spyOn(dialog, 'open').and.callFake((comp, context) => { + if (context.data.reason === pdfjsLib.PasswordResponses.NEED_PASSWORD) { + return { + afterClosed: () => of('wrong_password') + }; + } + + if (context.data.reason === pdfjsLib.PasswordResponses.INCORRECT_PASSWORD) { + return { + afterClosed: () => of('password') + }; + } + }); + + fixtureUrlTestPasswordComponent.detectChanges(); + + componentUrlTestPasswordComponent.pdfViewerComponent.rendered.subscribe(() => { + done(); + }); + }); + + afterEach(() => { + document.body.removeChild(fixtureUrlTestPasswordComponent.nativeElement); + }); + + it('should try to access protected pdf', (done) => { + fixture.detectChanges(); + fixture.whenStable().then(() => { + fixture.detectChanges(); + + expect(dialog.open).toHaveBeenCalledTimes(2); + done(); + }); + }); + + it('should raise dialog asking for password', (done) => { + fixture.detectChanges(); + fixture.whenStable().then(() => { + fixture.detectChanges(); + expect(dialog.open['calls'].all()[0].args[1].data).toEqual({ + reason: pdfjsLib.PasswordResponses.NEED_PASSWORD + }); + done(); + }); + }); + + it('it should raise dialog with incorrect password', (done) => { + fixture.detectChanges(); + fixture.whenStable().then(() => { + fixture.detectChanges(); + expect(dialog.open['calls'].all()[1].args[1].data).toEqual({ + reason: pdfjsLib.PasswordResponses.INCORRECT_PASSWORD + }); + done(); + }); + }); + }); + describe('User interaction', () => { let fixtureUrlTestComponent: ComponentFixture; @@ -432,72 +499,36 @@ describe('Test PdfViewer component', () => { }); }, 5000); - describe('Zoom', () => { - - it('should zoom in increment the scale value', fakeAsync(() => { - spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => { - }); - - const zoomInButton: any = elementUrlTestComponent.querySelector('#viewer-zoom-in-button'); - - tick(250); - - const zoomBefore = componentUrlTestComponent.pdfViewerComponent.currentScale; - zoomInButton.click(); - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); - const currentZoom = componentUrlTestComponent.pdfViewerComponent.currentScale; - expect(zoomBefore < currentZoom).toBe(true); - })); - - it('should zoom out decrement the scale value', fakeAsync(() => { - spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => { - }); - const zoomOutButton: any = elementUrlTestComponent.querySelector('#viewer-zoom-out-button'); - - tick(250); - - const zoomBefore = componentUrlTestComponent.pdfViewerComponent.currentScale; - zoomOutButton.click(); - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); - const currentZoom = componentUrlTestComponent.pdfViewerComponent.currentScale; - 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(() => { - }); - - const itPage: any = elementUrlTestComponent.querySelector('#viewer-scale-page-button'); - - tick(250); - - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); - itPage.click(); - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('page-fit'); - itPage.click(); - expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); - })); - }); - describe('Resize interaction', () => { - it('should resize event trigger setScaleUpdatePages', () => { + it('should resize event trigger setScaleUpdatePages', (done) => { spyOn(componentUrlTestComponent.pdfViewerComponent, 'onResize'); EventMock.resizeMobileView(); - expect(componentUrlTestComponent.pdfViewerComponent.onResize).toHaveBeenCalled(); + + fixtureUrlTestComponent.whenStable().then(() => { + expect(componentUrlTestComponent.pdfViewerComponent.onResize).toHaveBeenCalled(); + done(); + }); + }, 5000); }); describe('Thumbnails', () => { - it('should have own context', () => { - expect(componentUrlTestComponent.pdfViewerComponent.pdfThumbnailsContext.viewer).not.toBeNull(); + it('should have own context', (done) => { + fixtureUrlTestComponent.detectChanges(); + + fixtureUrlTestComponent.whenStable().then(() => { + expect(componentUrlTestComponent.pdfViewerComponent.pdfThumbnailsContext.viewer).not.toBeNull(); + done(); + }); }, 5000); it('should open thumbnails panel', (done) => { expect(elementUrlTestComponent.querySelector('.adf-pdf-viewer__thumbnails')).toBeNull(); componentUrlTestComponent.pdfViewerComponent.toggleThumbnails(); + fixtureUrlTestComponent.detectChanges(); fixtureUrlTestComponent.whenStable().then(() => { @@ -556,72 +587,53 @@ describe('Test PdfViewer component', () => { }); + describe('Zoom', () => { + + it('should zoom in increment the scale value', fakeAsync(() => { + spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => { + }); + + const zoomInButton: any = elementUrlTestComponent.querySelector('#viewer-zoom-in-button'); + + tick(250); + + const zoomBefore = componentUrlTestComponent.pdfViewerComponent.currentScale; + zoomInButton.click(); + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); + const currentZoom = componentUrlTestComponent.pdfViewerComponent.currentScale; + expect(zoomBefore < currentZoom).toBe(true); + })); + + it('should zoom out decrement the scale value', fakeAsync(() => { + spyOn(componentUrlTestComponent.pdfViewerComponent.pdfViewer, 'forceRendering').and.callFake(() => { + }); + const zoomOutButton: any = elementUrlTestComponent.querySelector('#viewer-zoom-out-button'); + + tick(250); + + const zoomBefore = componentUrlTestComponent.pdfViewerComponent.currentScale; + zoomOutButton.click(); + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); + const currentZoom = componentUrlTestComponent.pdfViewerComponent.currentScale; + 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(() => { + }); + + const itPage: any = elementUrlTestComponent.querySelector('#viewer-scale-page-button'); + + tick(250); + + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); + itPage.click(); + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('page-fit'); + itPage.click(); + expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto'); + })); + }); + }); - describe('Password protection dialog', () => { - - let fixtureUrlTestPasswordComponent: ComponentFixture; - let componentUrlTestPasswordComponent: UrlTestPasswordComponent; - - beforeEach((done) => { - fixtureUrlTestPasswordComponent = TestBed.createComponent(UrlTestPasswordComponent); - componentUrlTestPasswordComponent = fixtureUrlTestPasswordComponent.componentInstance; - - spyOn(dialog, 'open').and.callFake((comp, context) => { - if (context.data.reason === pdfjsLib.PasswordResponses.NEED_PASSWORD) { - return { - afterClosed: () => of('wrong_password') - }; - } - - if (context.data.reason === pdfjsLib.PasswordResponses.INCORRECT_PASSWORD) { - return { - afterClosed: () => of('password') - }; - } - }); - - fixtureUrlTestPasswordComponent.detectChanges(); - - componentUrlTestPasswordComponent.pdfViewerComponent.rendered.subscribe(() => { - done(); - }); - }); - - afterEach(() => { - document.body.removeChild(fixtureUrlTestPasswordComponent.nativeElement); - }); - - it('should try to access protected pdf', (done) => { - fixture.detectChanges(); - fixture.whenStable().then(() => { - fixture.detectChanges(); - - expect(dialog.open).toHaveBeenCalledTimes(2); - done(); - }); - }); - - it('should raise dialog asking for password', (done) => { - fixture.detectChanges(); - fixture.whenStable().then(() => { - fixture.detectChanges(); - expect(dialog.open['calls'].all()[0].args[1].data).toEqual({ - reason: pdfjsLib.PasswordResponses.NEED_PASSWORD - }); - done(); - }); - }); - - it('it should raise dialog with incorrect password', (done) => { - fixture.detectChanges(); - fixture.whenStable().then(() => { - fixture.detectChanges(); - expect(dialog.open['calls'].all()[1].args[1].data).toEqual({ - reason: pdfjsLib.PasswordResponses.INCORRECT_PASSWORD - }); - done(); - }); - }); - }); }); diff --git a/lib/process-services/task-list/components/task-header.component.spec.ts b/lib/process-services/task-list/components/task-header.component.spec.ts index f7c0558932..3b381291a3 100644 --- a/lib/process-services/task-list/components/task-header.component.spec.ts +++ b/lib/process-services/task-list/components/task-header.component.spec.ts @@ -88,20 +88,6 @@ describe('TaskHeaderComponent', () => { }); })); - it('should display clickable edit icon', async(() => { - component.refreshData(); - fixture.detectChanges(); - - fixture.whenStable().then(() => { - const formNameEl = fixture.debugElement.query(By.css('[data-automation-id="header-assignee"] .adf-textitem-clickable-value')); - const iconE = fixture.debugElement.query(By.css(`[data-automation-id="card-textitem-edit-icon-create"]`)); - expect(formNameEl).not.toBeNull(); - expect(iconE).not.toBeNull(); - expect(formNameEl.nativeElement.innerText).toBe('Wilbur Adams'); - expect(iconE.nativeElement.innerText.trim()).toBe('create'); - }); - })); - it('should display placeholder if no assignee', async(() => { component.taskDetails.assignee = null; component.refreshData();