|
| 1 | +import React from 'react'; |
| 2 | +import thunk from 'redux-thunk'; |
| 3 | +import configureStore from 'redux-mock-store'; |
| 4 | +import { screen, fireEvent, act } from '@testing-library/react'; |
| 5 | + |
| 6 | +import { LoginView } from './LoginView'; |
| 7 | +import { reduxRender, history } from '../../../test-utils'; |
| 8 | +import { initialTestState } from '../../../testData/testReduxStore'; |
| 9 | +import * as actions from '../actions'; |
| 10 | + |
| 11 | +const mockStore = configureStore([thunk]); |
| 12 | + |
| 13 | +jest.mock('../actions', () => ({ |
| 14 | + ...jest.requireActual('../actions'), |
| 15 | + validateAndLoginUser: jest.fn().mockReturnValue( |
| 16 | + (dispatch) => |
| 17 | + new Promise((resolve) => { |
| 18 | + dispatch({ type: 'AUTH_USER', payload: {} }); |
| 19 | + dispatch({ type: 'SET_PREFERENCES', payload: {} }); |
| 20 | + resolve(); |
| 21 | + }) |
| 22 | + ) |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock('../../../common/useSyncFormTranslations', () => ({ |
| 26 | + useSyncFormTranslations: jest.fn() |
| 27 | +})); |
| 28 | + |
| 29 | +describe('<LoginView />', () => { |
| 30 | + let store; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + store = mockStore(initialTestState); |
| 34 | + jest.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + const renderComponent = () => reduxRender(<LoginView />, { store }); |
| 38 | + |
| 39 | + it('renders page heading, login form, social auth buttons, and navigation links', () => { |
| 40 | + renderComponent(); |
| 41 | + |
| 42 | + // Verify main page title and divider |
| 43 | + expect( |
| 44 | + screen.getByRole('heading', { name: /log in/i, level: 2 }) |
| 45 | + ).toBeInTheDocument(); |
| 46 | + expect( |
| 47 | + screen.getByRole('heading', { name: /^or$/i, level: 2 }) |
| 48 | + ).toBeInTheDocument(); |
| 49 | + |
| 50 | + // Verify embedded LoginForm fields |
| 51 | + expect( |
| 52 | + screen.getByRole('textbox', { name: /email or username/i }) |
| 53 | + ).toBeInTheDocument(); |
| 54 | + expect(screen.getByLabelText(/^password$/i)).toBeInTheDocument(); |
| 55 | + expect(screen.getByRole('button', { name: /log in/i })).toBeInTheDocument(); |
| 56 | + |
| 57 | + // Verify social authentication buttons |
| 58 | + const githubButton = screen.getByRole('link', { name: /github/i }); |
| 59 | + expect(githubButton).toBeInTheDocument(); |
| 60 | + expect(githubButton).toHaveAttribute('href', '/auth/github'); |
| 61 | + |
| 62 | + const googleButton = screen.getByRole('link', { name: /google/i }); |
| 63 | + expect(googleButton).toBeInTheDocument(); |
| 64 | + expect(googleButton).toHaveAttribute('href', '/auth/google'); |
| 65 | + |
| 66 | + // Verify navigation links for signup and reset-password |
| 67 | + const signupLink = screen.getByRole('link', { name: /sign up/i }); |
| 68 | + expect(signupLink).toBeInTheDocument(); |
| 69 | + expect(signupLink).toHaveAttribute('href', '/signup'); |
| 70 | + |
| 71 | + const resetPasswordLink = screen.getByRole('link', { |
| 72 | + name: /reset your password/i |
| 73 | + }); |
| 74 | + expect(resetPasswordLink).toBeInTheDocument(); |
| 75 | + expect(resetPasswordLink).toHaveAttribute('href', '/reset-password'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('navigates to the signup page when the sign up link is clicked', () => { |
| 79 | + renderComponent(); |
| 80 | + |
| 81 | + const signupLink = screen.getByRole('link', { name: /sign up/i }); |
| 82 | + fireEvent.click(signupLink); |
| 83 | + |
| 84 | + expect(history.location.pathname).toBe('/signup'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('navigates to the reset password page when the reset password link is clicked', () => { |
| 88 | + renderComponent(); |
| 89 | + |
| 90 | + const resetPasswordLink = screen.getByRole('link', { |
| 91 | + name: /reset your password/i |
| 92 | + }); |
| 93 | + fireEvent.click(resetPasswordLink); |
| 94 | + |
| 95 | + expect(history.location.pathname).toBe('/reset-password'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('integrates with LoginForm and submits user credentials', async () => { |
| 99 | + renderComponent(); |
| 100 | + |
| 101 | + const emailInput = screen.getByRole('textbox', { |
| 102 | + name: /email or username/i |
| 103 | + }); |
| 104 | + fireEvent.change(emailInput, { |
| 105 | + target: { value: 'testuser@example.com' } |
| 106 | + }); |
| 107 | + |
| 108 | + const passwordInput = screen.getByLabelText(/^password$/i); |
| 109 | + fireEvent.change(passwordInput, { |
| 110 | + target: { value: 'password123' } |
| 111 | + }); |
| 112 | + |
| 113 | + const submitButton = screen.getByRole('button', { name: /log in/i }); |
| 114 | + await act(async () => { |
| 115 | + fireEvent.click(submitButton); |
| 116 | + }); |
| 117 | + |
| 118 | + expect(actions.validateAndLoginUser).toHaveBeenCalledWith({ |
| 119 | + email: 'testuser@example.com', |
| 120 | + password: 'password123' |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments