-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into 339-bug-scroll-bar-issue
- Loading branch information
Showing
7 changed files
with
227 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,18 @@ import React from 'react'; | |
import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
import Login from './page'; | ||
|
||
const mockLoginAccount = jest.fn(); | ||
const mockLogin = jest.fn(); | ||
const mockPush = jest.fn(); | ||
const getUser = jest.fn(); | ||
|
||
let emailInput: HTMLInputElement, | ||
passwordInput: HTMLInputElement, | ||
continueButton: HTMLElement; | ||
let continueButton: HTMLElement, | ||
emailInput: HTMLInputElement, | ||
passwordInput: HTMLInputElement; | ||
|
||
const mockUseAuthContext = { | ||
loginAccount: mockLoginAccount, | ||
isSignedIn: false, | ||
getUser, | ||
isSignedIn: false, | ||
login: mockLogin, | ||
}; | ||
|
||
jest.mock('next/navigation', () => ({ | ||
|
@@ -63,7 +63,7 @@ describe('Login', () => { | |
fireEvent.click(continueButton); | ||
|
||
await waitFor(() => { | ||
expect(mockLoginAccount).toHaveBeenCalledWith({ | ||
expect(mockLogin).toHaveBeenCalledWith({ | ||
email: '[email protected]', | ||
password: 'password123', | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
import Register from './page'; | ||
import { registerAccount } from '@/api/apiFunctions'; | ||
import Alert from '@/components/AlertNotification/AlertNotification'; | ||
import { AlertVariants } from '@/components/AlertNotification/Alerts.enum'; | ||
import { toast } from 'react-hot-toast'; | ||
|
||
const mockLoginAccount = jest.fn(); | ||
const mockLogin = jest.fn(); | ||
const mockPush = jest.fn(); | ||
|
||
jest.mock('../../api/apiFunctions', () => ({ | ||
registerAccount: jest.fn(), | ||
})); | ||
|
||
let emailInput: HTMLElement; | ||
let passwordInput: HTMLElement; | ||
jest.mock('react-hot-toast', () => ({ | ||
toast: { | ||
custom: jest.fn(), | ||
}, | ||
})); | ||
|
||
let confirmPasswordInput: HTMLElement; | ||
let continueButton: HTMLElement; | ||
let emailInput: HTMLElement; | ||
let passwordInput: HTMLElement; | ||
|
||
const mockUseAuthContext = { | ||
loginAccount: mockLoginAccount, | ||
isSignedIn: false, | ||
login: mockLogin, | ||
}; | ||
|
||
// Mock the useRouter and useAuthContext hooks | ||
|
@@ -83,23 +92,77 @@ describe('Register', () => { | |
password: 'rawr123', | ||
confirmPassword: 'rawr123', | ||
}); | ||
expect(mockLoginAccount).toHaveBeenCalledWith({ | ||
expect(mockLogin).toHaveBeenCalledWith({ | ||
email: '[email protected]', | ||
password: 'rawr123', | ||
confirmPassword: 'rawr123', | ||
}); | ||
}); | ||
}); | ||
|
||
test('redirects to /weeklyPicks when the button is clicked', async () => { | ||
test('redirects to /league/all when the button is clicked', async () => { | ||
mockUseAuthContext.isSignedIn = true; | ||
|
||
render(<Register />); | ||
|
||
await waitFor(() => { | ||
expect(mockPush).toHaveBeenCalledWith('/weeklyPicks'); | ||
expect(mockPush).toHaveBeenCalledWith('/league/all'); | ||
}); | ||
|
||
mockUseAuthContext.isSignedIn = false; | ||
}); | ||
|
||
test('should show success notification upon successful submission', async () => { | ||
fireEvent.change(emailInput, { target: { value: '[email protected]' } }); | ||
fireEvent.change(passwordInput, { target: { value: 'pw1234' } }); | ||
fireEvent.change(confirmPasswordInput, { target: { value: 'pw1234' } }); | ||
fireEvent.click(continueButton); | ||
|
||
await waitFor(() => { | ||
expect(registerAccount).toHaveBeenCalledWith({ | ||
email: '[email protected]', | ||
password: 'pw1234', | ||
confirmPassword: 'pw1234', | ||
}); | ||
expect(mockLogin).toHaveBeenCalledWith({ | ||
email: '[email protected]', | ||
password: 'pw1234', | ||
confirmPassword: 'pw1234', | ||
}); | ||
expect(mockPush).toHaveBeenCalledWith('/league/all'); | ||
expect(toast.custom).toHaveBeenCalledWith( | ||
<Alert | ||
variant={AlertVariants.Success} | ||
message="You have successfully registered your account." | ||
/>, | ||
); | ||
}); | ||
}); | ||
|
||
test('should show error notification upon submission failing', async () => { | ||
mockLogin.mockImplementationOnce(() => | ||
Promise.reject(new Error('Mock error')), | ||
); | ||
|
||
fireEvent.change(emailInput, { target: { value: '[email protected]' } }); | ||
fireEvent.change(passwordInput, { target: { value: 'pw1234' } }); | ||
fireEvent.change(confirmPasswordInput, { target: { value: 'pw1234' } }); | ||
fireEvent.click(continueButton); | ||
|
||
await waitFor(() => { | ||
expect(registerAccount).toHaveBeenCalledWith({ | ||
email: '[email protected]', | ||
password: 'pw1234', | ||
confirmPassword: 'pw1234', | ||
}); | ||
expect(mockLogin).toHaveBeenCalledWith({ | ||
email: '[email protected]', | ||
password: 'pw1234', | ||
confirmPassword: 'pw1234', | ||
}); | ||
expect(toast.custom).toHaveBeenCalledWith( | ||
<Alert variant={AlertVariants.Error} message="Something went wrong!" />, | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react'; | ||
import { account } from '../api/config'; | ||
import Alert from '../components/AlertNotification/AlertNotification'; | ||
import { AlertVariants } from '../components/AlertNotification/Alerts.enum'; | ||
import { toast } from 'react-hot-toast'; | ||
import { loginAccount } from './AuthHelper'; | ||
import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime'; | ||
|
||
const mockCreateEmailPasswordSession = jest.fn(); | ||
account.createEmailPasswordSession = mockCreateEmailPasswordSession; | ||
|
||
jest.mock('../api/config'); | ||
jest.mock('next/router'); | ||
jest.mock('react-hot-toast', () => ({ | ||
toast: { | ||
custom: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe('AuthContextProvider', () => { | ||
const user = { | ||
email: '[email protected]', | ||
password: 'password1234', | ||
}; | ||
|
||
const router = { | ||
push: jest.fn(), | ||
} as unknown as AppRouterInstance; | ||
|
||
const getUser = jest.fn().mockResolvedValue({ | ||
email: '[email protected]', | ||
password: 'password1234', | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('should show success notification after a successful login', async () => { | ||
mockCreateEmailPasswordSession.mockResolvedValue({}); | ||
|
||
await loginAccount({ user, router, getUser }); | ||
|
||
expect(mockCreateEmailPasswordSession).toHaveBeenCalledWith( | ||
user.email, | ||
user.password, | ||
); | ||
expect(getUser).toHaveBeenCalled(); | ||
expect(router.push).toHaveBeenCalledWith('/league/all'); | ||
expect(toast.custom).toHaveBeenCalledWith( | ||
<Alert | ||
variant={AlertVariants.Success} | ||
message="You've successfully logged in!" | ||
/>, | ||
); | ||
}); | ||
|
||
test('should show error notification after a login attempt fails', async () => { | ||
const mockError = new Error('Test error'); | ||
|
||
mockCreateEmailPasswordSession.mockRejectedValue(mockError); | ||
|
||
const error = await loginAccount({ user, router, getUser }); | ||
|
||
expect(error).toEqual(mockError); | ||
expect(toast.custom).toHaveBeenCalledWith( | ||
<Alert variant={AlertVariants.Error} message="Something went wrong!" />, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.