File size: 2,726 Bytes
dce7eca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'
import Home from '@/app/page'

// Mock dependencies
jest.mock('next-auth/react', () => ({
    useSession: jest.fn(() => ({ data: null })),
    signIn: jest.fn(),
    signOut: jest.fn(),
}))

jest.mock('next/dynamic', () => () => {
    const DynamicComponent = () => <div>PanoramaViewer Mock</div>
    DynamicComponent.displayName = 'PanoramaViewer'
    return DynamicComponent
})

// Mock child components
jest.mock('@/components/Navbar', () => () => <div data-testid="navbar">Navbar</div>)
jest.mock('@/components/UploadSection', () => ({ onGenerate, isGenerating }: any) => (
    <div data-testid="upload-section">
        <button onClick={() => onGenerate('test prompt', [])} disabled={isGenerating}>Generate</button>
    </div>
))

describe('Home Page', () => {
    beforeEach(() => {
        localStorage.clear()
        jest.clearAllMocks()
        jest.useFakeTimers()
    })

    afterEach(() => {
        jest.useRealTimers()
    })

    it('renders initial state correctly', () => {
        render(<Home />)
        expect(screen.getByTestId('navbar')).toBeInTheDocument()
        expect(screen.getByTestId('upload-section')).toBeInTheDocument()
        expect(screen.getByText('No panoramas yet')).toBeInTheDocument()
        expect(screen.getByText('Your Gallery')).toBeInTheDocument()
        expect(screen.getByText('0 Creations')).toBeInTheDocument()
    })

    it('generates panorama successfully', async () => {
        // Mock fetch success
        (global.fetch as jest.Mock).mockResolvedValueOnce({
            ok: true,
            json: async () => ({
                success: true,
                url: 'data:image/png;base64,mock',
                method: 'cv_ai_hybrid'
            })
        });

        render(<Home />)
        const generateBtn = screen.getByText('Generate')

        await act(async () => {
            fireEvent.click(generateBtn)
        })

        await waitFor(() => {
            expect(screen.getByText('PanoramaViewer Mock')).toBeInTheDocument()
        })

        expect(screen.getByText('1 Creations')).toBeInTheDocument()
    })

    it('loads history from localStorage', async () => {
        const mockHistory = [{
            id: 'test-id',
            url: '/mock-local-url.jpg',
            prompt: 'test prompt',
            timestamp: new Date().toISOString()
        }]

        Storage.prototype.getItem = jest.fn(() => JSON.stringify(mockHistory));

        render(<Home />)

        await waitFor(() => {
            expect(screen.getByText('1 Creations')).toBeInTheDocument()
        })
        expect(screen.getByText('PanoramaViewer Mock')).toBeInTheDocument()
    })
})