Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions frontend/src/components/NotificationDropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,53 @@ describe('NotificationDropdown empty state', () => {
expect(await screen.findByText('New stream #7 created')).toBeInTheDocument();
expect(screen.queryByTestId('notifications-empty-state')).not.toBeInTheDocument();
});

it('exposes accurate aria-expanded and aria-controls attributes on the bell trigger', async () => {
mockStream();
render(<NotificationDropdown publicKey={PUBLIC_KEY} />);

const bellButton = screen.getByRole('button', { name: /notifications/i });
expect(bellButton).toHaveAttribute('aria-expanded', 'false');
expect(bellButton).toHaveAttribute('aria-controls', 'notifications-dropdown-menu');

await userEvent.click(bellButton);
expect(bellButton).toHaveAttribute('aria-expanded', 'true');
expect(document.getElementById('notifications-dropdown-menu')).toBeInTheDocument();
});

it('closes the dropdown on Escape and returns keyboard focus to the bell trigger button', async () => {
mockStream();
render(<NotificationDropdown publicKey={PUBLIC_KEY} />);

const bellButton = screen.getByRole('button', { name: /notifications/i });
await userEvent.click(bellButton);
expect(screen.getByRole('dialog', { name: 'Notifications' })).toBeInTheDocument();

// Move focus away from the bell button to a button inside the dialog
const closeButton = screen.getByRole('button', { name: 'Close notifications' });
closeButton.focus();
expect(closeButton).toHaveFocus();

await userEvent.keyboard('{Escape}');
expect(screen.queryByRole('dialog', { name: 'Notifications' })).not.toBeInTheDocument();
expect(bellButton).toHaveAttribute('aria-expanded', 'false');
expect(bellButton).toHaveFocus();
});

it('announces unread notification count via an aria-live polite region', async () => {
mockStream({
events: [
{
type: 'created',
data: { streamId: 42 },
timestamp: 1_700_000_000_000,
},
],
});
const { container } = render(<NotificationDropdown publicKey={PUBLIC_KEY} />);

const liveRegion = container.querySelector('[aria-live="polite"]');
expect(liveRegion).not.toBeNull();
expect(await screen.findByText('1 new notifications')).toBeInTheDocument();
});
});
10 changes: 9 additions & 1 deletion frontend/src/components/NotificationDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const NotificationDropdown: React.FC<NotificationDropdownProps> = ({ publ
const [notifications, setNotifications] = useState<NotificationItem[]>([]);
const [unreadCount, setUnreadCount] = useState(0);
const containerRef = useRef<HTMLDivElement>(null);
const triggerRef = useRef<HTMLButtonElement>(null);

const { events: streamEvents, connected } = useStreamEvents({
userPublicKeys: [publicKey],
Expand Down Expand Up @@ -122,6 +123,7 @@ export const NotificationDropdown: React.FC<NotificationDropdownProps> = ({ publ
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") {
setIsOpen(false);
triggerRef.current?.focus();
return;
}

Expand Down Expand Up @@ -178,9 +180,11 @@ export const NotificationDropdown: React.FC<NotificationDropdownProps> = ({ publ
{unreadCount} new notifications
</span>
<button
ref={triggerRef}
onClick={handleDropdownOpen}
aria-label={`Notifications${unreadCount > 0 ? `, ${unreadCount} unread` : ""}`}
aria-expanded={isOpen}
aria-controls="notifications-dropdown-menu"
aria-haspopup="dialog"
className="relative p-2 text-slate-400 hover:text-accent transition-colors"
disabled={!connected}
Expand All @@ -207,6 +211,7 @@ export const NotificationDropdown: React.FC<NotificationDropdownProps> = ({ publ

{isOpen && (
<div
id="notifications-dropdown-menu"
role="dialog"
aria-label="Notifications"
className="absolute right-0 mt-2 w-80 bg-background/95 backdrop-blur-md border border-glass-border rounded-2xl shadow-2xl z-[100] overflow-hidden animate-in fade-in slide-in-from-top-2"
Expand All @@ -218,7 +223,10 @@ export const NotificationDropdown: React.FC<NotificationDropdownProps> = ({ publ
{!connected && <span className="text-xs text-red-400">Reconnecting...</span>}

<button
onClick={() => setIsOpen(false)}
onClick={() => {
setIsOpen(false);
triggerRef.current?.focus();
}}
aria-label="Close notifications"
className="text-slate-400 hover:text-white"
>
Expand Down
Loading