77
88import { test , expect , describe } from 'vitest' ;
99import { routes , routesInHeader , routesInSideNav } from '../routes/config' ;
10- import Dashboard from '../pages/dashboard/Dashboard' ;
11- import NotFound from '../pages/not-found/NotFound' ;
12- import Placeholder from '../pages/placeholder/Placeholder' ;
13- import Welcome from '../pages/welcome/Welcome' ;
1410
1511describe ( 'routes configuration' , ( ) => {
1612 test ( 'routes array contains expected structure' , ( ) => {
@@ -22,18 +18,22 @@ describe('routes configuration', () => {
2218 const indexRoute = routes . find ( ( route ) => route . index === true ) ;
2319 expect ( indexRoute ) . toBeDefined ( ) ;
2420 expect ( indexRoute . path ) . toBe ( '/' ) ;
25- expect ( indexRoute . element ) . toBe ( Welcome ) ;
21+ expect ( indexRoute . element ) . toBeDefined ( ) ;
22+ // Check for lazy component (has $$typeof and _payload properties)
23+ expect ( indexRoute . element . $$typeof ) . toBeDefined ( ) ;
2624
2725 // Check that the NotFound route is defined correctly
2826 const notFoundRoute = routes . find ( ( route ) => route . path === '*' ) ;
2927 expect ( notFoundRoute ) . toBeDefined ( ) ;
30- expect ( notFoundRoute . element ) . toBe ( NotFound ) ;
28+ expect ( notFoundRoute . element ) . toBeDefined ( ) ;
29+ expect ( notFoundRoute . element . $$typeof ) . toBeDefined ( ) ;
3130 expect ( notFoundRoute . status ) . toBe ( 404 ) ;
3231
3332 // Check that a regular route is defined correctly
3433 const dashboardRoute = routes . find ( ( route ) => route . path === '/dashboard' ) ;
3534 expect ( dashboardRoute ) . toBeDefined ( ) ;
36- expect ( dashboardRoute . element ) . toBe ( Dashboard ) ;
35+ expect ( dashboardRoute . element ) . toBeDefined ( ) ;
36+ expect ( dashboardRoute . element . $$typeof ) . toBeDefined ( ) ;
3737 expect ( dashboardRoute . carbon ) . toBeDefined ( ) ;
3838 expect ( dashboardRoute . carbon . label ) . toBe ( 'Dashboard' ) ;
3939 expect ( dashboardRoute . carbon . inHeader ) . toBe ( true ) ;
@@ -112,4 +112,80 @@ describe('routes configuration', () => {
112112 }
113113 } ) ;
114114 } ) ;
115+
116+ test ( 'route components are lazy-loaded for code splitting' , ( ) => {
117+ // Get all routes with elements
118+ const routesWithElements = routes . filter ( ( route ) => route . element ) ;
119+
120+ expect ( routesWithElements . length ) . toBeGreaterThan ( 0 ) ;
121+
122+ routesWithElements . forEach ( ( route ) => {
123+ const element = route . element ;
124+
125+ // Verify it's a lazy component by checking React's internal structure
126+ // Lazy components have $$typeof symbol and _payload/_init properties
127+ expect ( element ) . toBeDefined ( ) ;
128+ expect ( element . $$typeof ) . toBeDefined ( ) ;
129+ expect ( typeof element . $$typeof ) . toBe ( 'symbol' ) ;
130+
131+ // Verify the lazy component has the expected structure
132+ // _payload contains the import promise, _init is the initialization function
133+ expect ( element . _payload ) . toBeDefined ( ) ;
134+ expect ( element . _init ) . toBeDefined ( ) ;
135+ expect ( typeof element . _init ) . toBe ( 'function' ) ;
136+ } ) ;
137+ } ) ;
138+
139+ test ( 'lazy components have correct React.lazy structure' , ( ) => {
140+ // Test that lazy components have the expected React.lazy structure
141+ const indexRoute = routes . find ( ( route ) => route . index === true ) ;
142+ expect ( indexRoute ) . toBeDefined ( ) ;
143+ expect ( indexRoute . element ) . toBeDefined ( ) ;
144+
145+ const lazyComponent = indexRoute . element ;
146+
147+ // Verify it has the React lazy component structure
148+ expect ( lazyComponent . _payload ) . toBeDefined ( ) ;
149+ expect ( lazyComponent . _init ) . toBeDefined ( ) ;
150+ expect ( typeof lazyComponent . _init ) . toBe ( 'function' ) ;
151+
152+ // Verify the $$typeof is the REACT_LAZY_TYPE symbol
153+ expect ( lazyComponent . $$typeof ) . toBeDefined ( ) ;
154+ expect ( typeof lazyComponent . $$typeof ) . toBe ( 'symbol' ) ;
155+ expect ( String ( lazyComponent . $$typeof ) ) . toContain ( 'react.lazy' ) ;
156+ } ) ;
157+
158+ test ( 'lazy components can be dynamically imported' , async ( ) => {
159+ // Test that lazy components point to valid importable modules
160+ // by directly importing the component files
161+ const componentImports = {
162+ '/' : ( ) => import ( '../pages/welcome/Welcome' ) ,
163+ '/dashboard' : ( ) => import ( '../pages/dashboard/Dashboard' ) ,
164+ '*' : ( ) => import ( '../pages/not-found/NotFound' ) ,
165+ } ;
166+
167+ for ( const [ path , importFn ] of Object . entries ( componentImports ) ) {
168+ const route = routes . find ( ( r ) => r . path === path ) ;
169+ expect ( route ) . toBeDefined ( ) ;
170+ expect ( route . element ) . toBeDefined ( ) ;
171+
172+ // Verify the route has a lazy component
173+ const lazyComponent = route . element ;
174+ expect ( lazyComponent . $$typeof ) . toBeDefined ( ) ;
175+ expect ( String ( lazyComponent . $$typeof ) ) . toContain ( 'react.lazy' ) ;
176+
177+ // Verify we can actually import the component module
178+ const module = await importFn ( ) ;
179+ expect ( module ) . toBeDefined ( ) ;
180+ expect ( module . default ) . toBeDefined ( ) ;
181+ expect ( typeof module . default ) . toBe ( 'function' ) ;
182+
183+ // Verify it's a valid React component
184+ const component = module . default ;
185+ const componentName = component . displayName || component . name ;
186+ expect ( componentName ) . toBeDefined ( ) ;
187+ expect ( typeof componentName ) . toBe ( 'string' ) ;
188+ expect ( componentName . length ) . toBeGreaterThan ( 0 ) ;
189+ }
190+ } ) ;
115191} ) ;
0 commit comments