File size: 1,883 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
import { render, screen, fireEvent } from '@testing-library/react'
import Navbar from '@/components/Navbar'
import { useSession, signIn, signOut } from 'next-auth/react'

// Mocks are already setup in global or hoisted.
// But we need to control useSession specifically here.
// In jest.setup.js? No, I mocked it globally?
// No, I mocked it in Navbar.test.tsx previously.
// But jest resets mocks.
// So I mock it here again.

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

describe('Navbar', () => {

    beforeEach(() => {
        jest.clearAllMocks()
    })

    it('renders logo and sign in button when logged out', () => {
        (useSession as jest.Mock).mockReturnValue({ data: null, status: 'unauthenticated' })

        render(<Navbar />)

        expect(screen.getByText('PanoAI')).toBeInTheDocument()
        const signInBtn = screen.getByText('Sign In')
        expect(signInBtn).toBeInTheDocument()

        fireEvent.click(signInBtn)
        expect(signIn).toHaveBeenCalledWith('google')
    })

    it('renders user info and sign out button when logged in', () => {
        const mockUser = { name: 'Test User', email: 'test@example.com', image: 'test.jpg' };
        (useSession as jest.Mock).mockReturnValue({
            data: { user: mockUser },
            status: 'authenticated'
        })

        render(<Navbar />)

        expect(screen.getByText('Test User')).toBeInTheDocument()
        expect(screen.getByText('test@example.com')).toBeInTheDocument()

        // Sign out button is an icon button. LogOut icon mocked as icon-LogOut
        // Find by icon testid
        const signOutBtn = screen.getByTestId('icon-LogOut').closest('button')

        fireEvent.click(signOutBtn!)
        expect(signOut).toHaveBeenCalled()
    })
})