|
| 1 | +import { createTeamFixture, createTeamMemberFixture } from '@/test/mocks/teams.fixtures' |
| 2 | +import { cleanup, render, screen } from '@testing-library/react' |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +import { TEAM_ROLES } from '@repo/types' |
| 6 | + |
| 7 | +import { TeamSettingsPageView } from '@/views/teams/ui/team-settings-page-view' |
| 8 | + |
| 9 | +// ─── Мок: useParams ────────────────────────────────────────────── |
| 10 | +vi.mock('next/navigation', () => ({ |
| 11 | + useParams: () => ({ id: 'team-1' }), |
| 12 | +})) |
| 13 | + |
| 14 | +// ─── Мок: useTeamDetail ────────────────────────────────────────── |
| 15 | +const mockUseTeamDetail = vi.fn() |
| 16 | + |
| 17 | +vi.mock('@/shared/api/use-teams', () => ({ |
| 18 | + useTeamDetail: () => mockUseTeamDetail(), |
| 19 | +})) |
| 20 | + |
| 21 | +// ─── Мок: UI-компоненты ────────────────────────────────────────── |
| 22 | +vi.mock('@repo/ui', () => ({ |
| 23 | + Avatar: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 24 | + AvatarFallback: ({ children }: React.PropsWithChildren) => <span>{children}</span>, |
| 25 | + Badge: ({ |
| 26 | + children, |
| 27 | + }: React.PropsWithChildren<{ variant?: string; className?: string }>) => ( |
| 28 | + <span>{children}</span> |
| 29 | + ), |
| 30 | + Button: ({ |
| 31 | + children, |
| 32 | + ...props |
| 33 | + }: React.PropsWithChildren<React.ButtonHTMLAttributes<HTMLButtonElement>>) => ( |
| 34 | + <button {...props}>{children}</button> |
| 35 | + ), |
| 36 | + cn: (...args: unknown[]) => args.filter(Boolean).join(' '), |
| 37 | + ConfirmDialog: ({ |
| 38 | + open, |
| 39 | + title, |
| 40 | + onConfirm, |
| 41 | + onOpenChange, |
| 42 | + }: { |
| 43 | + open: boolean |
| 44 | + title?: string |
| 45 | + onConfirm: () => void |
| 46 | + onOpenChange: (v: boolean) => void |
| 47 | + description?: string |
| 48 | + confirmLabel?: string |
| 49 | + pendingLabel?: string |
| 50 | + }) => |
| 51 | + open ? ( |
| 52 | + <div data-testid='confirm-dialog'> |
| 53 | + <p>{title}</p> |
| 54 | + <button onClick={onConfirm}>Подтвердить</button> |
| 55 | + <button onClick={() => onOpenChange(false)}>Закрыть</button> |
| 56 | + </div> |
| 57 | + ) : null, |
| 58 | + DialogDrawer: ({ |
| 59 | + children, |
| 60 | + }: React.PropsWithChildren<{ open: boolean; onOpenChange: (v: boolean) => void }>) => ( |
| 61 | + <div>{children}</div> |
| 62 | + ), |
| 63 | + DialogDrawerContent: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 64 | + DialogDrawerDescription: ({ children }: React.PropsWithChildren) => <p>{children}</p>, |
| 65 | + DialogDrawerFooter: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 66 | + DialogDrawerHeader: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 67 | + DialogDrawerTitle: ({ children }: React.PropsWithChildren) => <h2>{children}</h2>, |
| 68 | + EmptyState: ({ |
| 69 | + title, |
| 70 | + action, |
| 71 | + }: { |
| 72 | + title: string |
| 73 | + description?: string |
| 74 | + action?: React.ReactNode |
| 75 | + icon?: React.ReactNode |
| 76 | + className?: string |
| 77 | + }) => ( |
| 78 | + <div data-testid='empty-state'> |
| 79 | + <p>{title}</p> |
| 80 | + {action} |
| 81 | + </div> |
| 82 | + ), |
| 83 | + Input: (props: React.InputHTMLAttributes<HTMLInputElement>) => <input {...props} />, |
| 84 | + Label: ({ |
| 85 | + children, |
| 86 | + ...props |
| 87 | + }: React.PropsWithChildren<React.LabelHTMLAttributes<HTMLLabelElement>>) => ( |
| 88 | + <label {...props}>{children}</label> |
| 89 | + ), |
| 90 | + Select: ({ |
| 91 | + children, |
| 92 | + }: { |
| 93 | + children: React.ReactNode |
| 94 | + value?: string |
| 95 | + onValueChange?: (v: string) => void |
| 96 | + }) => <div>{children}</div>, |
| 97 | + SelectContent: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 98 | + SelectItem: ({ children, value }: React.PropsWithChildren<{ value: string }>) => ( |
| 99 | + <option value={value}>{children}</option> |
| 100 | + ), |
| 101 | + SelectTrigger: ({ |
| 102 | + children, |
| 103 | + }: React.PropsWithChildren<{ className?: string; id?: string }>) => ( |
| 104 | + <div>{children}</div> |
| 105 | + ), |
| 106 | + SelectValue: ({ placeholder }: { placeholder?: string }) => <span>{placeholder}</span>, |
| 107 | + VStack: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 108 | +})) |
| 109 | + |
| 110 | +vi.mock('@repo/ui/icons', () => ({ |
| 111 | + Crown: () => <span />, |
| 112 | + Mail: () => <span />, |
| 113 | + Settings2: () => <span />, |
| 114 | + Shield: () => <span />, |
| 115 | + Trash2: () => <span />, |
| 116 | + UserPlus: () => <span />, |
| 117 | + Users: () => <span />, |
| 118 | +})) |
| 119 | + |
| 120 | +describe('TeamSettingsPageView', () => { |
| 121 | + beforeEach(() => { |
| 122 | + vi.clearAllMocks() |
| 123 | + }) |
| 124 | + |
| 125 | + afterEach(cleanup) |
| 126 | + |
| 127 | + it('loading — показывает "Загрузка участников..."', () => { |
| 128 | + mockUseTeamDetail.mockReturnValue({ |
| 129 | + data: undefined, |
| 130 | + isPending: true, |
| 131 | + isError: false, |
| 132 | + refetch: vi.fn(), |
| 133 | + }) |
| 134 | + |
| 135 | + render(<TeamSettingsPageView />) |
| 136 | + |
| 137 | + expect(screen.getByText('Загрузка участников...')).toBeDefined() |
| 138 | + }) |
| 139 | + |
| 140 | + it('error — показывает empty state "Не удалось загрузить команду"', () => { |
| 141 | + mockUseTeamDetail.mockReturnValue({ |
| 142 | + data: undefined, |
| 143 | + isPending: false, |
| 144 | + isError: true, |
| 145 | + refetch: vi.fn(), |
| 146 | + }) |
| 147 | + |
| 148 | + render(<TeamSettingsPageView />) |
| 149 | + |
| 150 | + expect(screen.getByTestId('empty-state')).toBeDefined() |
| 151 | + expect(screen.getByText('Не удалось загрузить команду')).toBeDefined() |
| 152 | + }) |
| 153 | + |
| 154 | + it('список участников — OWNER не имеет кнопки удаления, MEMBER — имеет', () => { |
| 155 | + mockUseTeamDetail.mockReturnValue({ |
| 156 | + data: createTeamFixture({ |
| 157 | + members: [ |
| 158 | + createTeamMemberFixture({ |
| 159 | + id: 'user-owner', |
| 160 | + name: 'Alice Owner', |
| 161 | + email: 'alice@example.com', |
| 162 | + role: TEAM_ROLES.OWNER, |
| 163 | + }), |
| 164 | + createTeamMemberFixture({ |
| 165 | + id: 'user-member', |
| 166 | + name: 'Bob Member', |
| 167 | + email: 'bob@example.com', |
| 168 | + role: TEAM_ROLES.MEMBER, |
| 169 | + }), |
| 170 | + ], |
| 171 | + }), |
| 172 | + isPending: false, |
| 173 | + isError: false, |
| 174 | + refetch: vi.fn(), |
| 175 | + }) |
| 176 | + |
| 177 | + render(<TeamSettingsPageView />) |
| 178 | + |
| 179 | + expect(screen.getByText('Alice Owner')).toBeDefined() |
| 180 | + expect(screen.getByText('Bob Member')).toBeDefined() |
| 181 | + expect(screen.queryByRole('button', { name: 'Удалить Alice Owner' })).toBeNull() |
| 182 | + expect(screen.getByRole('button', { name: 'Удалить Bob Member' })).toBeDefined() |
| 183 | + }) |
| 184 | +}) |
0 commit comments