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' ;
3
3
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 { }
4
18
5
19
describe ( 'NavbarTogglerDirective' , ( ) => {
20
+ let component : TestComponent ;
21
+ let fixture : ComponentFixture < TestComponent > ;
22
+ let debugElement : DebugElement ;
23
+ let debugElementCollapse : DebugElement ;
24
+
6
25
beforeEach ( ( ) => {
7
26
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 ( ) ;
10
36
} ) ;
11
37
12
38
it ( 'should create an instance' , ( ) => {
@@ -15,4 +41,35 @@ describe('NavbarTogglerDirective', () => {
15
41
expect ( directive ) . toBeTruthy ( ) ;
16
42
} ) ;
17
43
} ) ;
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
+ } ) ;
18
75
} ) ;
0 commit comments