|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 3 | +import { signIn, useSession } from 'next-auth/react'; |
| 4 | +import Login from '../src/app/login/page.js'; |
| 5 | +import Spinner from '../src/app/components/Spinner.js'; |
| 6 | +import React from 'react'; |
| 7 | + |
| 8 | +// Mock the necessary components and functions |
| 9 | +jest.mock('next-auth/react'); |
| 10 | +jest.mock('../src/app/components/Spinner.js', () => () => <div>Loading...</div>); |
| 11 | +jest.mock('next/image', () => (props) => <img {...props} />); |
| 12 | + |
| 13 | +describe('Login', () => { |
| 14 | + const mockSignIn = jest.fn(); |
| 15 | + const mockUseSession = useSession; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + mockUseSession.mockReturnValue({ |
| 19 | + data: null, |
| 20 | + status: 'unauthenticated' |
| 21 | + }); |
| 22 | + signIn.mockImplementation(mockSignIn); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('renders the login form', async () => { |
| 30 | + render(<Login initialLoading={false} />); |
| 31 | + |
| 32 | + await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument()); |
| 33 | + |
| 34 | + expect(screen.getByPlaceholderText('Username')).toBeInTheDocument(); |
| 35 | + expect(screen.getByPlaceholderText('Password')).toBeInTheDocument(); |
| 36 | + expect(screen.getByText('Login')).toBeInTheDocument(); |
| 37 | + expect(screen.getByText("Don't have an account?")).toBeInTheDocument(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('displays error message on login failure', async () => { |
| 41 | + mockSignIn.mockResolvedValueOnce({ error: 'Invalid credentials' }); |
| 42 | + render(<Login initialLoading={false} />); |
| 43 | + |
| 44 | + await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument()); |
| 45 | + |
| 46 | + fireEvent.change(screen.getByPlaceholderText('Username'), { target: { value: 'testuser' } }); |
| 47 | + fireEvent.change(screen.getByPlaceholderText('Password'), { target: { value: 'password' } }); |
| 48 | + fireEvent.click(screen.getByText('Login')); |
| 49 | + |
| 50 | + await waitFor(() => expect(screen.getByText('Invalid credentials')).toBeInTheDocument()); |
| 51 | + }); |
| 52 | + |
| 53 | + it('redirects to dashboard on successful login', async () => { |
| 54 | + // Mock window.location.href |
| 55 | + delete window.location; |
| 56 | + window.location = { href: '' }; |
| 57 | + |
| 58 | + mockSignIn.mockResolvedValueOnce({ error: null }); |
| 59 | + render(<Login initialLoading={false} />); |
| 60 | + |
| 61 | + await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument()); |
| 62 | + |
| 63 | + fireEvent.change(screen.getByPlaceholderText('Username'), { target: { value: 'testuser' } }); |
| 64 | + fireEvent.change(screen.getByPlaceholderText('Password'), { target: { value: 'password' } }); |
| 65 | + fireEvent.click(screen.getByText('Login')); |
| 66 | + |
| 67 | + await waitFor(() => expect(screen.getByText('Loading...')).toBeInTheDocument()); |
| 68 | + |
| 69 | + await waitFor(() => expect(window.location.href).toBe('/dashboard/?username=testuser')); |
| 70 | + }); |
| 71 | + |
| 72 | + it('renders the spinner when loading', () => { |
| 73 | + render(<Login />); |
| 74 | + expect(screen.getByText('Loading...')).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('handles OAuth sign-in error', async () => { |
| 78 | + mockSignIn.mockResolvedValueOnce({ error: 'OAuth error' }); |
| 79 | + render(<Login initialLoading={false} />); |
| 80 | + |
| 81 | + await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument()); |
| 82 | + |
| 83 | + // Changed the selector to use aria-label |
| 84 | + fireEvent.click(screen.getByRole('button', { name: 'Sign in with Google' })); |
| 85 | + |
| 86 | + await waitFor(() => expect(screen.getByText('Failed to sign in with google. Please try again.')).toBeInTheDocument()); |
| 87 | + }); |
| 88 | + |
| 89 | + it('redirects to dashboard on successful OAuth sign-in', async () => { |
| 90 | + // Mock window.location.href |
| 91 | + delete window.location; |
| 92 | + window.location = { href: '' }; |
| 93 | + |
| 94 | + mockUseSession.mockReturnValue({ |
| 95 | + data: { user: { email: 'testuser' } }, |
| 96 | + status: 'authenticated', |
| 97 | + }); |
| 98 | + |
| 99 | + render(<Login initialLoading={false} />); |
| 100 | + |
| 101 | + await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument()); |
| 102 | + |
| 103 | + // Changed the selector to use aria-label |
| 104 | + fireEvent.click(screen.getByRole('button', { name: 'Sign in with Google' })); |
| 105 | + |
| 106 | + await waitFor(() => expect(screen.getByText('Loading...')).toBeInTheDocument()); |
| 107 | + |
| 108 | + await waitFor(() => expect(window.location.href).toBe('/dashboard/?username=testuser')); |
| 109 | + }); |
| 110 | + |
| 111 | + it('applies correct styles on mount and cleans up on unmount', () => { |
| 112 | + const { unmount } = render(<Login initialLoading={false} />); |
| 113 | + const bodyStyles = document.body.style; |
| 114 | + |
| 115 | + expect(bodyStyles.fontFamily).toBe("'Poppins', sans-serif"); |
| 116 | + expect(bodyStyles.display).toBe('flex'); |
| 117 | + expect(bodyStyles.justifyContent).toBe('center'); |
| 118 | + expect(bodyStyles.alignItems).toBe('center'); |
| 119 | + expect(bodyStyles.minHeight).toBe('100vh'); |
| 120 | + expect(bodyStyles.backgroundImage).toContain('https://getwallpapers.com/wallpaper/full/2/8/f/537844.jpg'); |
| 121 | + expect(bodyStyles.backgroundRepeat).toBe('no-repeat'); |
| 122 | + expect(bodyStyles.backgroundSize).toBe('cover'); |
| 123 | + expect(bodyStyles.backgroundPosition).toBe('center'); |
| 124 | + expect(bodyStyles.color).toBe('rgb(255, 255, 255)'); |
| 125 | + |
| 126 | + unmount(); |
| 127 | + |
| 128 | + expect(bodyStyles.fontFamily).toBe(''); |
| 129 | + expect(bodyStyles.display).toBe(''); |
| 130 | + expect(bodyStyles.justifyContent).toBe(''); |
| 131 | + expect(bodyStyles.alignItems).toBe(''); |
| 132 | + expect(bodyStyles.minHeight).toBe(''); |
| 133 | + expect(bodyStyles.backgroundImage).toBe(''); |
| 134 | + expect(bodyStyles.backgroundRepeat).toBe('no-repeat'); // Changed to check if 'no-repeat' is retained |
| 135 | + expect(bodyStyles.backgroundSize).toBe('auto'); |
| 136 | + expect(bodyStyles.backgroundPosition).toBe('0% 0%'); |
| 137 | + expect(bodyStyles.color).toBe(''); |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
0 commit comments