Project.test.tsx159 lines · main
1import { render, screen } from '@testing-library/react'
2import type { ReactNode } from 'react'
3import { beforeEach, describe, expect, it, vi } from 'vitest'
4
5import { Project } from './Project'
6
7const { mockUseIsFeatureEnabled, mockUseProjectPauseStatusQuery, mockUseSelectedProjectQuery } =
8 vi.hoisted(() => ({
9 mockUseIsFeatureEnabled: vi.fn(),
10 mockUseProjectPauseStatusQuery: vi.fn(),
11 mockUseSelectedProjectQuery: vi.fn(),
12 }))
13
14vi.mock('next/link', () => ({
15 default: ({ href, children }: { href: string; children: ReactNode }) => (
16 <a href={href}>{children}</a>
17 ),
18}))
19
20vi.mock('ui', () => ({
21 Button: ({
22 children,
23 asChild,
24 type: _type,
25 ...props
26 }: {
27 children: ReactNode
28 asChild?: boolean
29 type?: string
30 }) => (asChild ? <>{children}</> : <button {...props}>{children}</button>),
31 Card: ({ children }: { children: ReactNode }) => <div>{children}</div>,
32 CardContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
33}))
34
35vi.mock('ui-patterns/PageSection', () => ({
36 PageSection: ({ children, id }: { children: ReactNode; id?: string }) => (
37 <section id={id}>{children}</section>
38 ),
39 PageSectionContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
40 PageSectionDescription: ({ children }: { children: ReactNode }) => <p>{children}</p>,
41 PageSectionMeta: ({ children }: { children: ReactNode }) => <div>{children}</div>,
42 PageSectionSummary: ({ children }: { children: ReactNode }) => <div>{children}</div>,
43 PageSectionTitle: ({ children }: { children: ReactNode }) => <h2>{children}</h2>,
44}))
45
46vi.mock('@/components/interfaces/Project/ResumeProjectButton', () => ({
47 ResumeProjectButton: () => <div>ResumeProjectButton</div>,
48}))
49
50vi.mock('./Infrastructure/PauseProjectButton', () => ({
51 default: () => <div>PauseProjectButton</div>,
52}))
53
54vi.mock('./Infrastructure/RestartServerButton', () => ({
55 default: () => <div>RestartServerButton</div>,
56}))
57
58vi.mock('@/data/projects/project-pause-status-query', () => ({
59 useProjectPauseStatusQuery: mockUseProjectPauseStatusQuery,
60}))
61
62vi.mock('@/hooks/misc/useIsFeatureEnabled', () => ({
63 useIsFeatureEnabled: mockUseIsFeatureEnabled,
64}))
65
66vi.mock('@/hooks/misc/useSelectedProject', () => ({
67 useSelectedProjectQuery: mockUseSelectedProjectQuery,
68}))
69
70describe('Project settings availability', () => {
71 beforeEach(() => {
72 vi.clearAllMocks()
73
74 mockUseIsFeatureEnabled.mockReturnValue({
75 projectSettingsRestartProject: true,
76 })
77
78 mockUseProjectPauseStatusQuery.mockReturnValue({
79 data: undefined,
80 isError: false,
81 isSuccess: false,
82 })
83 })
84
85 it('shows the restart action for active projects', () => {
86 mockUseSelectedProjectQuery.mockReturnValue({
87 data: {
88 parent_project_ref: null,
89 ref: 'active-project',
90 status: 'ACTIVE_HEALTHY',
91 },
92 })
93
94 render(<Project />)
95
96 expect(screen.getByText('Restart project')).toBeInTheDocument()
97 expect(screen.getByText('RestartServerButton')).toBeInTheDocument()
98 expect(screen.getByText('Pause project')).toBeInTheDocument()
99 expect(screen.getByText('PauseProjectButton')).toBeInTheDocument()
100 expect(screen.queryByText('ResumeProjectButton')).not.toBeInTheDocument()
101 })
102
103 it('shows the shared resume action for paused projects that can still be restored', () => {
104 mockUseSelectedProjectQuery.mockReturnValue({
105 data: {
106 parent_project_ref: null,
107 ref: 'paused-project',
108 status: 'INACTIVE',
109 },
110 })
111
112 mockUseProjectPauseStatusQuery.mockReturnValue({
113 data: { can_restore: true },
114 isError: false,
115 isSuccess: true,
116 })
117
118 render(<Project />)
119
120 expect(screen.getByText('Resume project')).toBeInTheDocument()
121 expect(screen.getByText('Bring your paused project back online.')).toBeInTheDocument()
122 expect(screen.getByText('ResumeProjectButton')).toBeInTheDocument()
123 expect(screen.queryByText('Pause project')).not.toBeInTheDocument()
124 expect(screen.queryByText('PauseProjectButton')).not.toBeInTheDocument()
125 expect(screen.queryByText('RestartServerButton')).not.toBeInTheDocument()
126 })
127
128 it('links back to the dashboard when the paused project can no longer be restored', () => {
129 mockUseSelectedProjectQuery.mockReturnValue({
130 data: {
131 parent_project_ref: null,
132 ref: 'paused-project',
133 status: 'INACTIVE',
134 },
135 })
136
137 mockUseProjectPauseStatusQuery.mockReturnValue({
138 data: { can_restore: false },
139 isError: false,
140 isSuccess: true,
141 })
142
143 render(<Project />)
144
145 expect(screen.getAllByText('View project dashboard')).toHaveLength(2)
146 expect(
147 screen.getByText(
148 'This project can no longer be resumed here. Open the dashboard to download backups and view recovery options.'
149 )
150 ).toBeInTheDocument()
151 expect(screen.getByRole('link', { name: 'View project dashboard' })).toHaveAttribute(
152 'href',
153 '/project/paused-project'
154 )
155 expect(screen.queryByText('Pause project')).not.toBeInTheDocument()
156 expect(screen.queryByText('PauseProjectButton')).not.toBeInTheDocument()
157 expect(screen.queryByText('ResumeProjectButton')).not.toBeInTheDocument()
158 })
159})