|
| 1 | +import { TestBed, type ComponentFixture } from '@angular/core/testing'; |
| 2 | +import { provideRouter, Router } from '@angular/router'; |
| 3 | +import { Component } from '@angular/core'; |
| 4 | +import { ShellComponent } from './shell'; |
| 5 | +import { SpecStoreService } from '../../services/spec-store.service'; |
| 6 | + |
| 7 | +@Component({ selector: 'app-dummy', standalone: true, template: '' }) |
| 8 | +class DummyComponent {} |
| 9 | + |
| 10 | +describe('ShellComponent', () => { |
| 11 | + let component: ShellComponent; |
| 12 | + let fixture: ComponentFixture<ShellComponent>; |
| 13 | + let router: Router; |
| 14 | + let storeSpy: Record<string, ReturnType<typeof vi.fn>>; |
| 15 | + |
| 16 | + function resizeWindow(width: number): void { |
| 17 | + Object.defineProperty(window, 'innerWidth', { |
| 18 | + value: width, |
| 19 | + writable: true, |
| 20 | + configurable: true, |
| 21 | + }); |
| 22 | + window.dispatchEvent(new Event('resize')); |
| 23 | + } |
| 24 | + |
| 25 | + /** Flush queueMicrotask callbacks */ |
| 26 | + function flushMicrotasks(): Promise<void> { |
| 27 | + return new Promise((resolve) => queueMicrotask(resolve)); |
| 28 | + } |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + // Default to desktop width |
| 32 | + Object.defineProperty(window, 'innerWidth', { |
| 33 | + value: 1024, |
| 34 | + writable: true, |
| 35 | + configurable: true, |
| 36 | + }); |
| 37 | + |
| 38 | + storeSpy = { |
| 39 | + suites: vi.fn(() => new Map()), |
| 40 | + allSpecs: vi.fn(() => []), |
| 41 | + activeSpec: vi.fn(() => null), |
| 42 | + createSpec: vi.fn(async () => ({ id: 1 })), |
| 43 | + selectSpec: vi.fn(async () => {}), |
| 44 | + deleteSpec: vi.fn(async () => {}), |
| 45 | + importMarkdownFiles: vi.fn(async () => 0), |
| 46 | + }; |
| 47 | + |
| 48 | + await TestBed.configureTestingModule({ |
| 49 | + imports: [ShellComponent], |
| 50 | + providers: [ |
| 51 | + provideRouter([ |
| 52 | + { path: '', component: DummyComponent }, |
| 53 | + { path: 'other', component: DummyComponent }, |
| 54 | + ]), |
| 55 | + { provide: SpecStoreService, useValue: storeSpy }, |
| 56 | + ], |
| 57 | + }).compileComponents(); |
| 58 | + |
| 59 | + fixture = TestBed.createComponent(ShellComponent); |
| 60 | + component = fixture.componentInstance; |
| 61 | + router = TestBed.inject(Router); |
| 62 | + fixture.detectChanges(); |
| 63 | + }); |
| 64 | + |
| 65 | + // --- Creation --- |
| 66 | + |
| 67 | + it('should create', () => { |
| 68 | + expect(component).toBeTruthy(); |
| 69 | + }); |
| 70 | + |
| 71 | + // --- toggleSidebar --- |
| 72 | + |
| 73 | + describe('toggleSidebar()', () => { |
| 74 | + it('toggles from true to false', () => { |
| 75 | + component.sidebarOpen.set(true); |
| 76 | + component.toggleSidebar(); |
| 77 | + expect(component.sidebarOpen()).toBe(false); |
| 78 | + }); |
| 79 | + |
| 80 | + it('toggles from false to true', () => { |
| 81 | + component.sidebarOpen.set(false); |
| 82 | + component.toggleSidebar(); |
| 83 | + expect(component.sidebarOpen()).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it('alternates on repeated calls', () => { |
| 87 | + const initial = component.sidebarOpen(); |
| 88 | + component.toggleSidebar(); |
| 89 | + expect(component.sidebarOpen()).toBe(!initial); |
| 90 | + component.toggleSidebar(); |
| 91 | + expect(component.sidebarOpen()).toBe(initial); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + // --- Auto-close on NavigationEnd --- |
| 96 | + |
| 97 | + describe('auto-close on NavigationEnd', () => { |
| 98 | + it('closes sidebar on navigation when viewport is mobile width', async () => { |
| 99 | + resizeWindow(600); |
| 100 | + component.sidebarOpen.set(true); |
| 101 | + |
| 102 | + await router.navigateByUrl('/other'); |
| 103 | + fixture.detectChanges(); |
| 104 | + |
| 105 | + expect(component.sidebarOpen()).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + it('keeps sidebar open on navigation when viewport is desktop width', async () => { |
| 109 | + resizeWindow(1024); |
| 110 | + component.sidebarOpen.set(true); |
| 111 | + |
| 112 | + await router.navigateByUrl('/other'); |
| 113 | + fixture.detectChanges(); |
| 114 | + |
| 115 | + expect(component.sidebarOpen()).toBe(true); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + // --- isMobile signal --- |
| 120 | + |
| 121 | + describe('isMobile signal', () => { |
| 122 | + it('is false for desktop width', () => { |
| 123 | + resizeWindow(1024); |
| 124 | + fixture.detectChanges(); |
| 125 | + expect(component.isMobile()).toBe(false); |
| 126 | + }); |
| 127 | + |
| 128 | + it('is true for mobile width', () => { |
| 129 | + resizeWindow(600); |
| 130 | + fixture.detectChanges(); |
| 131 | + expect(component.isMobile()).toBe(true); |
| 132 | + }); |
| 133 | + |
| 134 | + it('updates from desktop to mobile on resize', () => { |
| 135 | + resizeWindow(1024); |
| 136 | + fixture.detectChanges(); |
| 137 | + expect(component.isMobile()).toBe(false); |
| 138 | + |
| 139 | + resizeWindow(500); |
| 140 | + fixture.detectChanges(); |
| 141 | + expect(component.isMobile()).toBe(true); |
| 142 | + }); |
| 143 | + |
| 144 | + it('updates from mobile to desktop on resize', () => { |
| 145 | + resizeWindow(500); |
| 146 | + fixture.detectChanges(); |
| 147 | + expect(component.isMobile()).toBe(true); |
| 148 | + |
| 149 | + resizeWindow(1024); |
| 150 | + fixture.detectChanges(); |
| 151 | + expect(component.isMobile()).toBe(false); |
| 152 | + }); |
| 153 | + |
| 154 | + it('is true at 767px (just below breakpoint)', () => { |
| 155 | + resizeWindow(767); |
| 156 | + fixture.detectChanges(); |
| 157 | + expect(component.isMobile()).toBe(true); |
| 158 | + }); |
| 159 | + |
| 160 | + it('is false at 768px (at breakpoint)', () => { |
| 161 | + resizeWindow(768); |
| 162 | + fixture.detectChanges(); |
| 163 | + expect(component.isMobile()).toBe(false); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + // --- isDialog computed --- |
| 168 | + |
| 169 | + describe('isDialog computed', () => { |
| 170 | + it('is true when mobile and sidebar open', () => { |
| 171 | + resizeWindow(600); |
| 172 | + fixture.detectChanges(); |
| 173 | + component.sidebarOpen.set(true); |
| 174 | + expect(component.isDialog()).toBe(true); |
| 175 | + }); |
| 176 | + |
| 177 | + it('is false when mobile and sidebar closed', () => { |
| 178 | + resizeWindow(600); |
| 179 | + fixture.detectChanges(); |
| 180 | + component.sidebarOpen.set(false); |
| 181 | + expect(component.isDialog()).toBe(false); |
| 182 | + }); |
| 183 | + |
| 184 | + it('is false when desktop and sidebar open', () => { |
| 185 | + resizeWindow(1024); |
| 186 | + fixture.detectChanges(); |
| 187 | + component.sidebarOpen.set(true); |
| 188 | + expect(component.isDialog()).toBe(false); |
| 189 | + }); |
| 190 | + |
| 191 | + it('is false when desktop and sidebar closed', () => { |
| 192 | + resizeWindow(1024); |
| 193 | + fixture.detectChanges(); |
| 194 | + component.sidebarOpen.set(false); |
| 195 | + expect(component.isDialog()).toBe(false); |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + // --- Escape key --- |
| 200 | + |
| 201 | + describe('escape key', () => { |
| 202 | + it('closes sidebar when in dialog mode (mobile + open)', () => { |
| 203 | + resizeWindow(600); |
| 204 | + fixture.detectChanges(); |
| 205 | + component.sidebarOpen.set(true); |
| 206 | + expect(component.isDialog()).toBe(true); |
| 207 | + |
| 208 | + component.onEscape(); |
| 209 | + expect(component.sidebarOpen()).toBe(false); |
| 210 | + }); |
| 211 | + |
| 212 | + it('does nothing on desktop', () => { |
| 213 | + resizeWindow(1024); |
| 214 | + fixture.detectChanges(); |
| 215 | + component.sidebarOpen.set(true); |
| 216 | + |
| 217 | + component.onEscape(); |
| 218 | + expect(component.sidebarOpen()).toBe(true); |
| 219 | + }); |
| 220 | + |
| 221 | + it('does nothing when sidebar is already closed', () => { |
| 222 | + resizeWindow(600); |
| 223 | + fixture.detectChanges(); |
| 224 | + component.sidebarOpen.set(false); |
| 225 | + |
| 226 | + component.onEscape(); |
| 227 | + expect(component.sidebarOpen()).toBe(false); |
| 228 | + }); |
| 229 | + }); |
| 230 | + |
| 231 | + // --- Focus management --- |
| 232 | + |
| 233 | + describe('focus management', () => { |
| 234 | + it('moves focus to close button when sidebar opens on mobile', async () => { |
| 235 | + resizeWindow(600); |
| 236 | + fixture.detectChanges(); |
| 237 | + component.sidebarOpen.set(false); |
| 238 | + fixture.detectChanges(); |
| 239 | + await flushMicrotasks(); |
| 240 | + |
| 241 | + component.sidebarOpen.set(true); |
| 242 | + fixture.detectChanges(); |
| 243 | + await flushMicrotasks(); |
| 244 | + |
| 245 | + const closeBtn = fixture.nativeElement.querySelector('.sidebar-close'); |
| 246 | + if (closeBtn) { |
| 247 | + // Focus may or may not land depending on test env DOM support |
| 248 | + // At minimum, the close button should exist |
| 249 | + expect(closeBtn).toBeTruthy(); |
| 250 | + } |
| 251 | + }); |
| 252 | + }); |
| 253 | + |
| 254 | + // --- Template bindings --- |
| 255 | + |
| 256 | + describe('template', () => { |
| 257 | + it('applies sidebar-open class when sidebar is open', () => { |
| 258 | + component.sidebarOpen.set(true); |
| 259 | + fixture.detectChanges(); |
| 260 | + const sidebar = fixture.nativeElement.querySelector('.sidebar'); |
| 261 | + expect(sidebar.classList.contains('sidebar-open')).toBe(true); |
| 262 | + }); |
| 263 | + |
| 264 | + it('removes sidebar-open class when sidebar is closed', () => { |
| 265 | + component.sidebarOpen.set(false); |
| 266 | + fixture.detectChanges(); |
| 267 | + const sidebar = fixture.nativeElement.querySelector('.sidebar'); |
| 268 | + expect(sidebar.classList.contains('sidebar-open')).toBe(false); |
| 269 | + }); |
| 270 | + |
| 271 | + it('sets role="dialog" when in dialog mode', () => { |
| 272 | + resizeWindow(600); |
| 273 | + fixture.detectChanges(); |
| 274 | + component.sidebarOpen.set(true); |
| 275 | + fixture.detectChanges(); |
| 276 | + const sidebar = fixture.nativeElement.querySelector('.sidebar'); |
| 277 | + expect(sidebar.getAttribute('role')).toBe('dialog'); |
| 278 | + }); |
| 279 | + |
| 280 | + it('sets role="complementary" when not in dialog mode', () => { |
| 281 | + resizeWindow(1024); |
| 282 | + fixture.detectChanges(); |
| 283 | + component.sidebarOpen.set(true); |
| 284 | + fixture.detectChanges(); |
| 285 | + const sidebar = fixture.nativeElement.querySelector('.sidebar'); |
| 286 | + expect(sidebar.getAttribute('role')).toBe('complementary'); |
| 287 | + }); |
| 288 | + |
| 289 | + it('sets aria-modal="true" in dialog mode', () => { |
| 290 | + resizeWindow(600); |
| 291 | + fixture.detectChanges(); |
| 292 | + component.sidebarOpen.set(true); |
| 293 | + fixture.detectChanges(); |
| 294 | + const sidebar = fixture.nativeElement.querySelector('.sidebar'); |
| 295 | + expect(sidebar.getAttribute('aria-modal')).toBe('true'); |
| 296 | + }); |
| 297 | + |
| 298 | + it('toggles sidebar when close button is clicked', () => { |
| 299 | + component.sidebarOpen.set(true); |
| 300 | + fixture.detectChanges(); |
| 301 | + const closeBtn = fixture.nativeElement.querySelector('.sidebar-close'); |
| 302 | + closeBtn.click(); |
| 303 | + expect(component.sidebarOpen()).toBe(false); |
| 304 | + }); |
| 305 | + |
| 306 | + it('toggles sidebar when backdrop is clicked', () => { |
| 307 | + component.sidebarOpen.set(true); |
| 308 | + fixture.detectChanges(); |
| 309 | + const backdrop = fixture.nativeElement.querySelector('.sidebar-backdrop'); |
| 310 | + backdrop.click(); |
| 311 | + expect(component.sidebarOpen()).toBe(false); |
| 312 | + }); |
| 313 | + |
| 314 | + it('toggles sidebar when mobile menu button is clicked', () => { |
| 315 | + component.sidebarOpen.set(false); |
| 316 | + fixture.detectChanges(); |
| 317 | + const menuBtn = fixture.nativeElement.querySelector('.mobile-menu-btn'); |
| 318 | + menuBtn.click(); |
| 319 | + expect(component.sidebarOpen()).toBe(true); |
| 320 | + }); |
| 321 | + |
| 322 | + it('updates aria-expanded on menu button', () => { |
| 323 | + component.sidebarOpen.set(true); |
| 324 | + fixture.detectChanges(); |
| 325 | + const menuBtn = fixture.nativeElement.querySelector('.mobile-menu-btn'); |
| 326 | + expect(menuBtn.getAttribute('aria-expanded')).toBe('true'); |
| 327 | + |
| 328 | + component.sidebarOpen.set(false); |
| 329 | + fixture.detectChanges(); |
| 330 | + expect(menuBtn.getAttribute('aria-expanded')).toBe('false'); |
| 331 | + }); |
| 332 | + }); |
| 333 | +}); |
0 commit comments