Skip to content

Commit

Permalink
UIQM-735: Return also sub permissions in useUserTenantPermissions h…
Browse files Browse the repository at this point in the history
…ook. (#761)
  • Loading branch information
Dmytro-Melnyshyn authored Nov 26, 2024
1 parent a4c1649 commit 9eb5666
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-npm-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ jobs:
comment_title: Jest Unit Test Statistics

- name: Publish Jest coverage report
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: always()
with:
name: jest-coverage-report
path: ${{ env.JEST_COVERAGE_REPORT_DIR }}
retention-days: 30

- name: Publish yarn.lock
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: failure()
with:
name: yarn.lock
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ jobs:
comment_title: Jest Unit Test Statistics

- name: Publish Jest coverage report
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: always()
with:
name: jest-coverage-report
path: ${{ env.JEST_COVERAGE_REPORT_DIR }}
retention-days: 30

- name: Publish yarn.lock
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: failure()
with:
name: yarn.lock
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change history for ui-quick-marc

## [8.0.2] (IN PROGRESS)

* [UIQM-735](https://issues.folio.org/browse/UIQM-735) Return also sub permissions in `useUserTenantPermissions` hook.

## [8.0.1] (https://github.com/folio-org/ui-quick-marc/tree/v8.0.1) (2024-04-18)

* [UIQM-641](https://issues.folio.org/browse/UIQM-641) Call `cleanBytesFields` function with correct arguments to fix 008 field.
Expand Down
2 changes: 1 addition & 1 deletion src/MarcRoute/MarcRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const MarcRoute = ({
});

const checkCentralTenantPerm = useCallback((perm) => {
return centralTenantPermissions.some(({ permissionName }) => permissionName === perm);
return centralTenantPermissions.has(perm);
}, [centralTenantPermissions]);

if (isCentralTenantPermissionsLoading) {
Expand Down
6 changes: 2 additions & 4 deletions src/MarcRoute/MarcRoute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const renderMarcRoute = ({ history, ...props } = {}) => (render(
describe('Given Quick Marc', () => {
beforeEach(() => {
useUserTenantPermissions.mockReturnValue({
userPermissions: [],
userPermissions: new Set(),
isFetching: false,
});

Expand All @@ -49,9 +49,7 @@ describe('Given Quick Marc', () => {

describe('when a member tenant edits shared record', () => {
it('should fetch the central tenant permissions', () => {
const userPermissions = [{
permissionName: 'ui-quick-marc.quick-marc-editor.all',
}];
const userPermissions = new Set(['ui-quick-marc.quick-marc-editor.all']);

useUserTenantPermissions.mockReturnValue({
userPermissions,
Expand Down
4 changes: 4 additions & 0 deletions src/QuickMarcEditor/QuickMarcEditor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React from 'react';
import faker from 'faker';
import { useLocation } from 'react-router';
import { createMemoryHistory } from 'history';

import {
render,
Expand All @@ -25,9 +26,12 @@ import Harness from '../../test/jest/helpers/harness';
import buildStripes from '../../test/jest/__mock__/stripesCore.mock';
import { bibLeader } from '../../test/jest/fixtures/leaders';

const mockHistory = createMemoryHistory();

jest.mock('react-router', () => ({
...jest.requireActual('react-router'),
useLocation: jest.fn(),
withRouter: Component => props => <Component history={mockHistory} {...props} />,
}));

jest.mock('../queries', () => ({
Expand Down
48 changes: 24 additions & 24 deletions src/QuickMarcEditor/QuickMarcEditorContainer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,16 @@ describe('Given Quick Marc Editor Container', () => {
});
});

recordLengthField = screen.getByRole('textbox', { name: 'ui-quick-marc.record.fixedField.Record length' });
statusField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.Status' });
typeField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.Type' });
blvlField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.BLvl' });
ctrlField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.Ctrl' });
positions9to16Field = screen.getByRole('textbox', { name: 'ui-quick-marc.record.fixedField.9-16 positions' });
elvlField = screen.getByRole('textbox', { name: 'ui-quick-marc.record.fixedField.tip.ELvl' });
descField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.Desc' });
multiLvlField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.MultiLvl' });
positions20to23Field = screen.getByRole('textbox', { name: 'ui-quick-marc.record.fixedField.20-23 positions' });
recordLengthField = screen.getByRole('textbox', { name: /ui-quick-marc.record.fixedField.Record length/ });
statusField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.Status/ });
typeField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.Type/ });
blvlField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.BLvl/ });
ctrlField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.Ctrl/ });
positions9to16Field = screen.getByRole('textbox', { name: /ui-quick-marc.record.fixedField.9-16 positions/ });
elvlField = screen.getByRole('textbox', { name: /ui-quick-marc.record.fixedField.tip.ELvl/ });
descField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.Desc/ });
multiLvlField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.MultiLvl/ });
positions20to23Field = screen.getByRole('textbox', { name: /ui-quick-marc.record.fixedField.20-23 positions/ });
});

it('should display correct default values', () => {
Expand Down Expand Up @@ -408,13 +408,13 @@ describe('Given Quick Marc Editor Container', () => {
});
});

recordLengthField = screen.getByRole('textbox', { name: 'ui-quick-marc.record.fixedField.Record length' });
statusField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.Status' });
typeField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.Type' });
positions7to16Field = screen.getByRole('textbox', { name: 'ui-quick-marc.record.fixedField.7-16 positions' });
elvlField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.ELvl' });
punctField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.Punct' });
positions19to23Field = screen.getByRole('textbox', { name: 'ui-quick-marc.record.fixedField.19-23 positions' });
recordLengthField = screen.getByRole('textbox', { name: /ui-quick-marc.record.fixedField.Record length/ });
statusField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.Status/ });
typeField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.Type/ });
positions7to16Field = screen.getByRole('textbox', { name: /ui-quick-marc.record.fixedField.7-16 positions/ });
elvlField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.ELvl/ });
punctField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.Punct/ });
positions19to23Field = screen.getByRole('textbox', { name: /ui-quick-marc.record.fixedField.19-23 positions/ });
});

