me.test.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { render, screen } from '@testing-library/react'
  2. import type { ReactNode } from 'react'
  3. import { beforeEach, describe, expect, it, vi } from 'vitest'
  4. import PreferencesPage from '@/pages/account/me'
  5. const { mockIsPlatform, mockUseIsFeatureEnabled, mockUseProfile } = vi.hoisted(() => ({
  6. mockIsPlatform: { value: true },
  7. mockUseIsFeatureEnabled: vi.fn(),
  8. mockUseProfile: vi.fn(),
  9. }))
  10. vi.mock('@/lib/constants', async () => {
  11. const actual = await vi.importActual<Record<string, unknown>>('@/lib/constants')
  12. return {
  13. ...actual,
  14. get IS_PLATFORM() {
  15. return mockIsPlatform.value
  16. },
  17. }
  18. })
  19. vi.mock('@/hooks/misc/useIsFeatureEnabled', () => ({
  20. useIsFeatureEnabled: mockUseIsFeatureEnabled,
  21. }))
  22. vi.mock('@/lib/profile', () => ({
  23. useProfile: mockUseProfile,
  24. }))
  25. vi.mock('@/components/layouts/AccountLayout/AccountLayout', () => ({
  26. __esModule: true,
  27. default: ({ children }: { children: ReactNode }) => <div>{children}</div>,
  28. }))
  29. vi.mock('@/components/layouts/AppLayout/AppLayout', () => ({
  30. AppLayout: ({ children }: { children: ReactNode }) => <div>{children}</div>,
  31. }))
  32. vi.mock('@/components/layouts/DefaultLayout', () => ({
  33. DefaultLayout: ({ children }: { children: ReactNode }) => <div>{children}</div>,
  34. }))
  35. vi.mock('@/components/interfaces/Account/Preferences/ProfileInformation', () => ({
  36. ProfileInformation: () => <div>ProfileInformation</div>,
  37. }))
  38. vi.mock('@/components/interfaces/Account/Preferences/AccountIdentities', () => ({
  39. AccountIdentities: () => <div>AccountIdentities</div>,
  40. }))
  41. vi.mock('@/components/interfaces/Account/Preferences/AccountConnections', () => ({
  42. AccountConnections: () => <div>AccountConnections</div>,
  43. }))
  44. vi.mock('@/components/interfaces/Account/Preferences/ThemeSettings', () => ({
  45. ThemeSettings: () => <div>ThemeSettings</div>,
  46. }))
  47. vi.mock('@/components/interfaces/Account/Preferences/TimezoneSettings', () => ({
  48. TimezoneSettings: () => <div>TimezoneSettings</div>,
  49. }))
  50. vi.mock('@/components/interfaces/Account/Preferences/HotkeySettings', () => ({
  51. HotkeySettings: () => <div>HotkeySettings</div>,
  52. }))
  53. vi.mock('@/components/interfaces/Account/Preferences/DashboardSettings', () => ({
  54. DashboardSettings: () => <div>DashboardSettings</div>,
  55. }))
  56. vi.mock('@/components/interfaces/Account/Preferences/AnalyticsSettings', () => ({
  57. AnalyticsSettings: () => <div>AnalyticsSettings</div>,
  58. }))
  59. vi.mock('@/components/interfaces/Account/Preferences/AccountDeletion', () => ({
  60. AccountDeletion: () => <div>AccountDeletion</div>,
  61. }))
  62. vi.mock('@/components/ui/AlertError', () => ({
  63. AlertError: () => <div>AlertError</div>,
  64. }))
  65. describe('/account/me', () => {
  66. beforeEach(() => {
  67. mockUseIsFeatureEnabled.mockClear()
  68. mockUseProfile.mockClear()
  69. mockUseIsFeatureEnabled.mockReturnValue({
  70. profileShowInformation: true,
  71. profileShowAnalyticsAndMarketing: true,
  72. profileShowAccountDeletion: true,
  73. })
  74. mockUseProfile.mockReturnValue({
  75. error: null,
  76. isLoading: false,
  77. isError: false,
  78. isSuccess: true,
  79. })
  80. })
  81. it('renders hosted account sections on platform', () => {
  82. mockIsPlatform.value = true
  83. render(<PreferencesPage dehydratedState={{}} />)
  84. expect(screen.getByText('ProfileInformation')).toBeInTheDocument()
  85. expect(screen.getByText('AccountIdentities')).toBeInTheDocument()
  86. expect(screen.getByText('AccountConnections')).toBeInTheDocument()
  87. expect(screen.getByText('ThemeSettings')).toBeInTheDocument()
  88. expect(screen.getByText('TimezoneSettings')).toBeInTheDocument()
  89. expect(screen.getByText('HotkeySettings')).toBeInTheDocument()
  90. expect(screen.getByText('DashboardSettings')).toBeInTheDocument()
  91. expect(screen.getByText('AnalyticsSettings')).toBeInTheDocument()
  92. expect(screen.getByText('AccountDeletion')).toBeInTheDocument()
  93. })
  94. it('renders only local preferences on self-hosted', () => {
  95. mockIsPlatform.value = false
  96. render(<PreferencesPage dehydratedState={{}} />)
  97. expect(screen.getByText('ThemeSettings')).toBeInTheDocument()
  98. expect(screen.getByText('TimezoneSettings')).toBeInTheDocument()
  99. expect(screen.getByText('HotkeySettings')).toBeInTheDocument()
  100. expect(screen.getByText('DashboardSettings')).toBeInTheDocument()
  101. expect(screen.queryByText('ProfileInformation')).not.toBeInTheDocument()
  102. expect(screen.queryByText('AccountIdentities')).not.toBeInTheDocument()
  103. expect(screen.queryByText('AccountConnections')).not.toBeInTheDocument()
  104. expect(screen.queryByText('AnalyticsSettings')).not.toBeInTheDocument()
  105. expect(screen.queryByText('AccountDeletion')).not.toBeInTheDocument()
  106. expect(mockUseProfile).not.toHaveBeenCalled()
  107. })
  108. })