|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Sidebar Navigation Tests |
| 5 | + * |
| 6 | + * These tests verify that the sidebar navigation works correctly. |
| 7 | + * Tests cover: |
| 8 | + * - Navigation items are visible and clickable |
| 9 | + * - Clicking navigation items navigates to correct routes |
| 10 | + * - Active state highlighting works correctly |
| 11 | + * - Keyboard navigation support |
| 12 | + */ |
| 13 | + |
| 14 | +test.describe('Sidebar Navigation', () => { |
| 15 | + test.beforeEach(async ({ page }) => { |
| 16 | + await page.goto('/dashboard'); |
| 17 | + // Wait for sidebar to be visible |
| 18 | + await page.waitForSelector('aside, nav[aria-label="Primary navigation"]', { timeout: 10000 }); |
| 19 | + }); |
| 20 | + |
| 21 | + test('should display all navigation items', async ({ page }) => { |
| 22 | + const navItems = [ |
| 23 | + 'Dashboard', |
| 24 | + 'Code Quality', |
| 25 | + 'Test Coverage', |
| 26 | + 'Dependencies', |
| 27 | + 'Trends', |
| 28 | + 'Graph', |
| 29 | + 'Compare', |
| 30 | + 'Reports', |
| 31 | + 'AI Insights', |
| 32 | + 'Predictions', |
| 33 | + 'Analytics', |
| 34 | + 'Settings', |
| 35 | + ]; |
| 36 | + |
| 37 | + for (const item of navItems) { |
| 38 | + const navLink = page.getByRole('button', { name: item }).or( |
| 39 | + page.locator(`text="${item}"`) |
| 40 | + ); |
| 41 | + await expect(navLink.first()).toBeVisible(); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + test('should navigate to Code Quality page when clicked', async ({ page }) => { |
| 46 | + const qualityButton = page.getByRole('button', { name: /Code Quality/i }).first(); |
| 47 | + await qualityButton.click(); |
| 48 | + |
| 49 | + await page.waitForURL(/\/dashboard\/quality/, { timeout: 10000 }); |
| 50 | + await expect(page).toHaveURL(/\/dashboard\/quality/); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should navigate to Test Coverage page when clicked', async ({ page }) => { |
| 54 | + const coverageButton = page.getByRole('button', { name: /Test Coverage/i }).first(); |
| 55 | + await coverageButton.click(); |
| 56 | + |
| 57 | + await page.waitForURL(/\/dashboard\/coverage/, { timeout: 10000 }); |
| 58 | + await expect(page).toHaveURL(/\/dashboard\/coverage/); |
| 59 | + }); |
| 60 | + |
| 61 | + test('should navigate to Dependencies page when clicked', async ({ page }) => { |
| 62 | + const depsButton = page.getByRole('button', { name: /Dependencies/i }).first(); |
| 63 | + await depsButton.click(); |
| 64 | + |
| 65 | + await page.waitForURL(/\/dashboard\/dependencies/, { timeout: 10000 }); |
| 66 | + await expect(page).toHaveURL(/\/dashboard\/dependencies/); |
| 67 | + }); |
| 68 | + |
| 69 | + test('should navigate to Trends page when clicked', async ({ page }) => { |
| 70 | + const trendsButton = page.getByRole('button', { name: /Trends/i }).first(); |
| 71 | + await trendsButton.click(); |
| 72 | + |
| 73 | + await page.waitForURL(/\/dashboard\/trends/, { timeout: 10000 }); |
| 74 | + await expect(page).toHaveURL(/\/dashboard\/trends/); |
| 75 | + }); |
| 76 | + |
| 77 | + test('should navigate to Graph page when clicked', async ({ page }) => { |
| 78 | + const graphButton = page.getByRole('button', { name: /Graph/i }).first(); |
| 79 | + await graphButton.click(); |
| 80 | + |
| 81 | + await page.waitForURL(/\/dashboard\/graph/, { timeout: 10000 }); |
| 82 | + await expect(page).toHaveURL(/\/dashboard\/graph/); |
| 83 | + }); |
| 84 | + |
| 85 | + test('should navigate to Compare page when clicked', async ({ page }) => { |
| 86 | + const compareButton = page.getByRole('button', { name: /Compare/i }).first(); |
| 87 | + await compareButton.click(); |
| 88 | + |
| 89 | + await page.waitForURL(/\/dashboard\/compare/, { timeout: 10000 }); |
| 90 | + await expect(page).toHaveURL(/\/dashboard\/compare/); |
| 91 | + }); |
| 92 | + |
| 93 | + test('should navigate to Reports page when clicked', async ({ page }) => { |
| 94 | + const reportsButton = page.getByRole('button', { name: /Reports/i }).first(); |
| 95 | + await reportsButton.click(); |
| 96 | + |
| 97 | + await page.waitForURL(/\/dashboard\/reports/, { timeout: 10000 }); |
| 98 | + await expect(page).toHaveURL(/\/dashboard\/reports/); |
| 99 | + }); |
| 100 | + |
| 101 | + test('should navigate back to Dashboard when clicked', async ({ page }) => { |
| 102 | + // First navigate away |
| 103 | + const qualityButton = page.getByRole('button', { name: /Code Quality/i }).first(); |
| 104 | + await qualityButton.click(); |
| 105 | + await page.waitForURL(/\/dashboard\/quality/, { timeout: 10000 }); |
| 106 | + |
| 107 | + // Then navigate back to dashboard |
| 108 | + const dashboardButton = page.getByRole('button', { name: /^Dashboard$/i }).first(); |
| 109 | + await dashboardButton.click(); |
| 110 | + |
| 111 | + await page.waitForURL(/\/dashboard\/?$/, { timeout: 10000 }); |
| 112 | + }); |
| 113 | + |
| 114 | + test('should highlight active navigation item', async ({ page }) => { |
| 115 | + // Navigate to Quality page |
| 116 | + const qualityButton = page.getByRole('button', { name: /Code Quality/i }).first(); |
| 117 | + await qualityButton.click(); |
| 118 | + await page.waitForURL(/\/dashboard\/quality/, { timeout: 10000 }); |
| 119 | + |
| 120 | + // Check that the Quality item has aria-current="page" |
| 121 | + const activeItem = page.locator('[aria-current="page"]'); |
| 122 | + await expect(activeItem).toBeVisible(); |
| 123 | + await expect(activeItem).toContainText(/Code Quality/i); |
| 124 | + }); |
| 125 | + |
| 126 | + test('should support keyboard navigation with Enter key', async ({ page }) => { |
| 127 | + // Focus on a navigation item |
| 128 | + const qualityButton = page.getByRole('button', { name: /Code Quality/i }).first(); |
| 129 | + await qualityButton.focus(); |
| 130 | + |
| 131 | + // Press Enter to navigate |
| 132 | + await page.keyboard.press('Enter'); |
| 133 | + |
| 134 | + await page.waitForURL(/\/dashboard\/quality/, { timeout: 10000 }); |
| 135 | + await expect(page).toHaveURL(/\/dashboard\/quality/); |
| 136 | + }); |
| 137 | + |
| 138 | + test('should navigate through all main sections', async ({ page }) => { |
| 139 | + const routes = [ |
| 140 | + { name: /Code Quality/i, url: /\/dashboard\/quality/ }, |
| 141 | + { name: /Test Coverage/i, url: /\/dashboard\/coverage/ }, |
| 142 | + { name: /Dependencies/i, url: /\/dashboard\/dependencies/ }, |
| 143 | + { name: /Trends/i, url: /\/dashboard\/trends/ }, |
| 144 | + ]; |
| 145 | + |
| 146 | + for (const route of routes) { |
| 147 | + const button = page.getByRole('button', { name: route.name }).first(); |
| 148 | + await button.click(); |
| 149 | + await page.waitForURL(route.url, { timeout: 10000 }); |
| 150 | + await expect(page).toHaveURL(route.url); |
| 151 | + } |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +test.describe('Sidebar Navigation - Mobile', () => { |
| 156 | + test.beforeEach(async ({ page }) => { |
| 157 | + // Set mobile viewport |
| 158 | + await page.setViewportSize({ width: 375, height: 667 }); |
| 159 | + await page.goto('/dashboard'); |
| 160 | + }); |
| 161 | + |
| 162 | + test('should show hamburger menu on mobile', async ({ page }) => { |
| 163 | + // On mobile, sidebar is hidden and hamburger menu appears |
| 164 | + const hamburgerButton = page.getByRole('button', { name: /open navigation/i }).or( |
| 165 | + page.locator('[aria-label*="menu"]') |
| 166 | + ); |
| 167 | + |
| 168 | + // Hamburger button should be visible on mobile |
| 169 | + const count = await hamburgerButton.count(); |
| 170 | + if (count > 0) { |
| 171 | + await expect(hamburgerButton.first()).toBeVisible(); |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + test('should open drawer when hamburger is clicked', async ({ page }) => { |
| 176 | + const hamburgerButton = page.getByRole('button', { name: /open navigation/i }).or( |
| 177 | + page.locator('[aria-label*="menu"]') |
| 178 | + ).first(); |
| 179 | + |
| 180 | + const count = await hamburgerButton.count(); |
| 181 | + if (count === 0) { |
| 182 | + test.skip(); |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + await hamburgerButton.click(); |
| 187 | + |
| 188 | + // Drawer should now be visible |
| 189 | + const drawer = page.locator('.MuiDrawer-root').or( |
| 190 | + page.locator('[role="presentation"]') |
| 191 | + ); |
| 192 | + await expect(drawer.first()).toBeVisible(); |
| 193 | + }); |
| 194 | +}); |
| 195 | + |
| 196 | +test.describe('Sidebar Navigation - Focus Management', () => { |
| 197 | + test.beforeEach(async ({ page }) => { |
| 198 | + await page.goto('/dashboard'); |
| 199 | + await page.waitForSelector('aside, nav[aria-label="Primary navigation"]', { timeout: 10000 }); |
| 200 | + }); |
| 201 | + |
| 202 | + test('should show focus indicator on keyboard navigation', async ({ page }) => { |
| 203 | + // Tab to first navigation item |
| 204 | + await page.keyboard.press('Tab'); |
| 205 | + await page.keyboard.press('Tab'); // May need multiple tabs to reach nav |
| 206 | + |
| 207 | + // Find the focused element |
| 208 | + const focusedElement = page.locator(':focus'); |
| 209 | + const isFocusInNav = await focusedElement.evaluate(el => { |
| 210 | + return el.closest('nav, aside') !== null; |
| 211 | + }).catch(() => false); |
| 212 | + |
| 213 | + // At some point focus should be in navigation area |
| 214 | + // This is a soft assertion as focus order may vary |
| 215 | + expect(typeof isFocusInNav).toBe('boolean'); |
| 216 | + }); |
| 217 | + |
| 218 | + test('should have accessible labels on navigation items', async ({ page }) => { |
| 219 | + const nav = page.locator('nav[aria-label="Primary navigation"]'); |
| 220 | + |
| 221 | + if (await nav.count() > 0) { |
| 222 | + await expect(nav).toHaveAttribute('aria-label', 'Primary navigation'); |
| 223 | + } |
| 224 | + }); |
| 225 | +}); |
0 commit comments