Skip to content

Commit 6597289

Browse files
committed
refactor(sidebar-nav-label): signal inputs, test
1 parent 0d9e566 commit 6597289

File tree

3 files changed

+71
-41
lines changed

3 files changed

+71
-41
lines changed
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
<a [cHtmlAttr]="item.attributes"
2-
[ngClass]="getItemClass()"
3-
href="{{item.url}}">
4-
@if (helper.hasIcon(item)) {
5-
<i [ngClass]="getLabelIconClass()"></i>
1+
@let labelItem = item();
2+
<a [cHtmlAttr]="labelItem.attributes"
3+
[ngClass]="itemClass()"
4+
href="{{labelItem.url}}">
5+
@if (helper.hasIcon(labelItem)) {
6+
<i [ngClass]="labelIconClass()"></i>
67
}
7-
<ng-container>{{ item.name }}</ng-container>
8-
@if (helper.hasBadge(item)) {
9-
<span [ngClass]="item | cSidebarNavBadge">{{ item.badge.text }}</span>
8+
<ng-container>{{ labelItem.name }}</ng-container>
9+
@if (helper.hasBadge(labelItem)) {
10+
<span [ngClass]="labelItem | cSidebarNavBadge">{{ labelItem.badge?.text ?? '' }}</span>
1011
}
1112
</a>
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
2+
import { By } from '@angular/platform-browser';
23

34
import { SidebarNavLabelComponent } from './sidebar-nav-label.component';
45
import { SidebarNavHelper } from './sidebar-nav.service';
5-
// import {LayoutModule} from '../../shared/layout/index.ts_';
66

77
describe('SidebarNavLabelComponent', () => {
88
let component: SidebarNavLabelComponent;
@@ -13,24 +13,57 @@ describe('SidebarNavLabelComponent', () => {
1313
TestBed.configureTestingModule({
1414
providers: [SidebarNavHelper],
1515
imports: [SidebarNavLabelComponent]
16-
})
17-
.compileComponents();
16+
}).compileComponents();
1817
}));
1918

2019
beforeEach(() => {
2120
fixture = TestBed.createComponent(SidebarNavLabelComponent);
2221
component = fixture.componentInstance;
2322

2423
item = {
24+
name: 'Label Item',
2525
class: '',
26-
variant: 'info'
26+
variant: 'info',
27+
icon: 'c-icon',
28+
label: {
29+
variant: 'info',
30+
class: ''
31+
},
32+
badge: {}
2733
};
28-
component.item = item;
34+
fixture.componentRef.setInput('item', item);
2935

3036
fixture.detectChanges();
3137
});
3238

3339
it('should create', () => {
3440
expect(component).toBeTruthy();
3541
});
42+
43+
it('should set correct itemClass', () => {
44+
const link = fixture.debugElement.query(By.css('a')).nativeElement;
45+
46+
expect(link).toHaveClass('c-nav-label');
47+
expect(link).toHaveClass('c-active');
48+
item.class = 'custom-class';
49+
fixture.componentRef.setInput('item', { ...item });
50+
fixture.detectChanges();
51+
expect(link).toHaveClass('custom-class');
52+
});
53+
54+
it('should display label name', () => {
55+
const label = fixture.debugElement.query(By.css('a')).nativeElement;
56+
expect(label.textContent).toBe(item.name);
57+
expect(label.textContent).toBe('Label Item');
58+
});
59+
60+
it('should set correct labelIconClass', () => {
61+
const icon = fixture.debugElement.query(By.css('i')).nativeElement;
62+
expect(icon).toHaveClass('text-info');
63+
item.label = { variant: 'success', class: 'custom-icon-class' };
64+
fixture.componentRef.setInput('item', { ...item });
65+
fixture.detectChanges();
66+
expect(icon).toHaveClass('text-success');
67+
expect(icon).toHaveClass('custom-icon-class');
68+
});
3669
});
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
1-
import { Component, inject, Input, OnInit } from '@angular/core';
1+
import { Component, computed, inject, input } from '@angular/core';
22
import { NgClass } from '@angular/common';
33

44
import { HtmlAttributesDirective } from '../../shared';
55
import { SidebarNavHelper } from './sidebar-nav.service';
66
import { SidebarNavBadgePipe } from './sidebar-nav-badge.pipe';
7+
import { INavData } from './sidebar-nav';
78

89
@Component({
910
selector: 'c-sidebar-nav-label',
1011
templateUrl: './sidebar-nav-label.component.html',
1112
imports: [HtmlAttributesDirective, SidebarNavBadgePipe, NgClass]
1213
})
13-
export class SidebarNavLabelComponent implements OnInit {
14+
export class SidebarNavLabelComponent {
1415
readonly helper = inject(SidebarNavHelper);
1516

16-
@Input() item: any;
17+
readonly item = input<INavData>({});
1718

18-
private classes = {
19-
'c-nav-label': true,
20-
'c-active': true
21-
};
22-
private iconClasses = {};
19+
readonly itemClass = computed(() => {
20+
const classes: Record<string, boolean> = {
21+
'c-nav-label': true,
22+
'c-active': true
23+
};
24+
const itemClass = this.item().class;
25+
if (itemClass) {
26+
classes[itemClass] = !!itemClass;
27+
}
28+
return classes;
29+
});
2330

24-
ngOnInit() {
25-
this.iconClasses = this.helper.getIconClass(this.item);
26-
}
27-
28-
getItemClass() {
29-
const itemClass = this.item.class;
30-
// @ts-ignore
31-
this.classes[itemClass] = !!itemClass;
32-
return this.classes;
33-
}
34-
35-
getLabelIconClass() {
36-
const variant = `text-${this.item.label.variant}`;
37-
// @ts-ignore
38-
this.iconClasses[variant] = !!this.item.label.variant;
39-
const labelClass = this.item.label.class;
40-
// @ts-ignore
41-
this.iconClasses[labelClass] = !!labelClass;
42-
return this.iconClasses;
43-
}
31+
readonly labelIconClass = computed(() => {
32+
const item = this.item();
33+
const iconClasses: Record<string, boolean> = this.helper.getIconClass(item);
34+
const variant = `text-${item.label?.variant}`;
35+
iconClasses[variant] = !!item.label?.variant;
36+
const labelClass = item.label?.class ?? '';
37+
iconClasses[labelClass] = !!labelClass;
38+
return iconClasses;
39+
});
4440
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy