admin-layout.test.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { App as AntApp, ConfigProvider } from 'antd';
  2. import { render, screen, waitFor } from '@testing-library/react';
  3. import userEvent from '@testing-library/user-event';
  4. import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom';
  5. import { describe, expect, it, vi } from 'vitest';
  6. import { AppContext } from '../../src/frontend/app-context';
  7. import { I18nProvider } from '../../src/frontend/i18n/react';
  8. import { mailhubTheme } from '../../src/frontend/theme';
  9. import { AdminLayout, navigationSelection, visibleNavigation } from '../../src/layouts/AdminLayout';
  10. import type { AppContextValue } from '../../src/frontend/app-context';
  11. describe('AdminLayout navigation', () => {
  12. it('hides all system management destinations from a normal user', () => {
  13. const paths = visibleNavigation(false).flatMap((group) => group.items.map((item) => item.path));
  14. expect(paths).not.toContain('/admin/users');
  15. expect(paths).not.toContain('/settings');
  16. });
  17. it('maps detail routes back to their owning navigation item', () => {
  18. expect(navigationSelection('/activity/42')).toBe('/activity');
  19. expect(navigationSelection('/domains/7/dns')).toBe('/domains');
  20. expect(navigationSelection('/inbox/messages/9')).toBe('/inbox');
  21. });
  22. it('navigates with semantic menu items and exposes a skip link', async () => {
  23. const user = userEvent.setup();
  24. renderShell('/overview', { role: 'admin' });
  25. expect(screen.getByRole('link', { name: '跳至主要内容' }).getAttribute('href')).toBe('#main-content');
  26. const activityItems = screen.getAllByText('发送活动');
  27. await user.click(activityItems[0]);
  28. expect(screen.getByTestId('location').textContent).toBe('/activity');
  29. await waitFor(() => expect(document.activeElement?.id).toBe('main-content'));
  30. });
  31. it('provides an explicit mobile navigation escape action', async () => {
  32. const user = userEvent.setup();
  33. renderShell('/overview', { role: 'user' });
  34. await user.click(screen.getByRole('button', { name: '打开主导航' }));
  35. const close = await screen.findByRole('button', { name: '关闭主导航' });
  36. expect(close.style.minHeight).toBe('44px');
  37. expect(close.style.minWidth).toBe('44px');
  38. await user.click(close);
  39. await waitFor(() => expect(screen.queryByRole('button', { name: '关闭主导航' })).toBeNull());
  40. });
  41. it('keeps mobile navigation available between Ant Design lg and the 1024px product breakpoint', async () => {
  42. vi.spyOn(window, 'matchMedia').mockImplementation((query) => mediaQueryList(query === '(min-width: 992px)', query));
  43. const user = userEvent.setup();
  44. renderShell('/overview', { role: 'user' });
  45. await user.click(screen.getByRole('button', { name: '打开主导航' }));
  46. expect(await screen.findByRole('button', { name: '关闭主导航' })).toBeTruthy();
  47. });
  48. });
  49. function mediaQueryList(matches: boolean, media: string): MediaQueryList {
  50. return {
  51. matches,
  52. media,
  53. onchange: null,
  54. addListener: () => undefined,
  55. removeListener: () => undefined,
  56. addEventListener: () => undefined,
  57. removeEventListener: () => undefined,
  58. dispatchEvent: () => false
  59. };
  60. }
  61. function renderShell(path: string, { role }: { role: 'admin' | 'user' }) {
  62. const context: AppContextValue = {
  63. user: {
  64. id: 1,
  65. username: 'operator',
  66. email: 'operator@example.test',
  67. role,
  68. status: 'active'
  69. },
  70. config: {
  71. appBaseUrl: 'https://mail.example.test',
  72. mailHostname: 'mail.example.test',
  73. sendingIp: '192.0.2.10',
  74. defaultSpfMechanisms: '',
  75. dmarcPolicy: 'none',
  76. dmarcRua: '',
  77. sendRequiresVerified: true,
  78. engagementTrackingEnabled: true,
  79. listUnsubscribeMailto: '',
  80. listUnsubscribeUrl: '',
  81. listUnsubscribePostEnabled: false,
  82. feedbackIdEnabled: false,
  83. reportAbuseTo: '',
  84. csaComplaintsTo: '',
  85. bounceAddress: '',
  86. bounceEnvelopeEnabled: false
  87. },
  88. refreshBootstrap: vi.fn(async () => undefined),
  89. logout: vi.fn(async () => undefined)
  90. };
  91. return render(
  92. <ConfigProvider theme={mailhubTheme}>
  93. <AntApp>
  94. <I18nProvider>
  95. <AppContext.Provider value={context}>
  96. <MemoryRouter initialEntries={[path]}>
  97. <Routes>
  98. <Route element={<AdminLayout />}>
  99. <Route path="*" element={<LocationProbe />} />
  100. </Route>
  101. </Routes>
  102. </MemoryRouter>
  103. </AppContext.Provider>
  104. </I18nProvider>
  105. </AntApp>
  106. </ConfigProvider>
  107. );
  108. }
  109. function LocationProbe() {
  110. const location = useLocation();
  111. return <div data-testid="location">{location.pathname}</div>;
  112. }