11import { screen } from '@testing-library/react' ;
22import userEvent from '@testing-library/user-event' ;
33import { renderWrapper } from '@src/setupTest' ;
4+ import { useValidateUserPermissionsNonSuspense } from '@src/data/hooks' ;
5+ import { CONTENT_COURSE_PERMISSIONS , CONTENT_LIBRARY_PERMISSIONS } from '@src/authz-module/roles-permissions' ;
46import ScopesFilter from './ScopesFilter' ;
57
8+ jest . mock ( '@src/data/hooks' , ( ) => ( {
9+ useValidateUserPermissionsNonSuspense : jest . fn ( ) ,
10+ } ) ) ;
11+
12+ const mockUsePermissions = useValidateUserPermissionsNonSuspense as jest . Mock ;
13+
614jest . mock ( '@src/authz-module/data/hooks' , ( ) => ( {
7- useScopes : ( ) => ( {
15+ useScopes : jest . fn ( ( ) => ( {
816 data : {
917 pages : [
1018 {
1119 results : [
1220 {
13- externalKey : 'course:123 ' ,
14- name : 'Test Course' ,
15- organization : { name : 'Test Org ' } ,
21+ externalKey : 'course-v1:org+course+run ' ,
22+ displayName : 'Test Course' ,
23+ org : { shortName : 'TestOrg ' } ,
1624 } ,
1725 {
18- externalKey : 'library:456 ' ,
19- name : 'Test Library' ,
20- organization : { name : 'Another Org ' } ,
26+ externalKey : 'lib:org:library ' ,
27+ displayName : 'Test Library' ,
28+ org : { shortName : 'TestOrg ' } ,
2129 } ,
2230 ] ,
2331 } ,
2432 ] ,
2533 } ,
26- } ) ,
34+ } ) ) ,
2735} ) ) ;
2836
37+ // eslint-disable-next-line @typescript-eslint/no-var-requires
38+ const mockUseScopes = require ( '@src/authz-module/data/hooks' ) . useScopes as jest . Mock ;
39+
40+ const permissionsData = ( { library, course } : { library ?: boolean ; course ?: boolean } ) => [
41+ { action : CONTENT_LIBRARY_PERMISSIONS . VIEW_LIBRARY_TEAM , allowed : ! ! library } ,
42+ { action : CONTENT_COURSE_PERMISSIONS . VIEW_COURSE_TEAM , allowed : ! ! course } ,
43+ ] ;
44+
2945describe ( 'ScopesFilter' , ( ) => {
3046 const defaultProps = {
3147 filterButtonText : 'Scopes' ,
@@ -36,6 +52,7 @@ describe('ScopesFilter', () => {
3652
3753 beforeEach ( ( ) => {
3854 jest . clearAllMocks ( ) ;
55+ mockUsePermissions . mockReturnValue ( { data : permissionsData ( { library : true , course : true } ) } ) ;
3956 } ) ;
4057
4158 it ( 'renders without crashing' , ( ) => {
@@ -68,4 +85,27 @@ describe('ScopesFilter', () => {
6885 renderWrapper ( < ScopesFilter { ...defaultProps } setFilter = { mockSetFilter } /> ) ;
6986 expect ( screen . getByText ( 'Scopes' ) ) . toBeInTheDocument ( ) ;
7087 } ) ;
88+
89+ it ( 'fetches all scope types when the user can view courses' , ( ) => {
90+ renderWrapper ( < ScopesFilter { ...defaultProps } /> ) ;
91+ expect ( mockUseScopes ) . toHaveBeenCalledWith (
92+ expect . not . objectContaining ( { scopeType : 'library' } ) ,
93+ ) ;
94+ } ) ;
95+
96+ it ( 'fetches only library scopes when the user cannot view courses' , ( ) => {
97+ mockUsePermissions . mockReturnValue ( { data : permissionsData ( { library : true , course : false } ) } ) ;
98+ renderWrapper ( < ScopesFilter { ...defaultProps } /> ) ;
99+ expect ( mockUseScopes ) . toHaveBeenCalledWith (
100+ expect . objectContaining ( { scopeType : 'library' } ) ,
101+ ) ;
102+ } ) ;
103+
104+ it ( 'defaults to showing all scopes while permissions are loading' , ( ) => {
105+ mockUsePermissions . mockReturnValue ( { data : undefined } ) ;
106+ renderWrapper ( < ScopesFilter { ...defaultProps } /> ) ;
107+ expect ( mockUseScopes ) . toHaveBeenCalledWith (
108+ expect . not . objectContaining ( { scopeType : 'library' } ) ,
109+ ) ;
110+ } ) ;
71111} ) ;
0 commit comments