Skip to content

Commit b23de18

Browse files
spliffonespike-rabbit
authored andcommitted
test(element): migrate n to vitest
1 parent fd3bead commit b23de18

File tree

7 files changed

+186
-225
lines changed

7 files changed

+186
-225
lines changed

angular.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
"buildTarget": "element-ng:test-target",
171171
"browsers": ["chromium"],
172172
"browserViewport": "1280x720",
173-
"exclude": ["[n-q]*/**", "schematics/**"],
173+
"exclude": ["[o-q]*/**", "schematics/**"],
174174
"runnerConfig": true
175175
}
176176
},
@@ -179,7 +179,7 @@
179179
"options": {
180180
"tsConfig": "projects/element-ng/tsconfig.spec.json",
181181
"karmaConfig": "projects/element-ng/karma.conf.cjs",
182-
"exclude": ["**/[a-mr-z]*/**", "schematics/**", "**/*.vitest.spec.ts"],
182+
"exclude": ["**/[a-nr-z]*/**", "schematics/**", "**/*.vitest.spec.ts"],
183183
"assets": [
184184
{
185185
"glob": "**/*",

projects/element-ng/navbar-vertical/si-navbar-vertical-item.component.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ describe('SiNavbarVerticalItemComponent', () => {
4040
const mockNavbar = {
4141
collapsed: signal(false),
4242
textOnly: signal(false),
43-
itemTriggered: jasmine.createSpy('itemTriggered')
43+
itemTriggered: vi.fn()
4444
};
4545

4646
beforeEach(async () => {
4747
mockNavbar.collapsed.set(false);
4848
mockNavbar.textOnly.set(false);
49-
mockNavbar.itemTriggered.calls.reset();
5049

5150
await TestBed.configureTestingModule({
52-
imports: [TestHostComponent],
5351
providers: [{ provide: SI_NAVBAR_VERTICAL, useValue: mockNavbar }]
5452
}).compileComponents();
5553

@@ -58,6 +56,8 @@ describe('SiNavbarVerticalItemComponent', () => {
5856
fixture.detectChanges();
5957
});
6058

59+
afterEach(() => vi.clearAllMocks());
60+
6161
describe('formattedBadge() behavior through template', () => {
6262
it('should not display badge for undefined badge', () => {
6363
component.item.set({

projects/element-ng/navbar-vertical/si-navbar-vertical.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ describe('SiNavbarVertical', () => {
179179

180180
await harness.toggleCollapse();
181181

182-
const spySearch = spyOn(component, 'searchEvent');
182+
const spySearch = vi.spyOn(component, 'searchEvent');
183183
await harness.search('test');
184-
await fixture.whenStable();
185184
expect(spySearch).toHaveBeenCalledWith('test');
186185
});
187186

projects/element-ng/notification-item/si-notification-item.component.spec.ts

Lines changed: 61 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
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';
66
import { 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

910
import {
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-
5218
describe('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

Comments
 (0)