-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.component.spec.ts
More file actions
174 lines (149 loc) · 6.61 KB
/
app.component.spec.ts
File metadata and controls
174 lines (149 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ErrorHandler, provideZonelessChangeDetection } from '@angular/core';
import { provideRouter, Router } from '@angular/router';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting, HttpTestingController } from '@angular/common/http/testing';
import { BreakpointObserver, BreakpointState } from '@angular/cdk/layout';
import { of } from 'rxjs';
import { AppComponent } from './app.component';
import { GlobalErrorHandler } from './error/global-error-handler';
import { API_BASE_URL } from './tokens';
describe('AppComponent', () => {
let fixture: ComponentFixture<AppComponent>;
let component: AppComponent;
let httpTesting: HttpTestingController;
beforeEach(async () => {
localStorage.clear();
await TestBed.configureTestingModule({
imports: [AppComponent, NoopAnimationsModule],
providers: [
provideZonelessChangeDetection(),
provideRouter([]),
provideHttpClient(),
provideHttpClientTesting(),
{ provide: ErrorHandler, useClass: GlobalErrorHandler },
{ provide: API_BASE_URL, useValue: '/api' },
],
}).compileComponents();
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
httpTesting = TestBed.inject(HttpTestingController);
// Flush initial MetricsStore requests
httpTesting.match('/api/metrics').forEach(req => req.flush({
totalRequests: 0, averageDuration: 0, cacheHitRate: 0, successRate: 0,
}));
httpTesting.match('/api/health').forEach(req => req.flush({
status: 'healthy', version: '0.0.0',
}));
await fixture.whenStable();
});
afterEach(() => {
httpTesting.match(() => true).forEach(req => req.flush({}));
httpTesting.verify();
localStorage.clear();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should have 11 navigation items', () => {
expect(component.navItems.length).toBe(11);
});
it('should include Home nav item', () => {
const home = component.navItems.find(i => i.path === '/');
expect(home).toBeTruthy();
expect(home!.label).toBe('Home');
expect(home!.icon).toBe('home');
});
it('should include Compiler nav item', () => {
const compiler = component.navItems.find(i => i.path === '/compiler');
expect(compiler).toBeTruthy();
expect(compiler!.label).toBe('Compiler');
});
it('should include Admin nav item', () => {
const admin = component.navItems.find(i => i.path === '/admin');
expect(admin).toBeTruthy();
expect(admin!.label).toBe('Admin');
});
it('should start with sidenav closed (desktop uses horizontal nav tabs)', () => {
expect(component.sidenavOpen()).toBe(false);
});
it('should toggle sidenav', () => {
expect(component.sidenavOpen()).toBe(false);
component.toggleSidenav();
expect(component.sidenavOpen()).toBe(true);
component.toggleSidenav();
expect(component.sidenavOpen()).toBe(false);
});
it('should default isMobile to false', () => {
// In jsdom, BreakpointObserver resolves to non-mobile
expect(component.isMobile()).toBe(false);
});
it('should expose theme service', () => {
expect(component.themeService).toBeTruthy();
expect(component.themeService.isDark()).toBe(false);
});
it('should render the toolbar title', async () => {
await fixture.whenStable();
const el: HTMLElement = fixture.nativeElement;
expect(el.querySelector('.toolbar-title')?.textContent).toContain('Bloqr');
});
it('should render the main content area with role=main', async () => {
await fixture.whenStable();
const main = fixture.nativeElement.querySelector('[role="main"]');
expect(main).toBeTruthy();
expect(main.getAttribute('aria-label')).toBe('Main content');
});
it('should NOT render menu button in desktop viewport (isMobile is false)', async () => {
await fixture.whenStable();
const menuBtn = fixture.nativeElement.querySelector('button[aria-label="Toggle navigation"]');
expect(menuBtn).toBeNull();
});
});
describe('AppComponent (mobile viewport)', () => {
let fixture: ComponentFixture<AppComponent>;
let httpTesting: HttpTestingController;
beforeEach(async () => {
localStorage.clear();
await TestBed.configureTestingModule({
imports: [AppComponent, NoopAnimationsModule],
providers: [
provideZonelessChangeDetection(),
// Provide a non-root route so we can navigate away from `/` to reveal the app shell.
provideRouter([{ path: 'compiler', component: AppComponent }]),
provideHttpClient(),
provideHttpClientTesting(),
{ provide: ErrorHandler, useClass: GlobalErrorHandler },
{ provide: API_BASE_URL, useValue: '/api' },
{
provide: BreakpointObserver,
useValue: { observe: () => of({ matches: true } as BreakpointState) },
},
],
}).compileComponents();
fixture = TestBed.createComponent(AppComponent);
httpTesting = TestBed.inject(HttpTestingController);
httpTesting.match('/api/metrics').forEach(req => req.flush({
totalRequests: 0, averageDuration: 0, cacheHitRate: 0, successRate: 0,
}));
httpTesting.match('/api/health').forEach(req => req.flush({
status: 'healthy', version: '0.0.0',
}));
await fixture.whenStable();
});
afterEach(() => {
httpTesting.match(() => true).forEach(req => req.flush({}));
httpTesting.verify();
localStorage.clear();
});
it('should render menu button with aria-label on non-landing routes', async () => {
// The app shell hamburger is only shown on non-landing-page routes.
// Navigate away from `/` to make isLandingPage() = false and reveal the shell header.
const router = TestBed.inject(Router);
await router.navigate(['/compiler']);
await fixture.whenStable();
fixture.detectChanges();
const menuBtn = fixture.nativeElement.querySelector('button[aria-label="Toggle navigation"]');
expect(menuBtn).toBeTruthy();
});
});