1- import { Component , DebugElement , Renderer2 , Type , ViewChild } from '@angular/core' ;
1+ import { Component , DebugElement , ElementRef , Renderer2 , Type , ViewChild } from '@angular/core' ;
22import { ComponentFixture , TestBed } from '@angular/core/testing' ;
33import { By , DomSanitizer } from '@angular/platform-browser' ;
44
@@ -7,8 +7,10 @@ import { IconSetService } from '../icon-set';
77import { cilList } from '@coreui/icons' ;
88
99@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 ]
1214} )
1315class TestComponent {
1416constructor (
@@ -17,10 +19,13 @@ class TestComponent {
1719this . iconSet . icons = { cilList} ;
1820}
1921
20- @ViewChild ( IconDirective , { read :IconDirective } ) iconRef ! :IconDirective ;
22+ @ViewChild ( IconDirective , { read :IconDirective } ) public iconRef ! :IconDirective ;
2123}
2224
25+ class MockElementRef extends ElementRef { }
26+
2327describe ( 'IconDirective' , ( ) => {
28+ let component :TestComponent ;
2429let fixture :ComponentFixture < TestComponent > ;
2530let svgEl :DebugElement ;
2631let renderer :Renderer2 ;
@@ -29,12 +34,12 @@ describe('IconDirective', () => {
2934
3035beforeEach ( ( ) => {
3136TestBed . configureTestingModule ( {
32- declarations :[ TestComponent ] ,
33- providers :[ IconSetService ] ,
34- imports :[ IconDirective ]
37+ providers :[ IconSetService , { provide :ElementRef , useClass :MockElementRef } ] ,
38+ imports :[ IconDirective , TestComponent ]
3539} ) . compileComponents ( ) ;
3640
3741fixture = TestBed . createComponent ( TestComponent ) ;
42+ component = fixture . componentInstance ;
3843fixture . detectChanges ( ) ;
3944svgEl = fixture . debugElement . query ( By . css ( 'svg' ) ) ;
4045renderer = fixture . componentRef . injector . get ( Renderer2 as Type < Renderer2 > ) ;
@@ -43,16 +48,27 @@ describe('IconDirective', () => {
4348} ) ;
4449it ( 'should create an instance' , ( ) => {
4550TestBed . runInInjectionContext ( ( ) => {
46- const directive = new IconDirective ( renderer , svgEl , sanitizer , iconSetService ) ;
51+ const directive = new IconDirective ( ) ;
4752expect ( directive ) . toBeTruthy ( ) ;
4853} ) ;
4954} ) ;
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+
5065it ( 'icon classes should be applied' , ( ) => {
5166expect ( svgEl . nativeElement ) . toBeTruthy ( ) ;
5267expect ( svgEl . nativeElement ) . toHaveClass ( 'icon' ) ;
5368expect ( svgEl . nativeElement ) . toHaveClass ( 'icon-lg' ) ;
5469expect ( svgEl . nativeElement ) . toHaveClass ( 'test' ) ;
5570} ) ;
71+
5672it ( 'icon attributes should be applied' , ( ) => {
5773expect ( svgEl . nativeElement . getAttribute ( 'aria-hidden' ) ) . toBe ( 'true' ) ;
5874expect ( svgEl . nativeElement . getAttribute ( 'pointer-events' ) ) . toBe ( 'none' ) ;