it('should display correct default values', () => {
Expand Down Expand Up @@ -492,13 +492,13 @@ describe('Given Quick Marc Editor Container', () => {
});
});

recordLengthField = screen.getByRole('textbox', { name: 'ui-quick-marc.record.fixedField.Record length' });
statusField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.Status' });
typeField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.Type' });
positions7to16Field = screen.getByRole('textbox', { name: 'ui-quick-marc.record.fixedField.7-16 positions' });
elvlField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.ELvl' });
itemField = screen.getByRole('combobox', { name: 'ui-quick-marc.record.fixedField.tip.Item' });
positions19to23Field = screen.getByRole('textbox', { name: 'ui-quick-marc.record.fixedField.19-23 positions' });
recordLengthField = screen.getByRole('textbox', { name: /ui-quick-marc.record.fixedField.Record length/ });
statusField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.Status/ });
typeField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.Type/ });
positions7to16Field = screen.getByRole('textbox', { name: /ui-quick-marc.record.fixedField.7-16 positions/ });
elvlField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.ELvl/ });
itemField = screen.getByRole('combobox', { name: /ui-quick-marc.record.fixedField.tip.Item/ });
positions19to23Field = screen.getByRole('textbox', { name: /ui-quick-marc.record.fixedField.19-23 positions/ });
});

it('should display correct default values', async () => {
Expand Down
19 changes: 18 additions & 1 deletion src/queries/useUserTenantPermissions/useUserTenantPermissions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useMemo } from 'react';
import { useQuery } from 'react-query';

import {
Expand Down Expand Up @@ -48,10 +49,26 @@ const useUserTenantPermissions = (
},
);

const tenantPermissions = data.permissionNames || INITIAL_DATA;

const flattenTenantPermissions = useMemo(() => {
const permSet = new Set();

tenantPermissions.forEach(perm => {
permSet.add(perm.permissionName);

perm.subPermissions?.forEach(subPermission => {
permSet.add(subPermission);
});
});

return permSet;
}, [tenantPermissions]);

return ({
isFetching,
isLoading,
userPermissions: data.permissionNames || INITIAL_DATA,
userPermissions: flattenTenantPermissions,
totalRecords: data.totalRecords,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ const wrapper = ({ children }) => (
);

const response = {
permissionNames: [],
permissionNames: [
{
permissionName: 'permissionName1',
subPermissions: ['subPermissions1', 'subPermissions2'],
},
{
permissionName: 'permissionName2',
subPermissions: ['subPermissions1', 'subPermissions3'],
},
],
totalRecords: 0,
};

Expand Down Expand Up @@ -54,4 +63,22 @@ describe('useUserTenantPermissions', () => {
expect(setHeaderMock).toHaveBeenCalledWith('X-Okapi-Tenant', options.tenantId);
expect(getMock).toHaveBeenCalledWith(`perms/users/${options.userId}/permissions`, expect.objectContaining({}));
});

it('should consider sub permissions without duplicates', async () => {
const options = {
userId: 'userId',
tenantId: 'tenantId',
};
const { result } = renderHook(() => useUserTenantPermissions(options), { wrapper });

await act(async () => !result.current.isLoading);

expect([...result.current.userPermissions]).toEqual([
'permissionName1',
'subPermissions1',
'subPermissions2',
'permissionName2',
'subPermissions3',
]);
});
});

0 comments on commit 9eb5666

Please sign in to comment.