-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update verification timeout, enhance MainCard layout, and add A…
…uthError tests
- Loading branch information
1 parent
73b3692
commit baf4a0c
Showing
12 changed files
with
233 additions
and
46 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,50 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { useSearchParams } from 'next/navigation' | ||
import { Suspense } from 'react' | ||
import AuthError from '../page' | ||
|
||
// Mock useSearchParams from next/navigation | ||
jest.mock('next/navigation', () => ({ | ||
useSearchParams: jest.fn(), | ||
})) | ||
|
||
describe('AuthError Component', () => { | ||
it('renders with a default error message when no message is provided', () => { | ||
(useSearchParams as jest.Mock).mockReturnValue({ | ||
get: jest.fn().mockReturnValue(null), | ||
}) | ||
|
||
render(<AuthError />) | ||
|
||
expect(screen.getByText('Authentication Error')).toBeInTheDocument() | ||
expect(screen.getByText('An unknown error occurred')).toBeInTheDocument() | ||
expect(screen.getByRole('link', { name: /Return to Sign In/i })).toBeInTheDocument() | ||
}) | ||
|
||
it('renders with a specific error message from search params', () => { | ||
(useSearchParams as jest.Mock).mockReturnValue({ | ||
get: jest.fn().mockReturnValue(encodeURIComponent('Invalid credentials')), | ||
}) | ||
|
||
render(<AuthError />) | ||
|
||
expect(screen.getByText('Authentication Error')).toBeInTheDocument() | ||
expect(screen.getByText('Invalid credentials')).toBeInTheDocument() | ||
expect(screen.getByRole('link', { name: /Return to Sign In/i })).toBeInTheDocument() | ||
}) | ||
|
||
it('renders the fallback loading state', () => { | ||
(useSearchParams as jest.Mock).mockImplementation(() => { | ||
throw new Promise(() => {}); // Never resolves, forcing Suspense | ||
}); | ||
|
||
const { container } = render( | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<AuthError /> | ||
</Suspense> | ||
) | ||
|
||
expect(container).toHaveTextContent('Loading...') | ||
}) | ||
}) |
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
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,65 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import Page from '../page'; | ||
|
||
describe('Page Component', () => { | ||
it('renders the main elements correctly', () => { | ||
render(<Page />); | ||
|
||
// Check for the title | ||
expect(screen.getByText('Verify your email')).toBeInTheDocument(); | ||
|
||
// Check for the description | ||
expect(screen.getByText(/We ve sent you a verification link/i)).toBeInTheDocument(); | ||
|
||
// Check for the steps | ||
const steps = [ | ||
'Open your email inbox', | ||
'Click the verification link we sent you', | ||
'Return here to continue', | ||
]; | ||
steps.forEach((step) => { | ||
expect(screen.getByText(step)).toBeInTheDocument(); | ||
}); | ||
|
||
// Check for the buttons | ||
expect(screen.getByRole('button', { name: /Continue to Login/i })).toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: /Return to Home/i })).toBeInTheDocument(); | ||
|
||
// Check for the help text | ||
expect(screen.getByText(/Didnt receive the email/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('redirects to login when "Continue to Login" button is clicked', () => { | ||
global.window = Object.create(window); | ||
const mockHref = jest.fn(); | ||
Object.defineProperty(window, 'location', { | ||
value: { href: '', assign: mockHref }, | ||
writable: true, | ||
}); | ||
|
||
render(<Page />); | ||
const loginButton = screen.getByRole('button', { name: /Continue to Login/i }); | ||
fireEvent.click(loginButton); | ||
|
||
Object.defineProperty(window.location, 'href', { | ||
set: mockHref, | ||
}); | ||
|
||
}); | ||
|
||
it('redirects to home when "Return to Home" button is clicked', () => { | ||
global.window = Object.create(window); | ||
const mockHref = jest.fn(); | ||
Object.defineProperty(window, 'location', { | ||
value: { href: '', assign: mockHref }, | ||
writable: true, | ||
}); | ||
|
||
render(<Page />); | ||
const homeButton = screen.getByRole('button', { name: /Return to Home/i }); | ||
fireEvent.click(homeButton); | ||
|
||
expect(mockHref).toHaveBeenCalledWith('/'); | ||
}); | ||
}); |
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
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,51 @@ | ||
import { z } from "zod"; | ||
import { createError, createValidationError, Error } from './error'; | ||
|
||
describe('Error Schema', () => { | ||
it('should validate a correct error object', () => { | ||
const errorObject = { | ||
message: "An error occurred", | ||
errors: [ | ||
{ field: "username", message: "Username is required" }, | ||
{ field: "password", message: "Password is required" } | ||
] | ||
}; | ||
|
||
expect(() => Error.parse(errorObject)).not.toThrow(); | ||
}); | ||
|
||
it('should throw an error for an invalid error object', () => { | ||
const invalidErrorObject = { | ||
message: "", | ||
errors: [ | ||
{ field: "username", message: "" } | ||
] | ||
}; | ||
|
||
expect(() => Error.parse(invalidErrorObject)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('createError', () => { | ||
it('should create a valid error object', () => { | ||
const errorObject = createError("An error occurred", [ | ||
{ message: "Username is required" }, | ||
{ message: "Password is required" } | ||
]); | ||
|
||
expect(() => Error.parse(errorObject)).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('createValidationError', () => { | ||
it('should create a valid validation error object', () => { | ||
const validationError = new z.ZodError([ | ||
{ path: ["username"], message: "Username is required", code: "custom" }, | ||
{ path: ["password"], message: "Password is required", code: "custom" } | ||
]); | ||
|
||
const errorObject = createValidationError(validationError); | ||
|
||
expect(() => Error.parse(errorObject)).not.toThrow(); | ||
}); | ||
}); |