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 {
1416 constructor (
@@ -17,10 +19,13 @@ class TestComponent {
1719 this . 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 ;
2429 let fixture : ComponentFixture < TestComponent > ;
2530 let svgEl : DebugElement ;
2631 let renderer : Renderer2 ;
@@ -29,12 +34,12 @@ describe('IconDirective', () => {
2934
3035 beforeEach ( ( ) => {
3136 TestBed . configureTestingModule ( {
32- declarations : [ TestComponent ] ,
33- providers : [ IconSetService ] ,
34- imports : [ IconDirective ]
37+ providers : [ IconSetService , { provide : ElementRef , useClass : MockElementRef } ] ,
38+ imports : [ IconDirective , TestComponent ]
3539 } ) . compileComponents ( ) ;
3640
3741 fixture = TestBed . createComponent ( TestComponent ) ;
42+ component = fixture . componentInstance ;
3843 fixture . detectChanges ( ) ;
3944 svgEl = fixture . debugElement . query ( By . css ( 'svg' ) ) ;
4045 renderer = fixture . componentRef . injector . get ( Renderer2 as Type < Renderer2 > ) ;
@@ -43,14 +48,33 @@ describe('IconDirective', () => {
4348 } ) ;
4449 it ( 'should create an instance' , ( ) => {
4550 TestBed . runInInjectionContext ( ( ) => {
46- const directive = new IconDirective ( renderer , svgEl , sanitizer , iconSetService ) ;
51+ const directive = new IconDirective ( ) ;
4752 expect ( 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+
5065 it ( 'icon classes should be applied' , ( ) => {
5166 expect ( svgEl . nativeElement ) . toBeTruthy ( ) ;
5267 expect ( svgEl . nativeElement ) . toHaveClass ( 'icon' ) ;
5368 expect ( svgEl . nativeElement ) . toHaveClass ( 'icon-lg' ) ;
5469 expect ( svgEl . nativeElement ) . toHaveClass ( 'test' ) ;
5570 } ) ;
71+
72+ it ( 'icon attributes should be applied' , ( ) => {
73+ expect ( svgEl . nativeElement . getAttribute ( 'aria-hidden' ) ) . toBe ( 'true' ) ;
74+ expect ( svgEl . nativeElement . getAttribute ( 'pointer-events' ) ) . toBe ( 'none' ) ;
75+ expect ( svgEl . nativeElement . getAttribute ( 'role' ) ) . toBe ( 'img' ) ;
76+ expect ( svgEl . nativeElement . getAttribute ( 'title' ) ) . toBe ( 'Test' ) ;
77+ expect ( svgEl . nativeElement . getAttribute ( 'viewBox' ) ) . toBeDefined ( ) ;
78+ expect ( svgEl . nativeElement . getAttribute ( 'xmlns' ) ) . toBe ( 'http://www.w3.org/2000/svg' ) ;
79+ } ) ;
5680} ) ;
0 commit comments