Barazo default frontend
barazo.forum
1/**
2 * Tests for SetupGuard component.
3 */
4
5import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
6import { render, screen, waitFor } from '@testing-library/react'
7import { http, HttpResponse } from 'msw'
8import { server } from '@/mocks/server'
9import { SetupGuard } from './setup-guard'
10
11const API_URL = ''
12
13const mockReplace = vi.fn()
14let mockPathname = '/'
15
16vi.mock('next/navigation', () => ({
17 useRouter: () => ({
18 push: vi.fn(),
19 replace: mockReplace,
20 back: vi.fn(),
21 }),
22 usePathname: () => mockPathname,
23}))
24
25describe('SetupGuard', () => {
26 beforeEach(() => {
27 mockReplace.mockClear()
28 mockPathname = '/'
29 })
30
31 afterEach(() => {
32 vi.restoreAllMocks()
33 })
34
35 it('redirects to /setup when community is not initialized', async () => {
36 server.use(
37 http.get(`${API_URL}/api/setup/status`, () => {
38 return HttpResponse.json({ initialized: false })
39 })
40 )
41
42 render(
43 <SetupGuard>
44 <p>Child content</p>
45 </SetupGuard>
46 )
47
48 await waitFor(() => {
49 expect(mockReplace).toHaveBeenCalledWith('/setup')
50 })
51 })
52
53 it('renders children when community is initialized', async () => {
54 // Default handler returns initialized: true
55 render(
56 <SetupGuard>
57 <p>Child content</p>
58 </SetupGuard>
59 )
60
61 await waitFor(() => {
62 expect(screen.getByText('Child content')).toBeInTheDocument()
63 })
64 expect(mockReplace).not.toHaveBeenCalled()
65 })
66
67 it('does not redirect when current path is /setup', async () => {
68 mockPathname = '/setup'
69 server.use(
70 http.get(`${API_URL}/api/setup/status`, () => {
71 return HttpResponse.json({ initialized: false })
72 })
73 )
74
75 render(
76 <SetupGuard>
77 <p>Setup page</p>
78 </SetupGuard>
79 )
80
81 await waitFor(() => {
82 expect(screen.getByText('Setup page')).toBeInTheDocument()
83 })
84 expect(mockReplace).not.toHaveBeenCalled()
85 })
86
87 it('does not redirect when current path is /login', async () => {
88 mockPathname = '/login'
89 server.use(
90 http.get(`${API_URL}/api/setup/status`, () => {
91 return HttpResponse.json({ initialized: false })
92 })
93 )
94
95 render(
96 <SetupGuard>
97 <p>Login page</p>
98 </SetupGuard>
99 )
100
101 await waitFor(() => {
102 expect(screen.getByText('Login page')).toBeInTheDocument()
103 })
104 expect(mockReplace).not.toHaveBeenCalled()
105 })
106
107 it('does not redirect when current path starts with /auth/', async () => {
108 mockPathname = '/auth/callback'
109 server.use(
110 http.get(`${API_URL}/api/setup/status`, () => {
111 return HttpResponse.json({ initialized: false })
112 })
113 )
114
115 render(
116 <SetupGuard>
117 <p>Auth callback</p>
118 </SetupGuard>
119 )
120
121 await waitFor(() => {
122 expect(screen.getByText('Auth callback')).toBeInTheDocument()
123 })
124 expect(mockReplace).not.toHaveBeenCalled()
125 })
126
127 it('renders children when status check fails (allows through)', async () => {
128 server.use(
129 http.get(`${API_URL}/api/setup/status`, () => {
130 return HttpResponse.json({ error: 'Server error' }, { status: 500 })
131 })
132 )
133
134 render(
135 <SetupGuard>
136 <p>Child content</p>
137 </SetupGuard>
138 )
139
140 await waitFor(() => {
141 expect(screen.getByText('Child content')).toBeInTheDocument()
142 })
143 expect(mockReplace).not.toHaveBeenCalled()
144 })
145})