refactor: useLibrary directly in the componets to access the metadata

This commit is contained in:
Diana Olarte
2025-09-26 17:43:25 +10:00
committed by Adolfo R. Brandes
parent 36d2ba434d
commit 30dd1604d1
4 changed files with 16 additions and 24 deletions

View File

@@ -3,6 +3,7 @@ import LibrariesTeamManager from './LibrariesTeamManager';
import { useLibraryAuthZ } from './context';
import { renderWrapper } from '@src/setupTest';
import { initializeMockApp } from '@edx/frontend-platform/testing';
import { useLibrary } from '@src/authz-module/data/hooks';
jest.mock('./context', () => {
const actual = jest.requireActual('./context');
@@ -14,6 +15,10 @@ jest.mock('./context', () => {
});
const mockedUseLibraryAuthZ = useLibraryAuthZ as jest.Mock;
jest.mock('@src/authz-module/data/hooks', () => ({
useLibrary: jest.fn(),
}));
jest.mock('./components/TeamTable', () => ({
__esModule: true,
default: () => <div data-testid="team-table">MockTeamTable</div>,
@@ -35,6 +40,13 @@ describe('LibrariesTeamManager', () => {
permissions: [],
canManageTeam: true,
});
(useLibrary as jest.Mock).mockReturnValue({
data: {
title: 'Test Library',
org: 'Test Org',
},
});
});
it('renders tabs and layout content correctly', () => {

View File

@@ -2,19 +2,21 @@ import { useIntl } from '@edx/frontend-platform/i18n';
import { Tab, Tabs } from '@openedx/paragon';
import TeamTable from './components/TeamTable';
import AuthZLayout from '../components/AuthZLayout';
import { useLibrary } from '@src/authz-module/data/hooks';
import { LibraryAuthZProvider, useLibraryAuthZ } from './context';
import messages from './messages';
const LibrariesAuthZTeamView = () => {
const intl = useIntl();
const { libraryId, libraryName, libraryOrg } = useLibraryAuthZ();
const { libraryId } = useLibraryAuthZ();
const { data: library } = useLibrary(libraryId)
const rootBradecrumb = intl.formatMessage(messages['library.authz.breadcrumb.root']) || '';
const pageTitle = intl.formatMessage(messages['library.authz.manage.page.title']);
return (
<div className="authz-libraries">
<AuthZLayout
context={{ id: libraryId, title: libraryName, org: libraryOrg }}
context={{ id: libraryId, title: library.title, org: library.org }}
navLinks={[{ label: rootBradecrumb }]}
activeLabel={pageTitle}
pageTitle={pageTitle}

View File

@@ -2,7 +2,6 @@ import { screen } from '@testing-library/react';
import { useParams } from 'react-router-dom';
import { useValidateUserPermissions } from '@src/data/hooks';
import { renderWrapper } from '@src/setupTest';
import { useLibrary } from '../data/hooks';
import { LibraryAuthZProvider, useLibraryAuthZ } from './context';
jest.mock('react-router-dom', () => ({
@@ -14,10 +13,6 @@ jest.mock('@src/data/hooks', () => ({
useValidateUserPermissions: jest.fn(),
}));
jest.mock('../data/hooks', () => ({
useLibrary: jest.fn(),
}));
const TestComponent = () => {
const context = useLibraryAuthZ();
return (
@@ -25,8 +20,6 @@ const TestComponent = () => {
<div data-testid="username">{context.username}</div>
<div data-testid="libraryId">{context.libraryId}</div>
<div data-testid="canManageTeam">{context.canManageTeam ? 'true' : 'false'}</div>
<div data-testid="libraryName">{context.libraryName}</div>
<div data-testid="libraryOrg">{context.libraryOrg}</div>
</div>
);
};
@@ -36,13 +29,6 @@ describe('LibraryAuthZProvider', () => {
beforeEach(() => {
jest.clearAllMocks();
(useParams as jest.Mock).mockReturnValue({ libraryId: 'lib123' });
(useLibrary as jest.Mock).mockReturnValue({
data: {
title: 'Test Library',
org: 'Test Org',
},
});
});
it('provides the correct context values to consumers', () => {
@@ -62,8 +48,6 @@ describe('LibraryAuthZProvider', () => {
expect(screen.getByTestId('username')).toHaveTextContent('testuser');
expect(screen.getByTestId('libraryId')).toHaveTextContent('lib123');
expect(screen.getByTestId('canManageTeam')).toHaveTextContent('true');
expect(screen.getByTestId('libraryName')).toHaveTextContent('Test Library');
expect(screen.getByTestId('libraryOrg')).toHaveTextContent('Test Org');
});
it('throws error when user lacks both view and manage permissions', () => {

View File

@@ -21,8 +21,6 @@ type LibraryAuthZContextType = {
libraryId: string;
roles: string[];
permissions: string[];
libraryName: string;
libraryOrg: string;
};
const LibraryAuthZContext = createContext<LibraryAuthZContextType | undefined>(undefined);
@@ -48,13 +46,9 @@ export const LibraryAuthZProvider: React.FC<AuthZProviderProps> = ({ children })
throw new Error('NoAccess');
}
const { data: libraryMetadata } = useLibrary(libraryId);
const value = useMemo((): LibraryAuthZContextType => ({
username: authenticatedUser.username,
libraryId,
libraryName: libraryMetadata.title,
libraryOrg: libraryMetadata.org,
roles: [],
permissions: [],
canManageTeam,