15
15
*/
16
16
import React from 'react' ;
17
17
18
- import {
19
- RequirePermission ,
20
- usePermission ,
21
- } from '@backstage/plugin-permission-react' ;
22
- import { renderInTestApp } from '@backstage/test-utils' ;
18
+ import { renderInTestApp , TestApiProvider } from '@backstage/test-utils' ;
23
19
24
20
import { screen } from '@testing-library/react' ;
25
21
26
22
import { useCheckIfLicensePluginEnabled } from '../hooks/useCheckIfLicensePluginEnabled' ;
27
23
import { useRoles } from '../hooks/useRoles' ;
28
24
import { RbacPage } from './RbacPage' ;
29
25
26
+ import { RBACAPI , rbacApiRef } from '../api/RBACBackendClient' ;
27
+
30
28
jest . mock ( '@backstage/plugin-permission-react' , ( ) => ( {
31
29
usePermission : jest . fn ( ) ,
32
30
RequirePermission : jest . fn ( ) ,
@@ -40,25 +38,33 @@ jest.mock('../hooks/useCheckIfLicensePluginEnabled', () => ({
40
38
useCheckIfLicensePluginEnabled : jest . fn ( ) ,
41
39
} ) ) ;
42
40
43
- const mockUsePermission = usePermission as jest . MockedFunction <
44
- typeof usePermission
45
- > ;
46
-
47
41
const mockUseRoles = useRoles as jest . MockedFunction < typeof useRoles > ;
48
42
49
43
const mockUseCheckIfLicensePluginEnabled =
50
44
useCheckIfLicensePluginEnabled as jest . MockedFunction <
51
45
typeof useCheckIfLicensePluginEnabled
52
46
> ;
53
47
54
- const RequirePermissionMock = RequirePermission as jest . MockedFunction <
55
- typeof RequirePermission
56
- > ;
48
+ // Added
49
+ let useAsyncMockResult : { loading : boolean ; value ?: { status : string } } = {
50
+ loading : false ,
51
+ value : { status : 'Authorized' } ,
52
+ } ;
53
+
54
+ const mockRbacApi : jest . Mocked < Partial < RBACAPI > > = {
55
+ getUserAuthorization : jest . fn ( ) . mockImplementation ( ( ) => useAsyncMockResult ) ,
56
+ } ;
57
+
58
+ jest . mock ( 'react-use' , ( ) => ( {
59
+ ...jest . requireActual ( 'react-use' ) ,
60
+ useAsync : jest . fn ( ) . mockImplementation ( ( fn : any , _deps : any ) => {
61
+ fn ( ) ;
62
+ return useAsyncMockResult ;
63
+ } ) ,
64
+ } ) ) ;
57
65
58
66
describe ( 'RbacPage' , ( ) => {
59
67
it ( 'should render if authorized' , async ( ) => {
60
- RequirePermissionMock . mockImplementation ( props => < > { props . children } </ > ) ;
61
- mockUsePermission . mockReturnValue ( { loading : false , allowed : true } ) ;
62
68
mockUseRoles . mockReturnValue ( {
63
69
loading : true ,
64
70
data : [ ] ,
@@ -79,23 +85,40 @@ describe('RbacPage', () => {
79
85
name : '' ,
80
86
} ,
81
87
} ) ;
82
- await renderInTestApp ( < RbacPage /> ) ;
88
+
89
+ await renderInTestApp (
90
+ < TestApiProvider apis = { [ [ rbacApiRef , mockRbacApi ] ] } >
91
+ < RbacPage />
92
+ </ TestApiProvider > ,
93
+ ) ;
83
94
expect ( screen . getByText ( 'RBAC' ) ) . toBeInTheDocument ( ) ;
84
95
} ) ;
85
96
86
97
it ( 'should not render if not authorized' , async ( ) => {
87
- RequirePermissionMock . mockImplementation ( _props => < > Not Found</ > ) ;
88
- mockUsePermission . mockReturnValue ( { loading : false , allowed : false } ) ;
98
+ useAsyncMockResult = {
99
+ loading : false ,
100
+ value : { status : 'Unauthorized' } ,
101
+ } ;
89
102
90
- await renderInTestApp ( < RbacPage /> ) ;
91
- expect ( screen . getByText ( 'Not Found' ) ) . toBeInTheDocument ( ) ;
103
+ await renderInTestApp (
104
+ < TestApiProvider apis = { [ [ rbacApiRef , mockRbacApi ] ] } >
105
+ < RbacPage />
106
+ </ TestApiProvider > ,
107
+ ) ;
108
+ expect ( screen . getByText ( 'ERROR : Not Found' ) ) . toBeInTheDocument ( ) ;
92
109
} ) ;
93
110
94
111
it ( 'should not render if loading' , async ( ) => {
95
- RequirePermissionMock . mockImplementation ( _props => null ) ;
96
- mockUsePermission . mockReturnValue ( { loading : false , allowed : false } ) ;
112
+ useAsyncMockResult = {
113
+ loading : true ,
114
+ value : undefined ,
115
+ } ;
97
116
98
- const { queryByText } = await renderInTestApp ( < RbacPage /> ) ;
117
+ const { queryByText } = await renderInTestApp (
118
+ < TestApiProvider apis = { [ [ rbacApiRef , mockRbacApi ] ] } >
119
+ < RbacPage />
120
+ </ TestApiProvider > ,
121
+ ) ;
99
122
expect ( queryByText ( 'Not Found' ) ) . not . toBeInTheDocument ( ) ;
100
123
expect ( queryByText ( 'RBAC' ) ) . not . toBeInTheDocument ( ) ;
101
124
} ) ;
0 commit comments