|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import { useRoutes } from './useRoutes'; |
| 3 | +import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; |
| 4 | +import { useInstanceStatus } from 'hooks/api/getters/useInstanceStatus/useInstanceStatus'; |
| 5 | +import { type Mock, vi } from 'vitest'; |
| 6 | + |
| 7 | +vi.mock('hooks/api/getters/useUiConfig/useUiConfig'); |
| 8 | +vi.mock('hooks/api/getters/useInstanceStatus/useInstanceStatus'); |
| 9 | +vi.mock('component/menu/routes', () => ({ |
| 10 | + getNavRoutes: () => [ |
| 11 | + { path: '/features', title: 'Features', menu: { main: true } }, |
| 12 | + { |
| 13 | + path: '/enterprise', |
| 14 | + title: 'Enterprise', |
| 15 | + menu: { main: true, mode: ['enterprise'] }, |
| 16 | + }, |
| 17 | + { path: '/pro', title: 'Pro', menu: { main: true, mode: ['pro'] } }, |
| 18 | + { |
| 19 | + path: '/billing', |
| 20 | + title: 'Billing', |
| 21 | + menu: { main: true, billing: true }, |
| 22 | + }, |
| 23 | + { |
| 24 | + path: '/flagged-enterprise', |
| 25 | + title: 'Flagged Enterprise', |
| 26 | + menu: { main: true, mode: ['enterprise'] }, |
| 27 | + flag: 'someFeatureFlag', |
| 28 | + }, |
| 29 | + ], |
| 30 | + getPrimaryRoutes: () => [ |
| 31 | + { path: '/overview', title: 'Overview', menu: { primary: true } }, |
| 32 | + { |
| 33 | + path: '/admin', |
| 34 | + title: 'Admin', |
| 35 | + menu: { primary: true, mode: ['enterprise'] }, |
| 36 | + }, |
| 37 | + ], |
| 38 | +})); |
| 39 | + |
| 40 | +describe('useRoutes', () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.resetAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + test('filters routes based on enterprise access', () => { |
| 46 | + (useUiConfig as Mock).mockReturnValue({ |
| 47 | + uiConfig: { flags: {} }, |
| 48 | + isEnterprise: () => true, |
| 49 | + isPro: () => false, |
| 50 | + }); |
| 51 | + (useInstanceStatus as Mock).mockReturnValue({ |
| 52 | + isBilling: false, |
| 53 | + }); |
| 54 | + |
| 55 | + const { result } = renderHook(() => useRoutes()); |
| 56 | + |
| 57 | + expect(result.current.routes.mainNavRoutes).toHaveLength(2); |
| 58 | + expect(result.current.routes.mainNavRoutes[0].path).toBe('/features'); |
| 59 | + expect(result.current.routes.mainNavRoutes[1].path).toBe('/enterprise'); |
| 60 | + }); |
| 61 | + |
| 62 | + test('filters routes based on pro access (still shows enterprise routes with badge)', () => { |
| 63 | + (useUiConfig as Mock).mockReturnValue({ |
| 64 | + uiConfig: { flags: {} }, |
| 65 | + isEnterprise: () => false, |
| 66 | + isPro: () => true, |
| 67 | + }); |
| 68 | + (useInstanceStatus as Mock).mockReturnValue({ |
| 69 | + isBilling: false, |
| 70 | + }); |
| 71 | + |
| 72 | + const { result } = renderHook(() => useRoutes()); |
| 73 | + |
| 74 | + expect(result.current.routes.mainNavRoutes).toHaveLength(3); |
| 75 | + expect(result.current.routes.mainNavRoutes[0].path).toBe('/features'); |
| 76 | + expect(result.current.routes.mainNavRoutes[1].path).toBe('/enterprise'); |
| 77 | + expect(result.current.routes.mainNavRoutes[2].path).toBe('/pro'); |
| 78 | + }); |
| 79 | + |
| 80 | + test('filters routes based on billing access', () => { |
| 81 | + (useUiConfig as Mock).mockReturnValue({ |
| 82 | + uiConfig: { flags: {} }, |
| 83 | + isEnterprise: () => false, |
| 84 | + isPro: () => false, |
| 85 | + }); |
| 86 | + (useInstanceStatus as Mock).mockReturnValue({ |
| 87 | + isBilling: true, |
| 88 | + }); |
| 89 | + |
| 90 | + const { result } = renderHook(() => useRoutes()); |
| 91 | + |
| 92 | + expect(result.current.routes.mainNavRoutes).toHaveLength(2); |
| 93 | + expect(result.current.routes.mainNavRoutes[0].path).toBe('/features'); |
| 94 | + expect(result.current.routes.mainNavRoutes[1].path).toBe('/billing'); |
| 95 | + }); |
| 96 | + |
| 97 | + test('filters primary routes based on enterprise access', () => { |
| 98 | + (useUiConfig as Mock).mockReturnValue({ |
| 99 | + uiConfig: { flags: {} }, |
| 100 | + isEnterprise: () => true, |
| 101 | + isPro: () => false, |
| 102 | + }); |
| 103 | + (useInstanceStatus as Mock).mockReturnValue({ |
| 104 | + isBilling: false, |
| 105 | + }); |
| 106 | + |
| 107 | + const { result } = renderHook(() => useRoutes()); |
| 108 | + |
| 109 | + expect(result.current.routes.primaryRoutes).toHaveLength(2); |
| 110 | + expect(result.current.routes.primaryRoutes[0].path).toBe('/overview'); |
| 111 | + expect(result.current.routes.primaryRoutes[1].path).toBe('/admin'); |
| 112 | + }); |
| 113 | + |
| 114 | + test('filters primary routes without enterprise access', () => { |
| 115 | + (useUiConfig as Mock).mockReturnValue({ |
| 116 | + uiConfig: { flags: {} }, |
| 117 | + isEnterprise: () => false, |
| 118 | + isPro: () => false, |
| 119 | + }); |
| 120 | + (useInstanceStatus as Mock).mockReturnValue({ |
| 121 | + isBilling: false, |
| 122 | + }); |
| 123 | + |
| 124 | + const { result } = renderHook(() => useRoutes()); |
| 125 | + |
| 126 | + expect(result.current.routes.primaryRoutes).toHaveLength(1); |
| 127 | + expect(result.current.routes.primaryRoutes[0].path).toBe('/overview'); |
| 128 | + }); |
| 129 | + |
| 130 | + test('does not show enterprise routes if not enterprise, even if feature flag is enabled', () => { |
| 131 | + (useUiConfig as Mock).mockReturnValue({ |
| 132 | + uiConfig: { flags: { someFeatureFlag: true } }, |
| 133 | + isEnterprise: () => false, |
| 134 | + isPro: () => false, |
| 135 | + }); |
| 136 | + (useInstanceStatus as Mock).mockReturnValue({ |
| 137 | + isBilling: false, |
| 138 | + }); |
| 139 | + |
| 140 | + const { result } = renderHook(() => useRoutes()); |
| 141 | + |
| 142 | + expect(result.current.routes.mainNavRoutes).toHaveLength(1); |
| 143 | + expect(result.current.routes.mainNavRoutes[0].path).toBe('/features'); |
| 144 | + expect( |
| 145 | + result.current.routes.mainNavRoutes.find( |
| 146 | + (r) => r.path === '/flagged-enterprise', |
| 147 | + ), |
| 148 | + ).toBeUndefined(); |
| 149 | + }); |
| 150 | + |
| 151 | + test('shows enterprise routes with enabled feature flags when enterprise', () => { |
| 152 | + (useUiConfig as Mock).mockReturnValue({ |
| 153 | + uiConfig: { flags: { someFeatureFlag: true } }, |
| 154 | + isEnterprise: () => true, |
| 155 | + isPro: () => false, |
| 156 | + }); |
| 157 | + (useInstanceStatus as Mock).mockReturnValue({ |
| 158 | + isBilling: false, |
| 159 | + }); |
| 160 | + |
| 161 | + const { result } = renderHook(() => useRoutes()); |
| 162 | + |
| 163 | + expect(result.current.routes.mainNavRoutes).toHaveLength(3); |
| 164 | + expect(result.current.routes.mainNavRoutes[0].path).toBe('/features'); |
| 165 | + expect(result.current.routes.mainNavRoutes[1].path).toBe('/enterprise'); |
| 166 | + expect(result.current.routes.mainNavRoutes[2].path).toBe( |
| 167 | + '/flagged-enterprise', |
| 168 | + ); |
| 169 | + }); |
| 170 | +}); |
0 commit comments