-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore/13697-resourceadm-use-new-studiopagehe…
…ader-component-in-resourceadm
- Loading branch information
Showing
92 changed files
with
1,544 additions
and
268 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
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
19 changes: 19 additions & 0 deletions
19
frontend/dashboard/context/HeaderContext/HeaderContext.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,19 @@ | ||
import { createContext } from 'react'; | ||
import { type Organization } from 'app-shared/types/Organization'; | ||
import { type User } from 'app-shared/types/Repository'; | ||
|
||
export enum SelectedContextType { | ||
All = 'all', | ||
Self = 'self', | ||
None = 'none', | ||
} | ||
|
||
export type HeaderContextType = { | ||
selectableOrgs?: Organization[]; | ||
user: User; | ||
}; | ||
|
||
export const HeaderContext = createContext<HeaderContextType>({ | ||
selectableOrgs: undefined, | ||
user: undefined, | ||
}); |
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 @@ | ||
export { HeaderContext, type HeaderContextType, SelectedContextType } from './HeaderContext'; |
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 @@ | ||
export { usePageHeaderTitle } from './usePageHeaderTitle'; |
50 changes: 50 additions & 0 deletions
50
frontend/dashboard/hooks/usePageHeaderTitle/usePageHeaderTitle.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,50 @@ | ||
import React from 'react'; | ||
import { usePageHeaderTitle } from './usePageHeaderTitle'; | ||
import { HeaderContext, type HeaderContextType } from 'dashboard/context/HeaderContext'; | ||
import { useSelectedContext } from 'dashboard/hooks/useSelectedContext'; | ||
import { headerContextValueMock } from 'dashboard/testing/headerContextMock'; | ||
import { SelectedContextType } from 'dashboard/context/HeaderContext'; | ||
import { mockOrg1 } from 'dashboard/testing/organizationMock'; | ||
import { renderHookWithProviders } from 'dashboard/testing/mocks'; | ||
|
||
jest.mock('dashboard/hooks/useSelectedContext'); | ||
|
||
const renderUsePageHeaderTitleHook = (headerContextValueProps: Partial<HeaderContextType> = {}) => { | ||
return renderHookWithProviders(usePageHeaderTitle, { | ||
externalWrapper: (children) => ( | ||
<HeaderContext.Provider value={{ ...headerContextValueMock, ...headerContextValueProps }}> | ||
{children} | ||
</HeaderContext.Provider> | ||
), | ||
}); | ||
}; | ||
|
||
describe('usePageHeaderTitle', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return the organization name when a valid context is selected', () => { | ||
(useSelectedContext as jest.Mock).mockReturnValue(mockOrg1.username); | ||
|
||
const { result } = renderUsePageHeaderTitleHook(); | ||
|
||
expect(result.current).toBe(mockOrg1.full_name); | ||
}); | ||
|
||
it('should return an empty string when selected context is All', () => { | ||
(useSelectedContext as jest.Mock).mockReturnValue(SelectedContextType.All); | ||
|
||
const { result } = renderUsePageHeaderTitleHook(); | ||
|
||
expect(result.current).toBe(''); | ||
}); | ||
|
||
it('should return an empty string when selected context is Self', () => { | ||
(useSelectedContext as jest.Mock).mockReturnValue(SelectedContextType.Self); | ||
|
||
const { result } = renderUsePageHeaderTitleHook(); | ||
|
||
expect(result.current).toBe(''); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
frontend/dashboard/hooks/usePageHeaderTitle/usePageHeaderTitle.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,14 @@ | ||
import { useContext } from 'react'; | ||
import { HeaderContext, SelectedContextType } from 'dashboard/context/HeaderContext'; | ||
import { getOrgNameByUsername } from 'dashboard/utils/userUtils'; | ||
import { useSelectedContext } from '../useSelectedContext'; | ||
|
||
export const usePageHeaderTitle = () => { | ||
const selectedContext = useSelectedContext(); | ||
const { selectableOrgs } = useContext(HeaderContext); | ||
|
||
if (selectedContext !== SelectedContextType.All && selectedContext !== SelectedContextType.Self) { | ||
return getOrgNameByUsername(selectedContext, selectableOrgs); | ||
} | ||
return ''; | ||
}; |
1 change: 1 addition & 0 deletions
1
frontend/dashboard/hooks/useProfileMenuTriggerButtonText/index.ts
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 @@ | ||
export { useProfileMenuTriggerButtonText } from './useProfileMenuTriggerButtonText'; |
68 changes: 68 additions & 0 deletions
68
.../dashboard/hooks/useProfileMenuTriggerButtonText/useProfileMenuTriggerButtonText.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,68 @@ | ||
import React from 'react'; | ||
import { useProfileMenuTriggerButtonText } from './useProfileMenuTriggerButtonText'; | ||
import { | ||
HeaderContext, | ||
type HeaderContextType, | ||
SelectedContextType, | ||
} from 'dashboard/context/HeaderContext'; | ||
import { useSelectedContext } from '../useSelectedContext'; | ||
import { userMock } from 'dashboard/testing/userMock'; | ||
import { headerContextValueMock } from 'dashboard/testing/headerContextMock'; | ||
import { textMock } from '@studio/testing/mocks/i18nMock'; | ||
import { mockOrg1 } from 'dashboard/testing/organizationMock'; | ||
import { renderHookWithProviders } from 'dashboard/testing/mocks'; | ||
|
||
jest.mock('../useSelectedContext'); | ||
|
||
const renderUseProfileMenuTriggerButtonTextHook = ( | ||
headerContextValueProps: Partial<HeaderContextType> = {}, | ||
) => { | ||
return renderHookWithProviders(useProfileMenuTriggerButtonText, { | ||
externalWrapper: (children) => ( | ||
<HeaderContext.Provider value={{ ...headerContextValueMock, ...headerContextValueProps }}> | ||
{children} | ||
</HeaderContext.Provider> | ||
), | ||
}); | ||
}; | ||
|
||
describe('useProfileMenuTriggerButtonText', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return the full name of the user when selected context is Self', () => { | ||
(useSelectedContext as jest.Mock).mockReturnValue(SelectedContextType.Self); | ||
|
||
const { result } = renderUseProfileMenuTriggerButtonTextHook(); | ||
|
||
expect(result.current).toBe(userMock.full_name); | ||
}); | ||
|
||
it('should return the login name of the user when full_name is not available', () => { | ||
(useSelectedContext as jest.Mock).mockReturnValue(SelectedContextType.Self); | ||
|
||
const { result } = renderUseProfileMenuTriggerButtonTextHook({ | ||
user: { ...userMock, full_name: '' }, | ||
}); | ||
|
||
expect(result.current).toBe(userMock.login); | ||
}); | ||
|
||
it('should return the organization and username when selected context is an organization', () => { | ||
(useSelectedContext as jest.Mock).mockReturnValue(mockOrg1.username); | ||
const { result } = renderUseProfileMenuTriggerButtonTextHook(); | ||
|
||
expect(result.current).toBe( | ||
textMock('shared.header_user_for_org', { user: userMock.full_name, org: mockOrg1.full_name }), | ||
); | ||
}); | ||
|
||
it('should return the username when selected context is All', () => { | ||
(useSelectedContext as jest.Mock).mockReturnValue(SelectedContextType.All); | ||
|
||
const { result } = renderUseProfileMenuTriggerButtonTextHook(); | ||
|
||
expect(result.current).toBe(userMock.full_name); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
frontend/dashboard/hooks/useProfileMenuTriggerButtonText/useProfileMenuTriggerButtonText.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,21 @@ | ||
import { useContext } from 'react'; | ||
import { HeaderContext, SelectedContextType } from 'dashboard/context/HeaderContext'; | ||
import { getOrgNameByUsername } from 'dashboard/utils/userUtils'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useSelectedContext } from '../useSelectedContext'; | ||
|
||
export const useProfileMenuTriggerButtonText = (): string => { | ||
const { t } = useTranslation(); | ||
const { user, selectableOrgs } = useContext(HeaderContext); | ||
const selectedContext = useSelectedContext(); | ||
|
||
const username = user.full_name || user.login; | ||
|
||
if (selectedContext !== SelectedContextType.All && selectedContext !== SelectedContextType.Self) { | ||
return t('shared.header_user_for_org', { | ||
user: username, | ||
org: getOrgNameByUsername(selectedContext, selectableOrgs), | ||
}); | ||
} | ||
return username; | ||
}; |
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 @@ | ||
export { useRepoPath } from './useRepoPath'; |
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,53 @@ | ||
import React from 'react'; | ||
import { useRepoPath } from './useRepoPath'; | ||
import { HeaderContext, type HeaderContextType } from 'dashboard/context/HeaderContext'; | ||
import { useSelectedContext } from 'dashboard/hooks/useSelectedContext'; | ||
import { headerContextValueMock } from 'dashboard/testing/headerContextMock'; | ||
import { repositoryOwnerPath, repositoryBasePath } from 'app-shared/api/paths'; | ||
import { mockOrg1 } from 'dashboard/testing/organizationMock'; | ||
import { userMock } from 'dashboard/testing/userMock'; | ||
import { renderHookWithProviders } from 'dashboard/testing/mocks'; | ||
|
||
jest.mock('dashboard/hooks/useSelectedContext'); | ||
|
||
const renderUseRepoPathHook = (headerContextValueProps: Partial<HeaderContextType> = {}) => { | ||
return renderHookWithProviders(useRepoPath, { | ||
externalWrapper: (children) => ( | ||
<HeaderContext.Provider value={{ ...headerContextValueMock, ...headerContextValueProps }}> | ||
{children} | ||
</HeaderContext.Provider> | ||
), | ||
}); | ||
}; | ||
|
||
describe('useRepoPath', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return the repository owner path when an organization is selected', () => { | ||
(useSelectedContext as jest.Mock).mockReturnValue(mockOrg1.username); | ||
|
||
const { result } = renderUseRepoPathHook(); | ||
|
||
expect(result.current).toBe(repositoryOwnerPath(mockOrg1.username)); | ||
}); | ||
|
||
it('should return the user login as the owner path when no organization is selected', () => { | ||
(useSelectedContext as jest.Mock).mockReturnValue(null); | ||
|
||
const { result } = renderUseRepoPathHook(); | ||
|
||
expect(result.current).toBe(repositoryOwnerPath(userMock.login)); | ||
}); | ||
|
||
it('should return the repository base path if neither organization nor user is available', () => { | ||
(useSelectedContext as jest.Mock).mockReturnValue(null); | ||
|
||
const { result } = renderUseRepoPathHook({ | ||
user: { ...userMock, login: '' }, | ||
}); | ||
|
||
expect(result.current).toBe(repositoryBasePath()); | ||
}); | ||
}); |
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,18 @@ | ||
import { useContext } from 'react'; | ||
import { repositoryBasePath, repositoryOwnerPath } from 'app-shared/api/paths'; | ||
import { getOrgUsernameByUsername } from 'dashboard/utils/userUtils'; | ||
import { useSelectedContext } from 'dashboard/hooks/useSelectedContext'; | ||
import { HeaderContext } from 'dashboard/context/HeaderContext'; | ||
|
||
export const useRepoPath = () => { | ||
const { user, selectableOrgs } = useContext(HeaderContext); | ||
|
||
const selectedContext = useSelectedContext(); | ||
const org = getOrgUsernameByUsername(selectedContext, selectableOrgs); | ||
|
||
const owner = org || user?.login; | ||
if (owner) { | ||
return repositoryOwnerPath(owner); | ||
} | ||
return repositoryBasePath(); | ||
}; |
2 changes: 1 addition & 1 deletion
2
frontend/dashboard/hooks/useSelectedContext/useSelectedContext.ts
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
Oops, something went wrong.