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 , TestBed } from '@angular/core/testing' ;
33import { ThemeDirective } from './theme.directive' ;
4+ import { By } from '@angular/platform-browser' ;
5+
6+ @Component ( {
7+ imports : [ ThemeDirective ] ,
8+ template : '<div cTheme [colorScheme]="theme"></div>'
9+ } )
10+ export class TestComponent {
11+ theme ! : 'dark' | 'light' | undefined ;
12+ }
413
514class MockElementRef extends ElementRef { }
615
716describe ( 'ThemeDirective' , ( ) => {
17+ let fixture : ComponentFixture < TestComponent > ;
18+ let debugElement : DebugElement ;
819
920 beforeEach ( ( ) => {
1021 TestBed . configureTestingModule ( {
22+ imports : [ TestComponent ] ,
1123 providers : [ { provide : ElementRef , useClass : MockElementRef } , Renderer2 ]
1224 } ) ;
25+ fixture = TestBed . createComponent ( TestComponent ) ;
26+ debugElement = fixture . debugElement . query ( By . css ( 'div' ) ) ;
1327 } ) ;
1428
1529 it ( 'should create an instance' , ( ) => {
@@ -18,4 +32,18 @@ describe('ThemeDirective', () => {
1832 expect ( directive ) . toBeTruthy ( ) ;
1933 } ) ;
2034 } ) ;
35+
36+ it ( 'should set data-coreui-theme attribute' , ( ) => {
37+ fixture . detectChanges ( ) ;
38+ expect ( debugElement . nativeElement . getAttribute ( 'data-coreui-theme' ) ) . toBeNull ( ) ;
39+ fixture . componentInstance . theme = 'dark' ;
40+ fixture . detectChanges ( ) ;
41+ expect ( debugElement . nativeElement . getAttribute ( 'data-coreui-theme' ) ) . toBe ( 'dark' ) ;
42+ fixture . componentInstance . theme = 'light' ;
43+ fixture . detectChanges ( ) ;
44+ expect ( debugElement . nativeElement . getAttribute ( 'data-coreui-theme' ) ) . toBe ( 'light' ) ;
45+ fixture . componentInstance . theme = undefined ;
46+ fixture . detectChanges ( ) ;
47+ expect ( debugElement . nativeElement . getAttribute ( 'data-coreui-theme' ) ) . toBeNull ( ) ;
48+ } ) ;
2149} ) ;
0 commit comments