feat(settings): role-aware settings menu
Add an optional `roles` filter to the settings menu and hide items that don't apply to the current user's role, so a doctor no longer sees "مدیریت مطب" (clinic-only) and a clinic no longer sees "مدیریت پزشک" / "مدیریت نوبت دهی" (doctor-only) — avoiding menu entries that just redirect. Both the desktop shell (SettingsLayout) and the mobile list (SettingsMenuPage) filter via the shared `menuForRole` helper. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,45 +1,54 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { screen, fireEvent } from '@testing-library/react';
|
||||
import { renderWithProviders } from '../../test/utils';
|
||||
import SettingsLayout, { SETTINGS_MENU } from './SettingsLayout';
|
||||
import SettingsLayout, { menuForRole } from './SettingsLayout';
|
||||
import { useAuthStore } from '../../stores/authStore';
|
||||
|
||||
beforeEach(() => {
|
||||
useAuthStore.setState({ primaryRole: 'doctor' });
|
||||
});
|
||||
|
||||
describe('SettingsLayout', () => {
|
||||
it('renders every settings menu item and the section content', () => {
|
||||
it('renders every menu item available to the role, plus the content', () => {
|
||||
renderWithProviders(
|
||||
<SettingsLayout active="subscription">
|
||||
<div>محتوای اشتراک</div>
|
||||
</SettingsLayout>,
|
||||
);
|
||||
|
||||
for (const item of SETTINGS_MENU) {
|
||||
for (const item of menuForRole('doctor')) {
|
||||
expect(screen.getByText(item.label)).toBeInTheDocument();
|
||||
}
|
||||
expect(screen.getByText('محتوای اشتراک')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('marks the active item with aria-current=page', () => {
|
||||
renderWithProviders(
|
||||
<SettingsLayout active="subscription"><div /></SettingsLayout>,
|
||||
);
|
||||
renderWithProviders(<SettingsLayout active="subscription"><div /></SettingsLayout>);
|
||||
const active = screen.getByText('خرید اشتراک').closest('a');
|
||||
expect(active).toHaveAttribute('aria-current', 'page');
|
||||
expect(active).toHaveAttribute('href', '/admin/subscription');
|
||||
});
|
||||
|
||||
it('renders every menu item as a navigable link', () => {
|
||||
renderWithProviders(
|
||||
<SettingsLayout active="subscription"><div /></SettingsLayout>,
|
||||
);
|
||||
// all items are now wired to a route
|
||||
it('renders every visible item as a navigable link', () => {
|
||||
renderWithProviders(<SettingsLayout active="subscription"><div /></SettingsLayout>);
|
||||
expect(screen.getByText('مدیریت نوبت دهی').closest('a')).toHaveAttribute('href', '/admin/appointment-settings');
|
||||
expect(screen.getByText('برچسبها').closest('a')).toHaveAttribute('href', '/admin/tags-settings');
|
||||
expect(screen.queryByText('بهزودی')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('hides role-specific items from other roles', () => {
|
||||
// doctor sees مدیریت پزشک but not مدیریت مطب (clinic-only)
|
||||
renderWithProviders(<SettingsLayout active="subscription"><div /></SettingsLayout>);
|
||||
expect(screen.getByText('مدیریت پزشک')).toBeInTheDocument();
|
||||
expect(screen.queryByText('مدیریت مطب')).not.toBeInTheDocument();
|
||||
|
||||
// clinic sees مدیریت مطب but not مدیریت پزشک / نوبت دهی
|
||||
expect(menuForRole('clinic').map((i) => i.key)).toContain('clinic');
|
||||
expect(menuForRole('clinic').map((i) => i.key)).not.toContain('doctor');
|
||||
expect(menuForRole('clinic').map((i) => i.key)).not.toContain('appointment');
|
||||
});
|
||||
|
||||
it('filters the menu by the search query', () => {
|
||||
renderWithProviders(
|
||||
<SettingsLayout active="subscription"><div /></SettingsLayout>,
|
||||
);
|
||||
renderWithProviders(<SettingsLayout active="subscription"><div /></SettingsLayout>);
|
||||
fireEvent.change(screen.getByLabelText('جستجو در تنظیمات'), { target: { value: 'اشتراک' } });
|
||||
expect(screen.getByText('خرید اشتراک')).toBeInTheDocument();
|
||||
expect(screen.queryByText('خدمات')).not.toBeInTheDocument();
|
||||
|
||||
Reference in New Issue
Block a user