@@ -5,6 +5,7 @@ import { initializeMockApp } from '@edx/frontend-platform/testing';
55import { useLibrary , useUpdateLibrary } from '@src/authz-module/data/hooks' ;
66import { useLibraryAuthZ } from './context' ;
77import LibrariesTeamManager from './LibrariesTeamManager' ;
8+ import { ToastManagerProvider } from './ToastManagerContext' ;
89
910jest . 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 > ) ;
4951describe ( '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 : / T e a m M e m b e r s / i } ) ) . toBeInTheDocument ( ) ;
98102 expect ( screen . getByRole ( 'tab' , { name : / R o l e s / 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 : / r o l e s / 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 : / p e r m i s s i o n s / 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 : / A l l o w p u b l i c r e a d / 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 : / A l l o w p u b l i c r e a d / 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 : / A l l o w p u b l i c r e a d / i } ) ;
183+
184+ expect ( readPublicToggle ) . toBeInTheDocument ( ) ;
185+ expect ( readPublicToggle ) . toBeDisabled ( ) ;
159186 } ) ;
160187} ) ;
0 commit comments