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 { useScopes } from '@src/authz-module/data/hooks' ;
6+ import { CONTENT_COURSE_PERMISSIONS , CONTENT_LIBRARY_PERMISSIONS } from '@src/authz-module/roles-permissions' ;
47import ScopesFilter from './ScopesFilter' ;
58
9+ jest . mock ( '@src/data/hooks' , ( ) => ( {
10+ useValidateUserPermissionsNonSuspense : jest . fn ( ) ,
11+ } ) ) ;
12+
13+ const mockUsePermissions = useValidateUserPermissionsNonSuspense as jest . Mock ;
14+
615jest . mock ( '@src/authz-module/data/hooks' , ( ) => ( {
7- useScopes : ( ) => ( {
16+ useScopes : jest . fn ( ( ) => ( {
817 data : {
918 pages : [
1019 {
1120 results : [
1221 {
13- externalKey : 'course:123 ' ,
14- name : 'Test Course' ,
15- organization : { name : 'Test Org ' } ,
22+ externalKey : 'course-v1:org+course+run ' ,
23+ displayName : 'Test Course' ,
24+ org : { shortName : 'TestOrg ' } ,
1625 } ,
1726 {
18- externalKey : 'library:456 ' ,
19- name : 'Test Library' ,
20- organization : { name : 'Another Org ' } ,
27+ externalKey : 'lib:org:library ' ,
28+ displayName : 'Test Library' ,
29+ org : { shortName : 'TestOrg ' } ,
2130 } ,
2231 ] ,
2332 } ,
2433 ] ,
2534 } ,
26- } ) ,
35+ } ) ) ,
2736} ) ) ;
2837
38+ const mockUseScopes = 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