-
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.
refactor(resourceadm): Moving headercontext and gitea header
- Loading branch information
Showing
7 changed files
with
121 additions
and
53 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
File renamed without changes.
19 changes: 19 additions & 0 deletions
19
frontend/resourceadm/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 React 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 = React.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 |
---|---|---|
@@ -1,60 +1,106 @@ | ||
import { SelectedContextType } from 'app-shared/navigation/main-header/Header'; | ||
import { userHasAccessToOrganization } from './index'; | ||
import { SelectedContextType } from 'resourceadm/context/HeaderContext'; | ||
import { getOrgNameByUsername, userHasAccessToOrganization } from './userUtils'; | ||
import { type Organization } from 'app-shared/types/Organization'; | ||
|
||
describe('userHasAccessToOrganization', () => { | ||
it('should return true when context is self', () => { | ||
const result = userHasAccessToOrganization({ | ||
org: SelectedContextType.Self, | ||
orgs: [], | ||
const mockOrg1: Organization = { | ||
avatar_url: '', | ||
id: 12, | ||
username: 'ttd', | ||
full_name: 'Test', | ||
}; | ||
const mockOrg2: Organization = { | ||
avatar_url: '', | ||
id: 23, | ||
username: 'unit-test-2', | ||
full_name: 'unit-test-2', | ||
}; | ||
const mockOrganizations: Organization[] = [mockOrg1, mockOrg2]; | ||
|
||
describe('userUtils', () => { | ||
describe('userHasAccessToOrganization', () => { | ||
it('should return true when context is self', () => { | ||
const result = userHasAccessToOrganization({ | ||
org: SelectedContextType.Self, | ||
orgs: [], | ||
}); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
it('should return true when context is all', () => { | ||
const result = userHasAccessToOrganization({ | ||
org: SelectedContextType.All, | ||
orgs: [], | ||
}); | ||
|
||
it('should return true when context is all', () => { | ||
const result = userHasAccessToOrganization({ | ||
org: SelectedContextType.All, | ||
orgs: [], | ||
expect(result).toBe(true); | ||
}); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
it('should return true when context id is present in orgs list', () => { | ||
const result = userHasAccessToOrganization({ | ||
org: 'username1', | ||
orgs: [ | ||
{ | ||
avatar_url: 'avatar_url', | ||
description: '', | ||
full_name: 'full_name', | ||
id: 1, | ||
location: '', | ||
username: 'username1', | ||
website: '', | ||
}, | ||
], | ||
}); | ||
|
||
it('should return true when context id is present in orgs list', () => { | ||
const result = userHasAccessToOrganization({ | ||
org: 'username1', | ||
orgs: [ | ||
{ | ||
avatar_url: 'avatar_url', | ||
description: '', | ||
full_name: 'full_name', | ||
id: 1, | ||
location: '', | ||
username: 'username1', | ||
website: '', | ||
}, | ||
], | ||
}); | ||
|
||
expect(result).toBe(true); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false when context id is not present in orgs list', () => { | ||
const result = userHasAccessToOrganization({ | ||
org: 'username2', | ||
orgs: [ | ||
{ | ||
avatar_url: 'avatar_url', | ||
description: '', | ||
full_name: 'full_name', | ||
id: 1, | ||
location: '', | ||
username: 'username', | ||
website: '', | ||
}, | ||
], | ||
}); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
it('should return false when context id is not present in orgs list', () => { | ||
const result = userHasAccessToOrganization({ | ||
org: 'username2', | ||
orgs: [ | ||
{ | ||
avatar_url: 'avatar_url', | ||
description: '', | ||
full_name: 'full_name', | ||
id: 1, | ||
location: '', | ||
username: 'username', | ||
website: '', | ||
}, | ||
], | ||
}); | ||
|
||
expect(result).toBe(false); | ||
describe('getOrgNameByUsername', () => { | ||
it('should return the full name of the organization when a matching username is found', () => { | ||
const result = getOrgNameByUsername(mockOrg1.username, mockOrganizations); | ||
expect(result).toBe(mockOrg1.full_name); | ||
}); | ||
|
||
it('should return the username if full name is not available', () => { | ||
const mockOrg3: Organization = { | ||
username: 'org3', | ||
full_name: '', | ||
id: 12, | ||
avatar_url: '', | ||
}; | ||
const orgsWithoutFullName: Organization[] = [mockOrg3]; | ||
const result = getOrgNameByUsername(mockOrg3.username, orgsWithoutFullName); | ||
expect(result).toBe(mockOrg3.username); | ||
}); | ||
|
||
it('should return undefined if the organization is not found', () => { | ||
const result = getOrgNameByUsername('nonexistent-org', mockOrganizations); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if orgs array is undefined', () => { | ||
const result = getOrgNameByUsername(mockOrg1.username, undefined); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
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