22 * Copyright (c) Siemens 2016 - 2026
33 * SPDX-License-Identifier: MIT
44 */
5- import { Component } from '@angular/core' ;
5+ import { inputBinding , signal , WritableSignal } from '@angular/core' ;
66import { ComponentFixture , TestBed } from '@angular/core/testing' ;
7- import { ActivatedRoute , Router } from '@angular/router' ;
7+ import { provideRouter , Router } from '@angular/router' ;
8+ import { TranslatableString } from '@siemens/element-translate-ng/translate' ;
89
910import {
1011 SiNotificationItemComponent ,
@@ -14,49 +15,43 @@ import {
1415 type NotificationItemRouterLink
1516} from './si-notification-item.component' ;
1617
17- @Component ( {
18- imports : [ SiNotificationItemComponent ] ,
19- template : `
20- <si-notification-item
21- [timeStamp]="timeStamp"
22- [heading]="heading"
23- [description]="description"
24- [unread]="unread"
25- [itemLink]="itemLink"
26- [quickActions]="quickActions"
27- [primaryAction]="primaryAction"
28- />
29- ` ,
30- providers : [
31- {
32- provide : ActivatedRoute ,
33- useValue : {
34- snapshot : {
35- queryParams : { }
36- }
37- }
38- }
39- ]
40- } )
41- class TestHostComponent {
42- timeStamp = 'Today 12:00' ;
43- heading = 'Heading' ;
44-
45- description ?: string ;
46- unread ?: boolean ;
47- itemLink ?: NotificationItemRouterLink | NotificationItemLink ;
48- quickActions ?: NotificationItemQuickAction [ ] ;
49- primaryAction ?: NotificationItemPrimaryAction ;
50- }
51-
5218describe ( 'SiNotificationItemComponent' , ( ) => {
53- let fixture : ComponentFixture < TestHostComponent > ;
54- let component : TestHostComponent ;
19+ let fixture : ComponentFixture < SiNotificationItemComponent > ;
5520 let element : HTMLElement ;
21+ let timeStamp : WritableSignal < TranslatableString > ;
22+ let heading : WritableSignal < TranslatableString > ;
23+ let description : WritableSignal < TranslatableString | undefined > ;
24+ let unread : WritableSignal < boolean > ;
25+ let itemLink : WritableSignal < NotificationItemRouterLink | NotificationItemLink | undefined > ;
26+ let quickActions : WritableSignal < NotificationItemQuickAction [ ] | undefined > ;
27+ let primaryAction : WritableSignal < NotificationItemPrimaryAction | undefined > ;
28+
29+ beforeEach ( ( ) =>
30+ TestBed . configureTestingModule ( {
31+ providers : [ provideRouter ( [ ] ) ]
32+ } )
33+ ) ;
5634
5735 beforeEach ( ( ) => {
58- fixture = TestBed . createComponent ( TestHostComponent ) ;
59- component = fixture . componentInstance ;
36+ timeStamp = signal < TranslatableString > ( 'Today 12:00' ) ;
37+ heading = signal < TranslatableString > ( 'Heading' ) ;
38+ description = signal < TranslatableString | undefined > ( undefined ) ;
39+ unread = signal ( false ) ;
40+ itemLink = signal < NotificationItemRouterLink | NotificationItemLink | undefined > ( undefined ) ;
41+ quickActions = signal < NotificationItemQuickAction [ ] | undefined > ( undefined ) ;
42+ primaryAction = signal < NotificationItemPrimaryAction | undefined > ( undefined ) ;
43+
44+ fixture = TestBed . createComponent ( SiNotificationItemComponent , {
45+ bindings : [
46+ inputBinding ( 'timeStamp' , timeStamp ) ,
47+ inputBinding ( 'heading' , heading ) ,
48+ inputBinding ( 'description' , description ) ,
49+ inputBinding ( 'unread' , unread ) ,
50+ inputBinding ( 'itemLink' , itemLink ) ,
51+ inputBinding ( 'quickActions' , quickActions ) ,
52+ inputBinding ( 'primaryAction' , primaryAction )
53+ ]
54+ } ) ;
6055 element = fixture . nativeElement ;
6156 fixture . detectChanges ( ) ;
6257 } ) ;
@@ -69,42 +64,38 @@ describe('SiNotificationItemComponent', () => {
6964 expect ( element . querySelector ( 'span.si-h5' ) ! . innerHTML ) . toContain ( 'Heading' ) ;
7065 } ) ;
7166
72- it ( 'should display the description' , ( ) => {
73- component . description = 'Description' ;
74- fixture . changeDetectorRef . markForCheck ( ) ;
75- fixture . detectChanges ( ) ;
67+ it ( 'should display the description' , async ( ) => {
68+ description . set ( 'Description' ) ;
69+ await fixture . whenStable ( ) ;
7670 expect ( element . querySelectorAll ( 'span.si-body' ) [ 1 ] . innerHTML ) . toContain ( 'Description' ) ;
7771 } ) ;
7872
79- it ( 'should display the unread state' , ( ) => {
80- component . unread = true ;
81- fixture . changeDetectorRef . markForCheck ( ) ;
82- fixture . detectChanges ( ) ;
73+ it ( 'should display the unread state' , async ( ) => {
74+ unread . set ( true ) ;
75+ await fixture . whenStable ( ) ;
8376 expect ( element . querySelector ( 'span.si-h5' ) ) . not . toBeTruthy ( ) ;
8477 expect ( element . querySelector ( 'span.si-h5-bold' ) ) . toBeTruthy ( ) ;
8578 expect ( element . querySelector ( 'span.dot' ) ) . toBeTruthy ( ) ;
8679 } ) ;
8780
88- it ( 'should link with the item link' , ( ) => {
89- component . itemLink = { type : 'link' , href : '/test' } ;
90- fixture . changeDetectorRef . markForCheck ( ) ;
91- fixture . detectChanges ( ) ;
81+ it ( 'should link with the item link' , async ( ) => {
82+ itemLink . set ( { type : 'link' , href : '/test' } ) ;
83+ await fixture . whenStable ( ) ;
9284 expect ( element . querySelector ( 'a' ) ?. getAttribute ( 'href' ) ) . toBe ( '/test' ) ;
9385 } ) ;
9486
95- it ( 'should link with the router link' , ( ) => {
87+ it ( 'should link with the router link' , async ( ) => {
9688 const router = TestBed . inject ( Router ) ;
97- spyOn ( router , 'navigateByUrl' ) ;
98- component . itemLink = { type : 'router-link' , routerLink : '/test' } ;
99- fixture . changeDetectorRef . markForCheck ( ) ;
100- fixture . detectChanges ( ) ;
89+ vi . spyOn ( router , 'navigateByUrl' ) . mockResolvedValue ( true ) ;
90+ itemLink . set ( { type : 'router-link' , routerLink : '/test' } ) ;
91+ await fixture . whenStable ( ) ;
10192
10293 element . querySelector < HTMLElement > ( 'a' ) ?. click ( ) ;
10394 expect ( router . navigateByUrl ) . toHaveBeenCalled ( ) ;
10495 } ) ;
10596
106- it ( 'should display the quick actions' , ( ) => {
107- component . quickActions = [
97+ it ( 'should display the quick actions' , async ( ) => {
98+ quickActions . set ( [
10899 {
109100 type : 'action-icon-button' ,
110101 action : ( ) => { } ,
@@ -113,34 +104,31 @@ describe('SiNotificationItemComponent', () => {
113104 } ,
114105 { type : 'link' , href : '/test' , ariaLabel : 'Link' , icon : 'element-plant' } ,
115106 { type : 'router-link' , routerLink : '/test' , ariaLabel : 'Router Link' , icon : 'element-plant' }
116- ] ;
117- fixture . changeDetectorRef . markForCheck ( ) ;
118- fixture . detectChanges ( ) ;
107+ ] ) ;
108+ await fixture . whenStable ( ) ;
119109 expect ( element . querySelectorAll ( 'button' ) . length ) . toBe ( 1 ) ;
120110 expect ( element . querySelectorAll ( 'a' ) . length ) . toBe ( 2 ) ;
121111 } ) ;
122112
123- it ( 'should display the primary action menu' , ( ) => {
124- component . primaryAction = {
113+ it ( 'should display the primary action menu' , async ( ) => {
114+ primaryAction . set ( {
125115 type : 'menu' ,
126116 menuItems : [
127117 { type : 'action' , label : 'Action 1' , action : ( ) => { } } ,
128118 { type : 'action' , label : 'Action 2' , action : ( ) => { } }
129119 ]
130- } ;
131- fixture . changeDetectorRef . markForCheck ( ) ;
132- fixture . detectChanges ( ) ;
120+ } ) ;
121+ await fixture . whenStable ( ) ;
133122 expect ( element . querySelector ( 'button si-icon' ) ) . toBeTruthy ( ) ;
134123 } ) ;
135124
136- it ( 'should display the primary action action-button' , ( ) => {
137- component . primaryAction = {
125+ it ( 'should display the primary action action-button' , async ( ) => {
126+ primaryAction . set ( {
138127 type : 'action-button' ,
139128 label : 'Action' ,
140129 action : ( ) => { }
141- } ;
142- fixture . changeDetectorRef . markForCheck ( ) ;
143- fixture . detectChanges ( ) ;
130+ } ) ;
131+ await fixture . whenStable ( ) ;
144132 expect ( element . querySelector ( 'button' ) ?. textContent ) . toContain ( 'Action' ) ;
145133 } ) ;
146134} ) ;
0 commit comments