diff --git a/apps/web/src/common/components/Header/User.test.tsx b/apps/web/src/common/components/Header/User.test.tsx new file mode 100755 index 000000000..383d5dcb8 --- /dev/null +++ b/apps/web/src/common/components/Header/User.test.tsx @@ -0,0 +1,59 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import User from './User'; + +jest.mock('next/router', () => ({ + useRouter: () => ({ push: jest.fn() }), +})); + +jest.mock('next/image', () => ({ + __esModule: true, + default: (props: any) => { + // eslint-disable-next-line @next/next/no-img-element + return {props.alt}; + }, +})); + +jest.mock('@modules/auth', () => ({ + useUserInfo: () => ({ + providerUser: { avatarUrl: 'https://example.com/a.png', name: 'alice' }, + roleLevel: 0, + dashboardRole: false, + }), +})); + +jest.mock('@modules/auth/UserInfoStore', () => ({ + resetUserInfo: jest.fn(), +})); + +jest.mock('@oss-compass/graphql', () => ({ + useSignOutMutation: () => ({ mutate: jest.fn() }), +})); + +describe('Header User menu', () => { + it('exposes the avatar as a focusable menu trigger button', () => { + render(); + + const trigger = screen.getByRole('button', { name: 'alice' }); + expect(trigger).toHaveAttribute('aria-haspopup', 'menu'); + }); + + it('exposes the sign-out action as a button', () => { + render(); + + expect( + screen.getByRole('button', { name: /common:signout/i }) + ).toBeInTheDocument(); + }); + + it('renders the menu entries as links reachable after the trigger', () => { + render(); + + expect( + screen.getByRole('link', { name: /common:profile_setting/i }) + ).toBeInTheDocument(); + expect( + screen.getByRole('link', { name: /common:subscribe/i }) + ).toBeInTheDocument(); + }); +}); diff --git a/apps/web/src/common/components/Header/User.tsx b/apps/web/src/common/components/Header/User.tsx index 70d9e1828..a8ad816db 100644 --- a/apps/web/src/common/components/Header/User.tsx +++ b/apps/web/src/common/components/Header/User.tsx @@ -34,7 +34,12 @@ const User = () => { return (
-
+
+ -
+
{ {t('common:profile_setting')} -
{ mutation.mutate( {}, @@ -108,7 +114,7 @@ const User = () => { }} > {t('common:signout')} -
+