1
- import { Component , DebugElement , Renderer2 , Type , ViewChild } from '@angular/core' ;
1
+ import { Component , DebugElement , ElementRef , Renderer2 , Type , ViewChild } from '@angular/core' ;
2
2
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
3
3
import { By , DomSanitizer } from '@angular/platform-browser' ;
4
4
@@ -7,8 +7,10 @@ import { IconSetService } from '../icon-set';
7
7
import { cilList } from '@coreui/icons' ;
8
8
9
9
@Component ( {
10
- template : `
11
- <svg [cIcon]="this.iconSet.icons.cilList" size="lg" class="test" title="Test"></svg>`
10
+ template : '<svg [cIcon]="this.iconSet.icons.cilList" size="lg" class="test" title="Test"></svg>' ,
11
+ standalone : true ,
12
+ imports : [ IconDirective ] ,
13
+ providers : [ IconSetService ]
12
14
} )
13
15
class TestComponent {
14
16
constructor (
@@ -17,10 +19,13 @@ class TestComponent {
17
19
this . iconSet . icons = { cilList } ;
18
20
}
19
21
20
- @ViewChild ( IconDirective , { read : IconDirective } ) iconRef ! : IconDirective ;
22
+ @ViewChild ( IconDirective , { read : IconDirective } ) public iconRef ! : IconDirective ;
21
23
}
22
24
25
+ class MockElementRef extends ElementRef { }
26
+
23
27
describe ( 'IconDirective' , ( ) => {
28
+ let component : TestComponent ;
24
29
let fixture : ComponentFixture < TestComponent > ;
25
30
let svgEl : DebugElement ;
26
31
let renderer : Renderer2 ;
@@ -29,12 +34,12 @@ describe('IconDirective', () => {
29
34
30
35
beforeEach ( ( ) => {
31
36
TestBed . configureTestingModule ( {
32
- declarations : [ TestComponent ] ,
33
- providers : [ IconSetService ] ,
34
- imports : [ IconDirective ]
37
+ providers : [ IconSetService , { provide : ElementRef , useClass : MockElementRef } ] ,
38
+ imports : [ IconDirective , TestComponent ]
35
39
} ) . compileComponents ( ) ;
36
40
37
41
fixture = TestBed . createComponent ( TestComponent ) ;
42
+ component = fixture . componentInstance ;
38
43
fixture . detectChanges ( ) ;
39
44
svgEl = fixture . debugElement . query ( By . css ( 'svg' ) ) ;
40
45
renderer = fixture . componentRef . injector . get ( Renderer2 as Type < Renderer2 > ) ;
@@ -43,16 +48,27 @@ describe('IconDirective', () => {
43
48
} ) ;
44
49
it ( 'should create an instance' , ( ) => {
45
50
TestBed . runInInjectionContext ( ( ) => {
46
- const directive = new IconDirective ( renderer , svgEl , sanitizer , iconSetService ) ;
51
+ const directive = new IconDirective ( ) ;
47
52
expect ( directive ) . toBeTruthy ( ) ;
48
53
} ) ;
49
54
} ) ;
55
+
56
+ it ( 'service should exist' , ( ) => {
57
+ expect ( component . iconSet ) . toBeTruthy ( ) ;
58
+ } ) ;
59
+
60
+ it ( 'icon component should render' , ( ) => {
61
+ expect ( component . iconRef ) . toBeTruthy ( ) ;
62
+ expect ( component . iconRef . code ( ) ) . toBe ( component . iconSet . icons [ 'cilList' ] ) ;
63
+ } ) ;
64
+
50
65
it ( 'icon classes should be applied' , ( ) => {
51
66
expect ( svgEl . nativeElement ) . toBeTruthy ( ) ;
52
67
expect ( svgEl . nativeElement ) . toHaveClass ( 'icon' ) ;
53
68
expect ( svgEl . nativeElement ) . toHaveClass ( 'icon-lg' ) ;
54
69
expect ( svgEl . nativeElement ) . toHaveClass ( 'test' ) ;
55
70
} ) ;
71
+
56
72
it ( 'icon attributes should be applied' , ( ) => {
57
73
expect ( svgEl . nativeElement . getAttribute ( 'aria-hidden' ) ) . toBe ( 'true' ) ;
58
74
expect ( svgEl . nativeElement . getAttribute ( 'pointer-events' ) ) . toBe ( 'none' ) ;
0 commit comments