diff --git a/eslint.config.js b/eslint.config.js index 99a007a..94ba737 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -14,6 +14,7 @@ module.exports = tseslint.config( ], processor: angular.processInlineTemplates, rules: { + "@typescript-eslint/no-explicit-any": "off", "@angular-eslint/directive-selector": [ "error", { diff --git a/src/app/components/bread-crump/bread-crump.component.spec.ts b/src/app/components/bread-crump/bread-crump.component.spec.ts index ce78152..13bf452 100644 --- a/src/app/components/bread-crump/bread-crump.component.spec.ts +++ b/src/app/components/bread-crump/bread-crump.component.spec.ts @@ -1,21 +1,41 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; - import { BreadCrumpComponent } from './bread-crump.component'; -import { ActivatedRoute } from '@angular/router'; -import { of } from 'rxjs'; +import { Router, NavigationEnd, ActivatedRoute, UrlTree } from '@angular/router'; +import { Subject } from 'rxjs'; +import { By } from '@angular/platform-browser'; describe('BreadCrumpComponent', () => { let component: BreadCrumpComponent; let fixture: ComponentFixture; - const mockActivatedRoute = { - params: of({ id: 1 }), - snapshot: { params: { id: 1 } }, + let mockRouter: { + events: Subject; + url: string; + createUrlTree: jasmine.Spy; + serializeUrl: jasmine.Spy; }; + let mockActivatedRoute: { snapshot: any }; beforeEach(async () => { + mockRouter = { + events: new Subject(), + url: '/', + createUrlTree: jasmine.createSpy('createUrlTree').and.callFake((commands: any[]) => { + return { + toString: () => commands.join('/'), + } as any as UrlTree; + }), + serializeUrl: jasmine.createSpy('serializeUrl').and.callFake((urlTree: UrlTree) => { + return urlTree.toString(); + }), + }; + mockActivatedRoute = { snapshot: { url: [] } }; + await TestBed.configureTestingModule({ imports: [BreadCrumpComponent], - providers: [{ provide: ActivatedRoute, useValue: mockActivatedRoute }], + providers: [ + { provide: Router, useValue: mockRouter }, + { provide: ActivatedRoute, useValue: mockActivatedRoute } + ], }).compileComponents(); fixture = TestBed.createComponent(BreadCrumpComponent); @@ -23,7 +43,53 @@ describe('BreadCrumpComponent', () => { fixture.detectChanges(); }); - it('should create', () => { + it('SHOULD be created WHEN ever', () => { expect(component).toBeTruthy(); }); + + it('SHOULD generate breadcrumbs based on the URL WHEN ever', () => { + // Arrange + mockRouter.url = '/dashboard/user/profile'; + + // Act + mockRouter.events.next(new NavigationEnd(1, mockRouter.url, mockRouter.url)); + fixture.detectChanges(); + + expect(component.breadcrumbs).toEqual([ + { label: 'Dashboard', url: '/dashboard' }, + { label: 'User', url: '/dashboard/user' }, + { label: 'Profile', url: '/dashboard/user/profile' }, + ]); + }); + + it('SHOULD render breadcrumbs correctly in the template WHEN ever', () => { + // Arrange + mockRouter.url = '/user/profile'; + + // Act + mockRouter.events.next(new NavigationEnd(1, mockRouter.url, mockRouter.url)); + fixture.detectChanges(); + + // Assert + const breadcrumbItems = fixture.debugElement.queryAll(By.css('nz-breadcrumb-item')); + + expect(breadcrumbItems.length).toBe(3); + + expect(breadcrumbItems[0].nativeElement.textContent.trim()).toContain(''); + expect(breadcrumbItems[1].nativeElement.textContent.trim()).toContain('User'); + expect(breadcrumbItems[2].nativeElement.textContent.trim()).toContain('Profile'); + }); + + it('SHOULD handle the case with an empty URL correctly WHEN ever', () => { + // Arrange + mockRouter.url = '/'; + + // Act + mockRouter.events.next(new NavigationEnd(1, mockRouter.url, mockRouter.url)); + fixture.detectChanges(); + + // Assert + const breadcrumbItems = fixture.debugElement.queryAll(By.css('nz-breadcrumb-item')); + expect(breadcrumbItems.length).toBe(1); + }); }); diff --git a/src/app/components/bread-crump/bread-crump.component.ts b/src/app/components/bread-crump/bread-crump.component.ts index 6e79c18..af06b4f 100644 --- a/src/app/components/bread-crump/bread-crump.component.ts +++ b/src/app/components/bread-crump/bread-crump.component.ts @@ -1,6 +1,6 @@ import { NgFor, NgIf } from '@angular/common'; import { Component, OnInit } from '@angular/core'; -import {NavigationEnd, Router, RouterLink } from '@angular/router'; +import { NavigationEnd, Router, RouterLink } from '@angular/router'; import { NzBreadCrumbComponent, NzBreadCrumbItemComponent } from 'ng-zorro-antd/breadcrumb'; import { filter } from 'rxjs'; @@ -12,14 +12,12 @@ import { filter } from 'rxjs'; styleUrl: './bread-crump.component.scss', }) export class BreadCrumpComponent implements OnInit { - breadcrumbs: {label: string, url: string}[] = []; + breadcrumbs: { label: string; url: string }[] = []; constructor(private router: Router) {} ngOnInit() { - this.router.events.pipe( - filter(event => event instanceof NavigationEnd) - ).subscribe(() => { + this.router.events.pipe(filter((event) => event instanceof NavigationEnd)).subscribe(() => { this.generateBreadcrumbs(); }); @@ -27,7 +25,7 @@ export class BreadCrumpComponent implements OnInit { } private generateBreadcrumbs() { - const urlParts = this.router.url.split('/').filter(part => part); + const urlParts = this.router.url.split('/').filter((part) => part); this.breadcrumbs = urlParts.map((part, index) => { const url = '/' + urlParts.slice(0, index + 1).join('/'); const label = this.formatLabel(part); diff --git a/src/app/components/dashboard-content/dashboard-content.component.spec.ts b/src/app/components/dashboard-content/dashboard-content.component.spec.ts index 474dd71..7fc82e4 100644 --- a/src/app/components/dashboard-content/dashboard-content.component.spec.ts +++ b/src/app/components/dashboard-content/dashboard-content.component.spec.ts @@ -31,7 +31,7 @@ describe('DashboardContentComponent', () => { fixture.detectChanges(); }); - it('should create', () => { + it('SHOULD be created WHEN ever', () => { expect(component).toBeTruthy(); }); }); diff --git a/src/app/components/dashboard-content/dashboard-content.component.ts b/src/app/components/dashboard-content/dashboard-content.component.ts index cf0b83c..f06c6bc 100644 --- a/src/app/components/dashboard-content/dashboard-content.component.ts +++ b/src/app/components/dashboard-content/dashboard-content.component.ts @@ -18,8 +18,6 @@ export class DashboardContentComponent implements OnInit { protected commentList: commentType[] = []; ngOnInit(): void { - console.log(comments); - this.commentList = comments; } } diff --git a/src/app/components/dashboard-navbar/dashboard-navbar.component.html b/src/app/components/dashboard-navbar/dashboard-navbar.component.html index ffc33fa..171a5de 100644 --- a/src/app/components/dashboard-navbar/dashboard-navbar.component.html +++ b/src/app/components/dashboard-navbar/dashboard-navbar.component.html @@ -9,15 +9,15 @@