fix unit test

This commit is contained in:
Eugenio Romano
2019-04-02 00:24:31 +01:00
parent e6d3869891
commit ad15cdf028
4 changed files with 164 additions and 179 deletions

View File

@@ -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', () => {

View File

@@ -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,17 +186,19 @@ 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,7 +207,8 @@ 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();

View File

@@ -323,6 +323,73 @@ describe('Test PdfViewer component', () => {
}, 5000);
});
describe('Password protection dialog', () => {
let fixtureUrlTestPasswordComponent: ComponentFixture<UrlTestPasswordComponent>;
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<UrlTestComponent>;
@@ -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();
fixtureUrlTestComponent.whenStable().then(() => {
expect(componentUrlTestComponent.pdfViewerComponent.onResize).toHaveBeenCalled();
done();
});
}, 5000);
});
describe('Thumbnails', () => {
it('should have own context', () => {
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(() => {
});
describe('Password protection dialog', () => {
const zoomInButton: any = elementUrlTestComponent.querySelector('#viewer-zoom-in-button');
let fixtureUrlTestPasswordComponent: ComponentFixture<UrlTestPasswordComponent>;
let componentUrlTestPasswordComponent: UrlTestPasswordComponent;
tick(250);
beforeEach((done) => {
fixtureUrlTestPasswordComponent = TestBed.createComponent(UrlTestPasswordComponent);
componentUrlTestPasswordComponent = fixtureUrlTestPasswordComponent.componentInstance;
const zoomBefore = componentUrlTestComponent.pdfViewerComponent.currentScale;
zoomInButton.click();
expect(componentUrlTestComponent.pdfViewerComponent.currentScaleMode).toBe('auto');
const currentZoom = componentUrlTestComponent.pdfViewerComponent.currentScale;
expect(zoomBefore < currentZoom).toBe(true);
}));
spyOn(dialog, 'open').and.callFake((comp, context) => {
if (context.data.reason === pdfjsLib.PasswordResponses.NEED_PASSWORD) {
return {
afterClosed: () => of('wrong_password')
};
}
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');
if (context.data.reason === pdfjsLib.PasswordResponses.INCORRECT_PASSWORD) {
return {
afterClosed: () => of('password')
};
}
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(() => {
});
fixtureUrlTestPasswordComponent.detectChanges();
const itPage: any = elementUrlTestComponent.querySelector('#viewer-scale-page-button');
componentUrlTestPasswordComponent.pdfViewerComponent.rendered.subscribe(() => {
done();
});
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');
}));
});
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();
});
});
});
});

View File

@@ -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();