|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, type ComponentProps } from 'react' |
| 5 | +import { Users } from '@sim/emcn/icons' |
| 6 | +import { createRoot, type Root } from 'react-dom/client' |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | + |
| 9 | +const { mockPush, mockReplace, mockNavigate } = vi.hoisted(() => ({ |
| 10 | + mockPush: vi.fn(), |
| 11 | + mockReplace: vi.fn(), |
| 12 | + mockNavigate: vi.fn(), |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('next/navigation', () => ({ |
| 16 | + usePathname: () => '/o/org-a/settings/members', |
| 17 | + useRouter: () => ({ push: mockPush, replace: mockReplace }), |
| 18 | +})) |
| 19 | +vi.mock('@/app/(landing)/components/navbar/components', () => ({ SimWordmark: () => null })) |
| 20 | +vi.mock('@/components/settings/settings-intent-link', () => ({ |
| 21 | + SettingsIntentLink: ({ |
| 22 | + onNavigate, |
| 23 | + replace: _replace, |
| 24 | + scroll: _scroll, |
| 25 | + ...props |
| 26 | + }: ComponentProps<'a'> & { |
| 27 | + replace?: boolean |
| 28 | + scroll?: boolean |
| 29 | + onNavigate?: (event: { preventDefault: () => void }) => void |
| 30 | + }) => ( |
| 31 | + <a |
| 32 | + {...props} |
| 33 | + href={props.href} |
| 34 | + onClick={(event) => { |
| 35 | + event.preventDefault() |
| 36 | + let prevented = false |
| 37 | + onNavigate?.({ |
| 38 | + preventDefault: () => { |
| 39 | + prevented = true |
| 40 | + }, |
| 41 | + }) |
| 42 | + if (!prevented) mockNavigate(props.href) |
| 43 | + }} |
| 44 | + /> |
| 45 | + ), |
| 46 | +})) |
| 47 | + |
| 48 | +import { SettingsSidebar } from '@/components/settings/settings-sidebar' |
| 49 | +import { useSettingsDirtyStore } from '@/stores/settings/dirty/store' |
| 50 | + |
| 51 | +let root: Root |
| 52 | +let container: HTMLDivElement |
| 53 | + |
| 54 | +beforeEach(() => { |
| 55 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 56 | + vi.clearAllMocks() |
| 57 | + useSettingsDirtyStore.getState().reset() |
| 58 | + container = document.createElement('div') |
| 59 | + document.body.appendChild(container) |
| 60 | + root = createRoot(container) |
| 61 | +}) |
| 62 | + |
| 63 | +afterEach(() => { |
| 64 | + act(() => root.unmount()) |
| 65 | + container.remove() |
| 66 | + useSettingsDirtyStore.getState().reset() |
| 67 | +}) |
| 68 | + |
| 69 | +function renderSidebar(isCollapsed = false) { |
| 70 | + act(() => |
| 71 | + root.render( |
| 72 | + <SettingsSidebar |
| 73 | + plane='organization' |
| 74 | + activeSection='members' |
| 75 | + groups={[{ key: 'organization', title: 'Organization' }]} |
| 76 | + items={[ |
| 77 | + { id: 'members', label: 'Members', group: 'organization', icon: Users }, |
| 78 | + { id: 'search-mcp', label: 'Search MCP', group: 'organization', icon: Users }, |
| 79 | + ]} |
| 80 | + hrefForSection={(section) => `/o/org-a/settings/${section}`} |
| 81 | + backHref='/o/org-a/home' |
| 82 | + isCollapsed={isCollapsed} |
| 83 | + /> |
| 84 | + ) |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +function button(label: string): HTMLButtonElement { |
| 89 | + const element = [...document.querySelectorAll('button')].find( |
| 90 | + (candidate) => candidate.textContent?.trim() === label |
| 91 | + ) |
| 92 | + if (!element) throw new Error(`Missing button: ${label}`) |
| 93 | + return element |
| 94 | +} |
| 95 | + |
| 96 | +describe('SettingsSidebar interactions', () => { |
| 97 | + it('keeps destinations available in the icon rail after collapsing a section', () => { |
| 98 | + renderSidebar() |
| 99 | + act(() => button('Organization').click()) |
| 100 | + expect(container.querySelector('a')).toBeNull() |
| 101 | + |
| 102 | + renderSidebar(true) |
| 103 | + expect(container.querySelectorAll('a')).toHaveLength(2) |
| 104 | + |
| 105 | + renderSidebar() |
| 106 | + expect(button('Organization')).toHaveAttribute('aria-expanded', 'false') |
| 107 | + act(() => button('Organization').click()) |
| 108 | + expect(container.querySelectorAll('a')).toHaveLength(2) |
| 109 | + }) |
| 110 | + |
| 111 | + it('preserves dirty settings when Back is cancelled, then leaves only after confirmation', () => { |
| 112 | + renderSidebar() |
| 113 | + act(() => useSettingsDirtyStore.getState().setDirty(true)) |
| 114 | + act(() => button('Back').click()) |
| 115 | + expect(mockPush).not.toHaveBeenCalled() |
| 116 | + act(() => button('Keep editing').click()) |
| 117 | + expect(useSettingsDirtyStore.getState().isDirty).toBe(true) |
| 118 | + expect(mockPush).not.toHaveBeenCalled() |
| 119 | + |
| 120 | + act(() => button('Back').click()) |
| 121 | + act(() => button('Discard changes').click()) |
| 122 | + expect(mockPush).toHaveBeenCalledWith('/o/org-a/home') |
| 123 | + }) |
| 124 | + |
| 125 | + it('blocks section navigation during a save even when the draft is already clean', () => { |
| 126 | + renderSidebar() |
| 127 | + act(() => useSettingsDirtyStore.getState().setNavigationBlocked(true)) |
| 128 | + act(() => container.querySelector<HTMLAnchorElement>('a[href$="search-mcp"]')?.click()) |
| 129 | + expect(mockNavigate).not.toHaveBeenCalled() |
| 130 | + expect(mockReplace).not.toHaveBeenCalled() |
| 131 | + expect(useSettingsDirtyStore.getState().pendingLeave).toBeNull() |
| 132 | + |
| 133 | + act(() => useSettingsDirtyStore.getState().setNavigationBlocked(false)) |
| 134 | + act(() => container.querySelector<HTMLAnchorElement>('a[href$="search-mcp"]')?.click()) |
| 135 | + expect(mockNavigate).toHaveBeenCalledWith('/o/org-a/settings/search-mcp') |
| 136 | + }) |
| 137 | +}) |
0 commit comments