-
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.
⚗️(frontend) show username on AccountDropDown
- show username instead of "My account"
- Loading branch information
1 parent
232ea97
commit 1f75ae9
Showing
7 changed files
with
241 additions
and
21 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
Submodule secrets
updated
from 2572ba to 49b591
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
69 changes: 69 additions & 0 deletions
69
src/frontend/apps/desk/src/features/header/__tests__/AccountDropdown.test.tsx
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,69 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
|
||
import { useAuthStore } from '@/core/auth'; | ||
import { AppWrapper } from '@/tests/utils'; | ||
|
||
import { AccountDropdown } from '../AccountDropdown'; | ||
|
||
jest.mock('@/core/auth', () => ({ | ||
useAuthStore: jest.fn(), | ||
})); | ||
|
||
describe('AccountDropdown', () => { | ||
const mockLogout = jest.fn(); | ||
const mockUserData = { | ||
id: '1', | ||
email: '[email protected]', | ||
name: 'Test User', | ||
}; | ||
|
||
const renderAccountDropdown = () => | ||
render(<AccountDropdown />, { wrapper: AppWrapper }); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(useAuthStore as unknown as jest.Mock).mockReturnValue({ | ||
userData: mockUserData, | ||
logout: mockLogout, | ||
}); | ||
}); | ||
|
||
it('renders the user name correctly', async () => { | ||
renderAccountDropdown(); | ||
|
||
expect(await screen.findByText('Test User')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders "No Username" when userData name is missing', () => { | ||
(useAuthStore as unknown as jest.Mock).mockReturnValue({ | ||
userData: { id: '1', email: '[email protected]' }, // No name property | ||
logout: mockLogout, | ||
}); | ||
renderAccountDropdown(); | ||
expect(screen.getByText('No Username')).toBeInTheDocument(); | ||
}); | ||
|
||
it('opens the dropdown and shows logout button when clicked', async () => { | ||
renderAccountDropdown(); | ||
|
||
const dropButton = await screen.findByText('Test User'); | ||
fireEvent.click(dropButton); | ||
|
||
expect(screen.getByText('Logout')).toBeInTheDocument(); | ||
expect(screen.getByLabelText('Logout')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls logout function when logout button is clicked', async () => { | ||
renderAccountDropdown(); | ||
|
||
// Open the dropdown first | ||
const dropButton = await screen.findByText('Test User'); | ||
fireEvent.click(dropButton); | ||
|
||
// Click the logout button | ||
const logoutButton = screen.getByLabelText('Logout'); | ||
fireEvent.click(logoutButton); | ||
|
||
expect(mockLogout).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
142 changes: 142 additions & 0 deletions
142
...rontend/apps/desk/src/features/mail-domains/access-management/__tests__/accesses.test.tsx
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,142 @@ | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import { useRouter as useNavigate } from 'next/navigation'; | ||
import { useRouter } from 'next/router'; | ||
|
||
import { AccessesContent } from '@/features/mail-domains/access-management'; | ||
import { Role, useMailDomain } from '@/features/mail-domains/domains'; | ||
import MailDomainAccessesPage from '@/pages/mail-domains/[slug]/accesses'; | ||
import { AppWrapper } from '@/tests/utils'; | ||
|
||
jest.mock('next/navigation', () => ({ | ||
useRouter: jest.fn(), | ||
})); | ||
|
||
jest.mock('next/router', () => ({ | ||
useRouter: jest.fn(), | ||
})); | ||
|
||
jest.mock('@/features/mail-domains/domains/api/useMailDomain', () => ({ | ||
useMailDomain: jest.fn(), | ||
})); | ||
|
||
jest.mock( | ||
'@/features/mail-domains/access-management/components/AccessesContent', | ||
() => ({ | ||
AccessesContent: jest.fn(() => <div>AccessContent</div>), | ||
}), | ||
); | ||
|
||
describe('MailDomainAccessesPage', () => { | ||
const mockRouterReplace = jest.fn(); | ||
const mockNavigate = { replace: mockRouterReplace }; | ||
const mockRouter = { | ||
query: { slug: 'example-slug' }, | ||
}; | ||
|
||
(useRouter as jest.Mock).mockReturnValue(mockRouter); | ||
(useNavigate as jest.Mock).mockReturnValue(mockNavigate); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(useRouter as jest.Mock).mockReturnValue(mockRouter); | ||
(useNavigate as jest.Mock).mockReturnValue(mockNavigate); | ||
}); | ||
|
||
const renderPage = () => { | ||
render(<MailDomainAccessesPage />, { wrapper: AppWrapper }); | ||
}; | ||
|
||
it('renders loader while loading', () => { | ||
(useMailDomain as jest.Mock).mockReturnValue({ | ||
isLoading: true, | ||
data: null, | ||
error: null, | ||
isError: false, | ||
}); | ||
|
||
renderPage(); | ||
|
||
expect(screen.getByRole('status')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders error message when there is an error', () => { | ||
(useMailDomain as jest.Mock).mockReturnValue({ | ||
isLoading: false, | ||
data: null, | ||
error: { cause: ['Error loading domain data'] }, | ||
isError: true, | ||
}); | ||
|
||
renderPage(); | ||
|
||
expect(screen.getByText('Error loading domain data')).toBeInTheDocument(); | ||
}); | ||
|
||
it('redirects to 404 page if the domain is not found', async () => { | ||
(useMailDomain as jest.Mock).mockReturnValue({ | ||
isLoading: false, | ||
data: null, | ||
error: { status: 404 }, | ||
isError: true, | ||
}); | ||
|
||
renderPage(); | ||
|
||
await waitFor(() => { | ||
expect(mockRouterReplace).toHaveBeenCalledWith('/404'); | ||
}); | ||
}); | ||
|
||
it('renders the AccessesContent when data is available', async () => { | ||
const mockMailDomain = { | ||
id: '1-1-1-1-1', | ||
name: 'example.com', | ||
slug: 'example-com', | ||
status: 'enabled', | ||
created_at: new Date().toISOString(), | ||
updated_at: new Date().toISOString(), | ||
abilities: { | ||
get: true, | ||
patch: true, | ||
put: true, | ||
post: true, | ||
delete: true, | ||
manage_accesses: true, | ||
}, | ||
}; | ||
|
||
(useMailDomain as jest.Mock).mockReturnValue({ | ||
isLoading: false, | ||
data: mockMailDomain, | ||
error: null, | ||
isError: false, | ||
}); | ||
|
||
renderPage(); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('AccessContent')).toBeInTheDocument(); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(AccessesContent).toHaveBeenCalledWith( | ||
{ | ||
mailDomain: mockMailDomain, | ||
currentRole: Role.OWNER, | ||
}, | ||
{}, // adding this empty object is necessary to load jest context and that AccessContent is a mock | ||
); | ||
}); | ||
}); | ||
|
||
it('throws an error when slug is invalid', () => { | ||
// used to not add expected error to jest logs | ||
console.error = jest.fn(() => {}); | ||
|
||
(useRouter as jest.Mock).mockReturnValue({ | ||
query: { slug: ['invalid-array-slug-in-array'] }, | ||
}); | ||
|
||
expect(() => renderPage()).toThrow('Invalid mail domain slug'); | ||
}); | ||
}); |
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