1- import { screen } from '@testing-library/react' ;
1+ import { Component , ReactNode } from 'react' ;
2+ import { screen , renderHook } from '@testing-library/react' ;
23import { useParams } from 'react-router-dom' ;
34import { useValidateUserPermissions } from '@src/data/hooks' ;
45import { renderWrapper } from '@src/setupTest' ;
@@ -17,16 +18,31 @@ jest.mock('@src/authz-module/data/hooks', () => ({
1718 data : [
1819 {
1920 role : 'library_author' ,
20- permissions : [
21- 'view_library_team' ,
22- 'edit_library' ,
23- ] ,
21+ permissions : [ 'view_library_team' , 'edit_library' ] ,
2422 user_count : 12 ,
2523 } ,
2624 ] ,
2725 } ) ,
2826} ) ) ;
2927
28+ class ErrorBoundary extends Component < { children : ReactNode } , { hasError : boolean ; error ?: Error } > {
29+ constructor ( props : { children : ReactNode } ) {
30+ super ( props ) ;
31+ this . state = { hasError : false } ;
32+ }
33+
34+ static getDerivedStateFromError ( error : Error ) {
35+ return { hasError : true , error } ;
36+ }
37+
38+ render ( ) {
39+ if ( this . state . hasError && this . state . error ) {
40+ throw this . state . error ;
41+ }
42+ return this . props . children ;
43+ }
44+ }
45+
3046const TestComponent = ( ) => {
3147 const context = useLibraryAuthZ ( ) ;
3248 return (
@@ -35,7 +51,9 @@ const TestComponent = () => {
3551 < div data-testid = "libraryId" > { context . libraryId } </ div >
3652 < div data-testid = "canManageTeam" > { context . canManageTeam ? 'true' : 'false' } </ div >
3753 < div data-testid = "roles" > { Array . isArray ( context . roles ) ? context . roles . length : 'undefined' } </ div >
38- < div data-testid = "permissions" > { Array . isArray ( context . permissions ) ? context . permissions . length : 'undefined' } </ div >
54+ < div data-testid = "permissions" >
55+ { Array . isArray ( context . permissions ) ? context . permissions . length : 'undefined' }
56+ </ div >
3957 < div data-testid = "resources" > { Array . isArray ( context . resources ) ? context . resources . length : 'undefined' } </ div >
4058 </ div >
4159 ) ;
@@ -108,21 +126,18 @@ describe('LibraryAuthZProvider', () => {
108126
109127 expect ( ( ) => {
110128 renderWrapper (
111- < LibraryAuthZProvider >
112- < TestComponent />
113- </ LibraryAuthZProvider > ,
129+ < ErrorBoundary >
130+ < LibraryAuthZProvider >
131+ < TestComponent />
132+ </ LibraryAuthZProvider >
133+ </ ErrorBoundary > ,
114134 ) ;
115135 } ) . toThrow ( 'MissingLibrary' ) ;
116136 } ) ;
117137
118138 it ( 'throws error when useLibraryAuthZ is used outside provider' , ( ) => {
119- const BrokenComponent = ( ) => {
120- useLibraryAuthZ ( ) ;
121- return null ;
122- } ;
123-
124139 expect ( ( ) => {
125- renderWrapper ( < BrokenComponent /> ) ;
140+ renderHook ( ( ) => useLibraryAuthZ ( ) ) ;
126141 } ) . toThrow ( 'useLibraryAuthZ must be used within an LibraryAuthZProvider' ) ;
127142 } ) ;
128143} ) ;
0 commit comments