@@ -2,24 +2,22 @@ import { renderHook } from '@testing-library/react-native';
22
33import { useAuth } from '@/context/AuthContext' ;
44import { useFilteredNavigation } from '@/hooks/useFilteredNavigation' ;
5- import { canShowNavItem } from '@/utils/navigationPermissions ' ;
5+ import { hasModuleAccess } from '@/utils/moduleClaims ' ;
66
77// Mock dependencies
88jest . mock ( '@/context/AuthContext' , ( ) => ( {
99 useAuth : jest . fn ( ) ,
1010} ) ) ;
1111
12- jest . mock ( '@/utils/navigationPermissions ' , ( ) => ( {
13- canShowNavItem : jest . fn ( ) ,
12+ jest . mock ( '@/utils/moduleClaims ' , ( ) => ( {
13+ hasModuleAccess : jest . fn ( ) ,
1414} ) ) ;
1515
16- jest . mock ( '@/config/navigation-mapper.json' , ( ) => ( { } ) ) ;
17-
1816describe ( 'useFilteredNavigation' , ( ) => {
1917 const mockItems = [
20- { name : 'home' , component : jest . fn ( ) } ,
21- { name : 'orders' , component : jest . fn ( ) } ,
22- { name : 'billing' , component : jest . fn ( ) } ,
18+ { name : 'home' , component : jest . fn ( ) , modules : [ 'new-marketplace' ] } ,
19+ { name : 'orders' , component : jest . fn ( ) , modules : [ 'new-marketplace' ] , roles : [ 'Client' ] } ,
20+ { name : 'billing' , component : jest . fn ( ) , modules : [ 'billing' ] , roles : [ 'Client' ] } ,
2321 ] ;
2422
2523 beforeEach ( ( ) => {
@@ -35,7 +33,7 @@ describe('useFilteredNavigation', () => {
3533 const { result } = renderHook ( ( ) => useFilteredNavigation ( mockItems ) ) ;
3634
3735 expect ( result . current ) . toEqual ( mockItems ) ;
38- expect ( canShowNavItem ) . not . toHaveBeenCalled ( ) ;
36+ expect ( hasModuleAccess ) . not . toHaveBeenCalled ( ) ;
3937 } ) ;
4038
4139 it ( 'should filter items based on permissions' , ( ) => {
@@ -44,16 +42,15 @@ describe('useFilteredNavigation', () => {
4442 accountType : 'Client' ,
4543 } ) ;
4644
47- ( canShowNavItem as jest . Mock ) . mockImplementation ( ( _token , _accountType , navItemId ) => {
48- return navItemId === 'home' || navItemId === 'orders ';
45+ ( hasModuleAccess as jest . Mock ) . mockImplementation ( ( _token , module ) => {
46+ return module === 'new-marketplace ' ;
4947 } ) ;
5048
5149 const { result } = renderHook ( ( ) => useFilteredNavigation ( mockItems ) ) ;
5250
5351 expect ( result . current ) . toHaveLength ( 2 ) ;
5452 expect ( result . current [ 0 ] . name ) . toBe ( 'home' ) ;
5553 expect ( result . current [ 1 ] . name ) . toBe ( 'orders' ) ;
56- expect ( canShowNavItem ) . toHaveBeenCalledTimes ( 3 ) ;
5754 } ) ;
5855
5956 it ( 'should return empty array when no items match permissions' , ( ) => {
@@ -62,42 +59,37 @@ describe('useFilteredNavigation', () => {
6259 accountType : 'Client' ,
6360 } ) ;
6461
65- ( canShowNavItem as jest . Mock ) . mockReturnValue ( false ) ;
62+ ( hasModuleAccess as jest . Mock ) . mockReturnValue ( false ) ;
6663
6764 const { result } = renderHook ( ( ) => useFilteredNavigation ( mockItems ) ) ;
6865
6966 expect ( result . current ) . toEqual ( [ ] ) ;
7067 } ) ;
7168
72- it ( 'should return all items when all match permissions ' , ( ) => {
69+ it ( 'should filter by role when user does not have required role ' , ( ) => {
7370 ( useAuth as jest . Mock ) . mockReturnValue ( {
7471 tokens : { accessToken : 'mock.token' } ,
75- accountType : 'Client ' ,
72+ accountType : 'Operations ' ,
7673 } ) ;
7774
78- ( canShowNavItem as jest . Mock ) . mockReturnValue ( true ) ;
75+ ( hasModuleAccess as jest . Mock ) . mockReturnValue ( true ) ;
7976
8077 const { result } = renderHook ( ( ) => useFilteredNavigation ( mockItems ) ) ;
8178
82- expect ( result . current ) . toEqual ( mockItems ) ;
79+ expect ( result . current ) . toHaveLength ( 1 ) ;
80+ expect ( result . current [ 0 ] . name ) . toBe ( 'home' ) ;
8381 } ) ;
8482
85- it ( 'should handle accountType being null' , ( ) => {
83+ it ( 'should handle items without permissions' , ( ) => {
84+ const itemsWithoutPerms = [ { name : 'public' , component : jest . fn ( ) } ] ;
85+
8686 ( useAuth as jest . Mock ) . mockReturnValue ( {
8787 tokens : { accessToken : 'mock.token' } ,
88- accountType : null ,
88+ accountType : 'Client' ,
8989 } ) ;
9090
91- ( canShowNavItem as jest . Mock ) . mockReturnValue ( true ) ;
91+ const { result } = renderHook ( ( ) => useFilteredNavigation ( itemsWithoutPerms ) ) ;
9292
93- const { result } = renderHook ( ( ) => useFilteredNavigation ( mockItems ) ) ;
94-
95- expect ( result . current ) . toEqual ( mockItems ) ;
96- expect ( canShowNavItem ) . toHaveBeenCalledWith (
97- 'mock.token' ,
98- null ,
99- expect . any ( String ) ,
100- expect . any ( Object ) ,
101- ) ;
93+ expect ( result . current ) . toEqual ( itemsWithoutPerms ) ;
10294 } ) ;
10395} ) ;
0 commit comments