Skip to content

Commit 9d73b3d

Browse files
committed
feat: add success toast
1 parent 757f76f commit 9d73b3d

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

src/authz-module/libraries-manager/LibrariesTeamManager.test.tsx

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { initializeMockApp } from '@edx/frontend-platform/testing';
55
import { useLibrary, useUpdateLibrary } from '@src/authz-module/data/hooks';
66
import { useLibraryAuthZ } from './context';
77
import LibrariesTeamManager from './LibrariesTeamManager';
8+
import { ToastManagerProvider } from './ToastManagerContext';
89

910
jest.mock('./context', () => {
1011
const actual = jest.requireActual('./context');
@@ -46,6 +47,7 @@ jest.mock('../components/RoleCard', () => ({
4647
),
4748
}));
4849

50+
const renderTeamManager = () => renderWrapper(<ToastManagerProvider><LibrariesTeamManager /></ToastManagerProvider>);
4951
describe('LibrariesTeamManager', () => {
5052
const libraryData = {
5153
id: 'lib-001',
@@ -54,45 +56,47 @@ describe('LibrariesTeamManager', () => {
5456
allowPublicRead: false,
5557
};
5658
const mutate = jest.fn();
59+
const libraryAuthZContext = {
60+
libraryId: libraryData.id,
61+
libraryName: libraryData.title,
62+
libraryOrg: libraryData.org,
63+
username: 'mockuser',
64+
roles: [
65+
{
66+
name: 'Instructor',
67+
description: 'Can manage content.',
68+
userCount: 3,
69+
permissions: ['view', 'edit'],
70+
},
71+
],
72+
permissions: [
73+
{ key: 'view_library', label: 'view', resource: 'library' },
74+
{ key: 'edit_library', label: 'edit', resource: 'library' },
75+
],
76+
resources: [{ key: 'library', label: 'Library' }],
77+
canManageTeam: true,
78+
};
5779

5880
beforeEach(() => {
5981
initializeMockApp({
6082
authenticatedUser: {
6183
username: 'admin',
6284
},
6385
});
64-
mockedUseLibraryAuthZ.mockReturnValue({
65-
libraryId: libraryData.id,
66-
libraryName: libraryData.title,
67-
libraryOrg: libraryData.org,
68-
username: 'mockuser',
69-
roles: [
70-
{
71-
name: 'Instructor',
72-
description: 'Can manage content.',
73-
userCount: 3,
74-
permissions: ['view', 'edit'],
75-
},
76-
],
77-
permissions: [
78-
{ key: 'view_library', label: 'view', resource: 'library' },
79-
{ key: 'edit_library', label: 'edit', resource: 'library' },
80-
],
81-
resources: [{ key: 'library', label: 'Library' }],
82-
canManageTeam: true,
83-
});
86+
jest.resetAllMocks();
87+
mockedUseLibraryAuthZ.mockReturnValue(libraryAuthZContext);
8488

8589
(useLibrary as jest.Mock).mockReturnValue({
8690
data: libraryData,
8791
});
8892
(useUpdateLibrary as jest.Mock).mockReturnValue({
8993
mutate,
94+
isPending: false
9095
});
9196
});
9297

9398
it('renders tabs and layout content correctly', () => {
94-
renderWrapper(<LibrariesTeamManager />);
95-
99+
renderTeamManager();
96100
// Tabs
97101
expect(screen.getByRole('tab', { name: /Team Members/i })).toBeInTheDocument();
98102
expect(screen.getByRole('tab', { name: /Roles/i })).toBeInTheDocument();
@@ -112,7 +116,7 @@ describe('LibrariesTeamManager', () => {
112116
it('renders role cards when "Roles" tab is selected', async () => {
113117
const user = userEvent.setup();
114118

115-
renderWrapper(<LibrariesTeamManager />);
119+
renderTeamManager();
116120

117121
// Click on "Roles" tab
118122
const rolesTab = await screen.findByRole('tab', { name: /roles/i });
@@ -129,7 +133,7 @@ describe('LibrariesTeamManager', () => {
129133
it('renders role matrix when "Permissions" tab is selected', async () => {
130134
const user = userEvent.setup();
131135

132-
renderWrapper(<LibrariesTeamManager />);
136+
renderTeamManager();
133137

134138
// Click on "Permissions" tab
135139
const permissionsTab = await screen.findByRole('tab', { name: /permissions/i });
@@ -147,14 +151,37 @@ describe('LibrariesTeamManager', () => {
147151
it('renders allow public library read toggle and change the value by user interaction', async () => {
148152
const user = userEvent.setup();
149153

150-
renderWrapper(<LibrariesTeamManager />);
154+
renderTeamManager();
151155

152156
const readPublicToggle = await screen.findByRole('switch', { name: /Allow public read/i });
153157

154158
await user.click(readPublicToggle);
155159
expect(mutate).toHaveBeenCalledWith({
156160
libraryId: 'lib-001',
157161
updatedData: { allowPublicRead: !libraryData.allowPublicRead },
158-
});
162+
},
163+
expect.objectContaining({
164+
onSuccess: expect.any(Function),
165+
}));
166+
});
167+
168+
it('should not render the toggle if the user can not manage team and the Library Public Read is disabled', () => {
169+
(useLibrary as jest.Mock).mockReturnValue({ data: { ...libraryData, allowPublicRead: false } });
170+
(useLibraryAuthZ as jest.Mock).mockReturnValue({ ...libraryAuthZContext, canManageTeam: false });
171+
172+
renderTeamManager();
173+
expect(screen.queryByRole('switch', { name: /Allow public read/i })).not.toBeInTheDocument();
174+
});
175+
176+
it('should render the toggle as disabled if the user can not manage team but the Library Public Read is enabled', async () => {
177+
(useLibrary as jest.Mock).mockReturnValue({ data: { ...libraryData, allowPublicRead: true } });
178+
(useLibraryAuthZ as jest.Mock).mockReturnValue({ ...libraryAuthZContext, canManageTeam: false });
179+
180+
renderTeamManager();
181+
182+
const readPublicToggle = await screen.findByRole('switch', { name: /Allow public read/i });
183+
184+
expect(readPublicToggle).toBeInTheDocument();
185+
expect(readPublicToggle).toBeDisabled();
159186
});
160187
});

src/authz-module/libraries-manager/components/PublicReadToggle.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useIntl } from '@edx/frontend-platform/i18n';
22
import { Form } from '@openedx/paragon';
33
import { useLibrary, useUpdateLibrary } from '@src/authz-module/data/hooks';
4+
import { useToastManager } from '../ToastManagerContext';
45

56
import messages from './messages';
67

@@ -13,14 +14,16 @@ const PublicReadToggle = ({ libraryId, canEditToggle }: PublicReadToggleProps) =
1314
const intl = useIntl();
1415
const { data: library } = useLibrary(libraryId);
1516
const { mutate: updateLibrary, isPending } = useUpdateLibrary();
16-
17+
const { handleShowToast } = useToastManager()
1718
const onChangeToggle = () => updateLibrary({
1819
libraryId,
1920
updatedData: { allowPublicRead: !library.allowPublicRead },
21+
}, {
22+
onSuccess: () => {
23+
handleShowToast(intl.formatMessage(messages['libraries.authz.public.read.toggle.success']))
24+
}
2025
});
2126

22-
if (!library) return null;
23-
2427
if (!library.allowPublicRead && !canEditToggle) {
2528
return null;
2629
}

src/authz-module/libraries-manager/components/messages.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ const messages = defineMessages({
7171
defaultMessage: 'Allows reuse of library content in courses.',
7272
description: 'Library description toggle to allow public read',
7373
},
74+
'libraries.authz.public.read.toggle.success': {
75+
id: 'libraries.authz.public.read.toggle.success',
76+
defaultMessage: 'The library setting has been updated successfully.',
77+
description: 'Sucessfull message for allow public read',
78+
},
7479
});
7580

7681
export default messages;

0 commit comments

Comments
 (0)