1
- import { TestBed } from '@angular/core/testing' ;
2
- import { ElementRef , Renderer2 } from '@angular/core' ;
1
+ import { ComponentFixture , TestBed } from '@angular/core/testing' ;
2
+ import { Component , DebugElement , ElementRef , Renderer2 } from '@angular/core' ;
3
+ import { By } from '@angular/platform-browser' ;
3
4
4
5
import { HeaderTogglerDirective } from './header-toggler.directive' ;
5
6
7
+ @Component ( {
8
+ imports : [ HeaderTogglerDirective ] ,
9
+ template : '<div cHeaderToggler></div>'
10
+ } )
11
+ export class TestComponent {
12
+ theme ! : 'dark' | 'light' | undefined ;
13
+ }
14
+
6
15
class MockElementRef extends ElementRef { }
7
16
8
17
describe ( 'HeaderTogglerDirective' , ( ) => {
18
+ let fixture : ComponentFixture < TestComponent > ;
19
+ let debugElement : DebugElement ;
20
+
9
21
beforeEach ( ( ) => {
10
22
TestBed . configureTestingModule ( {
23
+ imports : [ TestComponent ] ,
11
24
providers : [ Renderer2 , { provide : ElementRef , useClass : MockElementRef } ]
12
25
} ) ;
26
+ fixture = TestBed . createComponent ( TestComponent ) ;
27
+ debugElement = fixture . debugElement . query ( By . directive ( HeaderTogglerDirective ) ) ;
28
+ fixture . detectChanges ( ) ;
13
29
} ) ;
14
30
15
31
it ( 'should create an instance' , ( ) => {
@@ -18,4 +34,13 @@ describe('HeaderTogglerDirective', () => {
18
34
expect ( directive ) . toBeTruthy ( ) ;
19
35
} ) ;
20
36
} ) ;
37
+
38
+ it ( 'should have css class' , ( ) => {
39
+ expect ( debugElement . nativeElement ) . toHaveClass ( 'header-toggler' ) ;
40
+ } ) ;
41
+
42
+ it ( 'should set attributes' , ( ) => {
43
+ expect ( debugElement . nativeElement . getAttribute ( 'type' ) ) . toBe ( 'button' ) ;
44
+ expect ( debugElement . nativeElement . getAttribute ( 'aria-label' ) ) . toBe ( 'Toggle navigation' ) ;
45
+ } ) ;
21
46
} ) ;
0 commit comments