forked from openedx/frontend-app-admin-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrariesTeamManager.test.tsx
More file actions
195 lines (162 loc) · 6.67 KB
/
LibrariesTeamManager.test.tsx
File metadata and controls
195 lines (162 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderWrapper } from '@src/setupTest';
import { initializeMockApp } from '@edx/frontend-platform/testing';
import { useLibrary, useUpdateLibrary } from '@src/authz-module/data/hooks';
import { useLibraryAuthZ } from './context';
import LibrariesTeamManager from './LibrariesTeamManager';
import { ToastManagerProvider } from './ToastManagerContext';
import { CONTENT_LIBRARY_PERMISSIONS } from './constants';
jest.mock('./context', () => {
const actual = jest.requireActual('./context');
return {
...actual,
useLibraryAuthZ: jest.fn(),
LibraryAuthZProvider: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
};
});
const mockedUseLibraryAuthZ = useLibraryAuthZ as jest.Mock;
jest.mock('@src/authz-module/data/hooks', () => ({
useLibrary: jest.fn(),
useUpdateLibrary: jest.fn(),
}));
jest.mock('./components/TeamTable', () => ({
__esModule: true,
default: () => <div data-testid="team-table">MockTeamTable</div>,
}));
jest.mock('./components/AddNewTeamMemberModal', () => ({
__esModule: true,
AddNewTeamMemberTrigger: () => <div data-testid="add-team-member-trigger">MockAddNewTeamMemberTrigger</div>,
}));
jest.mock('../components/RoleCard', () => ({
__esModule: true,
default: ({ title, description, permissionsByResource }: {
title: string,
description: string,
permissionsByResource: any[]
}) => (
<div data-testid="role-card">
<div>{title}</div>
<div>{description}</div>
<div>{permissionsByResource.length} permissions</div>
</div>
),
}));
const renderTeamManager = () => renderWrapper(<ToastManagerProvider><LibrariesTeamManager /></ToastManagerProvider>);
describe('LibrariesTeamManager', () => {
const libraryData = {
id: 'lib-001',
title: 'Test Library',
org: 'Test Org',
allowPublicRead: false,
};
const mutate = jest.fn();
const libraryAuthZContext = {
libraryId: libraryData.id,
libraryName: libraryData.title,
libraryOrg: libraryData.org,
username: 'mockuser',
roles: [
{
name: 'Instructor',
description: 'Can manage content.',
userCount: 3,
permissions: ['view', 'edit'],
},
],
permissions: [
{ key: CONTENT_LIBRARY_PERMISSIONS.VIEW_LIBRARY, label: 'view', resource: 'library' },
{ key: CONTENT_LIBRARY_PERMISSIONS.EDIT_LIBRARY_COLLECTION, label: 'edit', resource: 'library' },
],
resources: [{ key: 'library', label: 'Library' }],
canManageTeam: true,
};
beforeEach(() => {
initializeMockApp({
authenticatedUser: {
username: 'admin',
},
});
jest.resetAllMocks();
mockedUseLibraryAuthZ.mockReturnValue(libraryAuthZContext);
(useLibrary as jest.Mock).mockReturnValue({
data: libraryData,
});
(useUpdateLibrary as jest.Mock).mockReturnValue({
mutate,
isPending: false,
});
});
it('renders tabs and layout content correctly', () => {
renderTeamManager();
// Tabs
expect(screen.getByRole('tab', { name: /Team Members/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /Roles/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /Permissions/i })).toBeInTheDocument();
// Breadcrumb/page title
expect(screen.getByText('Manage Access')).toBeInTheDocument(); // from intl.formatMessage
expect(screen.getByText('lib-001')).toBeInTheDocument(); // subtitle
// TeamTable is rendered
expect(screen.getByTestId('team-table')).toBeInTheDocument();
// AddNewTeamMemberTrigger is rendered
expect(screen.getByTestId('add-team-member-trigger')).toBeInTheDocument();
});
it('renders role cards when "Roles" tab is selected', async () => {
const user = userEvent.setup();
renderTeamManager();
// Click on "Roles" tab
const rolesTab = await screen.findByRole('tab', { name: /roles/i });
await user.click(rolesTab);
const roleCards = await screen.findAllByTestId('role-card');
const rolesScope = within(roleCards[0]);
expect(roleCards.length).toBe(1);
expect(rolesScope.getByText('Instructor')).toBeInTheDocument();
expect(screen.getByText(/Can manage content/i)).toBeInTheDocument();
expect(screen.getByText(/1 permissions/i)).toBeInTheDocument();
});
it('renders role matrix when "Permissions" tab is selected', async () => {
const user = userEvent.setup();
renderTeamManager();
// Click on "Permissions" tab
const permissionsTab = await screen.findByRole('tab', { name: /permissions/i });
await user.click(permissionsTab);
const tablePermissionMatrix = await screen.getByRole('table');
const matrixScope = within(tablePermissionMatrix);
expect(matrixScope.getByText('Library')).toBeInTheDocument();
expect(matrixScope.getByText('Instructor')).toBeInTheDocument();
expect(matrixScope.getByText('edit')).toBeInTheDocument();
expect(matrixScope.getByText('view')).toBeInTheDocument();
});
it('renders allow public library read toggle and change the value by user interaction', async () => {
const user = userEvent.setup();
renderTeamManager();
const readPublicToggle = await screen.findByRole('switch', { name: /Allow public read/i });
await user.click(readPublicToggle);
expect(mutate).toHaveBeenCalledWith(
{
libraryId: 'lib-001',
updatedData: { allowPublicRead: !libraryData.allowPublicRead },
},
expect.objectContaining({
onSuccess: expect.any(Function),
}),
);
const { onSuccess } = (mutate as jest.Mock).mock.calls[0][1];
onSuccess?.();
expect(await screen.findByText(/updated successfully/i)).toBeInTheDocument();
});
it('should not render the toggle if the user can not manage team and the Library Public Read is disabled', () => {
(useLibrary as jest.Mock).mockReturnValue({ data: { ...libraryData, allowPublicRead: false } });
(useLibraryAuthZ as jest.Mock).mockReturnValue({ ...libraryAuthZContext, canManageTeam: false });
renderTeamManager();
expect(screen.queryByRole('switch', { name: /Allow public read/i })).not.toBeInTheDocument();
});
it('should render the toggle as disabled if the user can not manage team but the Library Public Read is enabled', async () => {
(useLibrary as jest.Mock).mockReturnValue({ data: { ...libraryData, allowPublicRead: true } });
(useLibraryAuthZ as jest.Mock).mockReturnValue({ ...libraryAuthZContext, canManageTeam: false });
renderTeamManager();
const readPublicToggle = await screen.findByRole('switch', { name: /Allow public read/i });
expect(readPublicToggle).toBeInTheDocument();
expect(readPublicToggle).toBeDisabled();
});
});