Skip to content

Commit 4708a87

Browse files
rafaellygrubersamir-ayoub
authored andcommitted
test(navbar): adiciona testes unitários ao componente
Corrige a logo do navbar, melhora o código e adiciona testes unitários Fixes DTHFUI-1065
1 parent ee62bde commit 4708a87

26 files changed

+1734
-127
lines changed

projects/ui/karma.conf.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ module.exports = function (config) {
2222
thresholds: {
2323
emitWarning: false, // set to 'true' to not fail the test command when thresholds are not met
2424
global: {
25-
statements: 95,
26-
branches: 95,
27-
functions: 95,
28-
lines: 95
25+
statements: 98,
26+
branches: 98,
27+
functions: 98,
28+
lines: 98
2929
}, each: {
3030
statements: 80,
3131
branches: 80,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { RouterModule } from '@angular/router';
3+
4+
import { configureTestSuite } from 'projects/ui/src/lib/util-test/util-expect.spec';
5+
6+
import { PoNavbarActionPopupComponent } from './po-navbar-action-popup.component';
7+
import { PoPopupModule } from '../../../po-popup';
8+
9+
describe('PoNavbarActionPopupComponent:', () => {
10+
let component: PoNavbarActionPopupComponent;
11+
let fixture: ComponentFixture<PoNavbarActionPopupComponent>;
12+
let nativeElement: any;
13+
14+
configureTestSuite(() => {
15+
TestBed.configureTestingModule({
16+
declarations: [ PoNavbarActionPopupComponent ],
17+
imports: [ PoPopupModule, RouterModule.forRoot([]) ]
18+
});
19+
});
20+
21+
beforeEach(() => {
22+
fixture = TestBed.createComponent(PoNavbarActionPopupComponent);
23+
component = fixture.componentInstance;
24+
nativeElement = fixture.debugElement.nativeElement;
25+
26+
fixture.detectChanges();
27+
});
28+
29+
it('should be created', () => {
30+
expect(component instanceof PoNavbarActionPopupComponent).toBeTruthy();
31+
});
32+
33+
describe('Methods:', () => {
34+
35+
it('getLastIconAction: should return the last icon', () => {
36+
component.iconActions = [
37+
{ label: 'eye', icon: 'po-icon-eye'},
38+
{ label: 'gas', icon: 'po-icon-gas'},
39+
{ label: 'mail', icon: 'po-icon-mail'},
40+
{ label: 'menu', icon: 'po-icon-menu'}
41+
];
42+
43+
expect(component.getLastIconAction()).toEqual('po-icon-menu');
44+
});
45+
46+
it('getLastIconAction: should return `undefined` if `iconActions` is undefined', () => {
47+
component.iconActions = undefined;
48+
49+
expect(component.getLastIconAction()).toBeUndefined();
50+
});
51+
52+
it('getLastIconAction: should return `undefined` if `iconActions` is empty array', () => {
53+
component.iconActions = [];
54+
55+
expect(component.getLastIconAction()).toBeUndefined();
56+
});
57+
58+
});
59+
60+
describe('Templates:', () => {
61+
62+
it('should contain the last icon', () => {
63+
component.iconActions = [
64+
{ label: 'eye', icon: 'po-icon-eye'},
65+
{ label: 'gas', icon: 'po-icon-gas'},
66+
{ label: 'mail', icon: 'po-icon-mail'},
67+
{ label: 'menu', icon: 'po-icon-menu'}
68+
];
69+
70+
fixture.detectChanges();
71+
72+
const icon = nativeElement.querySelector('span.po-icon');
73+
expect(icon.classList).toContain('po-icon-menu');
74+
});
75+
76+
});
77+
78+
});

projects/ui/src/lib/components/po-navbar/po-navbar-actions/po-navbar-action-popup/po-navbar-action-popup.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class PoNavbarActionPopupComponent {
1111
@Input('p-icon-actions') iconActions: Array<PoNavbarIconAction>;
1212

1313
getLastIconAction() {
14-
if (this.iconActions && this.iconActions.length > 0) {
14+
if (this.iconActions && this.iconActions.length) {
1515
return this.iconActions[this.iconActions.length - 1].icon;
1616
}
1717
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<div tabindex="0"
1+
<div
2+
tabindex="0"
23
class="po-navbar-action-content po-clickable"
3-
(click)="onActionClick()">
4+
(click)="click()">
45

56
<span
6-
class="po-icon {{ icon }}"
7-
[p-tooltip]="tooltip">
7+
class="po-icon {{ icon }}"
8+
[p-tooltip]="tooltip">
89
</span>
910

1011
</div>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { RouterModule } from '@angular/router';
3+
4+
import { configureTestSuite } from 'projects/ui/src/lib/util-test/util-expect.spec';
5+
import * as utils from '../../../../utils/util';
6+
7+
import { PoNavbarActionComponent } from './po-navbar-action.component';
8+
import { PoTooltipModule } from 'projects/ui/src/lib/directives';
9+
10+
describe('PoNavbarActionComponent:', () => {
11+
let component: PoNavbarActionComponent;
12+
let fixture: ComponentFixture<PoNavbarActionComponent>;
13+
14+
configureTestSuite(() => {
15+
TestBed.configureTestingModule({
16+
declarations: [ PoNavbarActionComponent ],
17+
imports: [ PoTooltipModule, RouterModule.forRoot([]) ]
18+
});
19+
});
20+
21+
beforeEach(() => {
22+
fixture = TestBed.createComponent(PoNavbarActionComponent);
23+
component = fixture.componentInstance;
24+
25+
fixture.detectChanges();
26+
});
27+
28+
it('should be created', () => {
29+
expect(component instanceof PoNavbarActionComponent).toBeTruthy();
30+
});
31+
32+
describe('Methods:', () => {
33+
34+
it('click: should call `callFunction` with `action` and `this` if `action` is defined and `parentRef` is undefined', () => {
35+
component.action = () => 'action';
36+
component['parentRef'] = undefined;
37+
38+
spyOn(utils, 'callFunction');
39+
40+
component.click();
41+
42+
expect(utils.callFunction).toHaveBeenCalledWith(component.action, component);
43+
});
44+
45+
it('click: should call `callFunction` with `action` and `parentRef` if `action` and `parentRef` are defined', () => {
46+
component.action = () => 'action';
47+
component['parentRef'] = '<po-navbar></po-navbar>';
48+
49+
spyOn(utils, 'callFunction');
50+
51+
component.click();
52+
53+
expect(utils.callFunction).toHaveBeenCalledWith(component.action, component['parentRef']);
54+
});
55+
56+
it('click: should call and return `openUrl` with `link` if `action` is undefined and `link` is defined', () => {
57+
component.action = undefined;
58+
component.link = 'http://test.com';
59+
const linkReturn = 'test';
60+
61+
spyOn(utils, 'callFunction');
62+
spyOn(component, <any>'openUrl').and.returnValue(linkReturn);
63+
64+
const result = <any> component.click();
65+
66+
expect(utils.callFunction).not.toHaveBeenCalled();
67+
expect(component['openUrl']).toHaveBeenCalledWith(component.link);
68+
expect(result).toBe(linkReturn);
69+
});
70+
71+
it('click: shouldn`t call `callFunction` and `openUrl` if `action` and `link` is undefined', () => {
72+
component.action = undefined;
73+
component.link = undefined;
74+
75+
spyOn(utils, 'callFunction');
76+
spyOn(component, <any>'openUrl');
77+
78+
component.click();
79+
80+
expect(utils.callFunction).not.toHaveBeenCalled();
81+
expect(component['openUrl']).not.toHaveBeenCalled();
82+
});
83+
84+
it('openUrl: should call `openExternalLink` if url is external link', () => {
85+
const url = 'http://www.google.com';
86+
87+
spyOn(utils, 'openExternalLink');
88+
spyOn(component['router'], 'navigate');
89+
90+
component['openUrl'](url);
91+
92+
expect(utils.openExternalLink).toHaveBeenCalledWith(url);
93+
expect(component['router'].navigate).not.toHaveBeenCalled();
94+
});
95+
96+
it('openUrl: should call `router.navigate` if url is internal link', () => {
97+
const url = '/customers';
98+
99+
spyOn(component['router'], 'navigate');
100+
spyOn(utils, 'openExternalLink');
101+
102+
component['openUrl'](url);
103+
104+
expect(component['router'].navigate).toHaveBeenCalled();
105+
expect(utils.openExternalLink).not.toHaveBeenCalledWith(url);
106+
});
107+
108+
it('openUrl: shouldn`t call `router.navigate` and `openExternalLink` if url is undefined ', () => {
109+
spyOn(component['router'], 'navigate');
110+
spyOn(utils, 'openExternalLink');
111+
112+
component['openUrl'](undefined);
113+
114+
expect(component['router'].navigate).not.toHaveBeenCalled();
115+
expect(utils.openExternalLink).not.toHaveBeenCalled();
116+
});
117+
118+
});
119+
});

projects/ui/src/lib/components/po-navbar/po-navbar-actions/po-navbar-action/po-navbar-action.component.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { callFunction, isExternalLink, openExternalLink } from '../../../../util
99
})
1010
export class PoNavbarActionComponent {
1111

12-
private param;
13-
private parentRef;
12+
private parentRef: any;
1413

1514
@Input('p-action') action?: Function;
1615

@@ -26,9 +25,10 @@ export class PoNavbarActionComponent {
2625
this.parentRef = viewContainerRef['_view']['component'];
2726
}
2827

29-
onActionClick() {
28+
click() {
3029
if (this.action) {
31-
return callFunction(this.action, this.parentRef, this.param || this);
30+
callFunction(this.action, this.parentRef || this);
31+
return;
3232
}
3333

3434
if (this.link) {
@@ -37,6 +37,7 @@ export class PoNavbarActionComponent {
3737
}
3838

3939
private openUrl(url: string) {
40+
4041
if (isExternalLink(url)) {
4142
return openExternalLink(url);
4243
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { RouterModule } from '@angular/router';
3+
4+
import { configureTestSuite, expectPropertiesValues } from '../../../util-test/util-expect.spec';
5+
6+
import { PoNavbarActionsComponent } from './po-navbar-actions.component';
7+
import { PoNavbarActionsModule } from './po-navbar-actions.module';
8+
import { PoNavbarIconAction } from '../interfaces/po-navbar-icon-action.interface';
9+
import { PoTooltipModule } from '../../../directives';
10+
11+
describe('PoNavbarActionsComponent:', () => {
12+
let component: PoNavbarActionsComponent;
13+
let fixture: ComponentFixture<PoNavbarActionsComponent>;
14+
15+
configureTestSuite(() => {
16+
TestBed.configureTestingModule({
17+
imports: [
18+
PoTooltipModule,
19+
PoNavbarActionsModule,
20+
RouterModule.forRoot([])
21+
]
22+
});
23+
});
24+
25+
beforeEach(() => {
26+
fixture = TestBed.createComponent(PoNavbarActionsComponent);
27+
component = fixture.componentInstance;
28+
29+
fixture.detectChanges();
30+
});
31+
32+
it('should be created', () => {
33+
expect(component instanceof PoNavbarActionsComponent).toBeTruthy();
34+
});
35+
36+
describe('Properties:', () => {
37+
38+
it('iconActions: should return valid values', () => {
39+
const icons: Array<PoNavbarIconAction> = [
40+
{ label: 'eye', icon: 'po-icon-eye', link: 'test/'},
41+
{ label: 'gas', icon: 'po-icon-gas', link: 'test/'},
42+
{ label: 'mail', icon: 'po-icon-mail', link: 'test/'},
43+
{ label: 'menu', icon: 'po-icon-menu', link: 'test/'}
44+
];
45+
46+
const expectedIcons = [
47+
{ label: 'eye', icon: 'po-icon-eye', link: 'test/', separator: true, url: 'test/'},
48+
{ label: 'gas', icon: 'po-icon-gas', link: 'test/', separator: true, url: 'test/'},
49+
{ label: 'mail', icon: 'po-icon-mail', link: 'test/', separator: true, url: 'test/'},
50+
{ label: 'menu', icon: 'po-icon-menu', link: 'test/', separator: true, url: 'test/'}
51+
];
52+
53+
expectPropertiesValues(component, 'iconActions', [icons], [expectedIcons]);
54+
});
55+
56+
});
57+
58+
});

projects/ui/src/lib/components/po-navbar/po-navbar-actions/po-navbar-actions.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { PoNavbarIconAction } from '../interfaces/po-navbar-icon-action.interfac
88
})
99
export class PoNavbarActionsComponent {
1010

11-
private _iconActions;
11+
private _iconActions: Array<PoNavbarIconAction>;
1212

1313
@Input('p-icon-actions') set iconActions(actions: Array<PoNavbarIconAction>) {
1414
this._iconActions = actions.map(action => ({ ...action, separator: true, url: action.link }));

0 commit comments

Comments
 (0)