Skip to content

Commit 8138514

Browse files
committed
refactor(navbar-toggler): host binding, cleanup, tests
1 parent 8cdbb9e commit 8138514

File tree

3 files changed

+74
-10
lines changed

3 files changed

+74
-10
lines changed
Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
1-
import { ElementRef, Renderer2 } from '@angular/core';
2-
import { TestBed } from '@angular/core/testing';
1+
import { Component, DebugElement, ElementRef, Renderer2 } from '@angular/core';
2+
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
33
import { NavbarTogglerDirective } from './navbar-toggler.directive';
4+
import { By } from '@angular/platform-browser';
5+
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
6+
import { CollapseDirective } from '../../collapse';
7+
8+
class MockElementRef extends ElementRef {}
9+
10+
@Component({
11+
imports: [NavbarTogglerDirective, CollapseDirective],
12+
template: `
13+
<button [cNavbarToggler]="collapseRef"></button>
14+
<div #collapseRef="cCollapse" navbar cCollapse></div>
15+
`
16+
})
17+
class TestComponent {}
418

519
describe('NavbarTogglerDirective', () => {
20+
let component: TestComponent;
21+
let fixture: ComponentFixture<TestComponent>;
22+
let debugElement: DebugElement;
23+
let debugElementCollapse: DebugElement;
24+
625
beforeEach(() => {
726
TestBed.configureTestingModule({
8-
providers: [Renderer2, { provide: ElementRef, useValue: { nativeElement: document.createElement('button') } }]
9-
});
27+
imports: [TestComponent],
28+
providers: [Renderer2, { provide: ElementRef, useValue: MockElementRef }, provideAnimationsAsync()]
29+
}).compileComponents();
30+
31+
fixture = TestBed.createComponent(TestComponent);
32+
component = fixture.componentInstance;
33+
debugElement = fixture.debugElement.query(By.directive(NavbarTogglerDirective));
34+
debugElementCollapse = fixture.debugElement.query(By.directive(CollapseDirective));
35+
fixture.detectChanges();
1036
});
1137

1238
it('should create an instance', () => {
@@ -15,4 +41,35 @@ describe('NavbarTogglerDirective', () => {
1541
expect(directive).toBeTruthy();
1642
});
1743
});
44+
45+
it('should have css classes', () => {
46+
expect(debugElement.nativeElement).toHaveClass('navbar-toggler');
47+
});
48+
49+
it('should have default aria-label', () => {
50+
expect(debugElement.nativeElement.getAttribute('aria-label')).toBe('Toggle navigation');
51+
});
52+
53+
it('should have default type', () => {
54+
expect(debugElement.nativeElement.getAttribute('type')).toBe('button');
55+
});
56+
57+
it('should toggle collapse on click', fakeAsync(() => {
58+
const collapseRef = debugElementCollapse.injector.get(CollapseDirective);
59+
expect(collapseRef.visible()).toBeFalse();
60+
fixture.autoDetectChanges();
61+
debugElement.nativeElement.dispatchEvent(new Event('click'));
62+
expect(collapseRef.visible()).toBeTrue();
63+
debugElement.nativeElement.dispatchEvent(new Event('click'));
64+
expect(collapseRef.visible()).toBeFalse();
65+
}));
66+
67+
it('should add default icon', () => {
68+
const directive = debugElement.injector.get(NavbarTogglerDirective);
69+
directive.addDefaultIcon();
70+
const renderer = debugElement.injector.get(Renderer2);
71+
const span = debugElement.nativeElement.querySelector('span');
72+
expect(span).toBeTruthy();
73+
expect(span.classList.contains('navbar-toggler-icon')).toBeTrue();
74+
});
1875
});

projects/coreui-angular/src/lib/navbar/navbar-toggler/navbar-toggler.directive.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { afterNextRender, Directive, ElementRef, HostListener, inject, input, Renderer2 } from '@angular/core';
1+
import { afterNextRender, Directive, ElementRef, inject, input, Renderer2 } from '@angular/core';
22
import { CollapseDirective } from '../../collapse';
33

44
@Directive({
55
selector: '[cNavbarToggler]',
66
host: {
77
'[attr.aria-label]': 'ariaLabel()',
88
'[attr.type]': 'type()',
9-
class: 'navbar-toggler'
9+
class: 'navbar-toggler',
10+
'(click)': 'handleClick($event)'
1011
}
1112
})
1213
export class NavbarTogglerDirective {
@@ -45,8 +46,7 @@ export class NavbarTogglerDirective {
4546
*/
4647
readonly ariaLabel = input('Toggle navigation');
4748

48-
@HostListener('click', ['$event'])
49-
handleClick() {
49+
handleClick($event: MouseEvent): void {
5050
const collapseRef = this.collapseRef();
5151
collapseRef?.toggle(!collapseRef?.visible());
5252
}

projects/coreui-angular/src/lib/navbar/navbar.component.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ describe('NavbarComponent', () => {
99
beforeEach(waitForAsync(() => {
1010
TestBed.configureTestingModule({
1111
imports: [NavbarComponent]
12-
})
13-
.compileComponents();
12+
}).compileComponents();
1413
}));
1514

1615
beforeEach(() => {
@@ -26,4 +25,12 @@ describe('NavbarComponent', () => {
2625
it('should have css classes', () => {
2726
expect(fixture.nativeElement).toHaveClass('navbar');
2827
});
28+
29+
it('should have container class', () => {
30+
fixture.componentRef.setInput('expand', 'xl');
31+
fixture.componentRef.setInput('container', 'sm');
32+
fixture.detectChanges();
33+
expect(fixture.componentInstance.containerClass()).toBe('container-sm');
34+
expect(fixture.componentInstance.breakpoint()).toBe('');
35+
});
2936
});

0 commit comments

Comments
 (0)