@@ -14,14 +14,22 @@ vi.mock('@flareapp/js', () => ({
1414 } ,
1515} ) ) ;
1616
17+ function createMockRouter ( route : Record < string , unknown > ) {
18+ return { currentRoute : { value : route } } ;
19+ }
20+
1721function createMockApp ( options ?: {
1822 errorHandler ?: ( ...args : unknown [ ] ) => void ;
1923 warnHandler ?: ( ...args : unknown [ ] ) => void ;
24+ router ?: ReturnType < typeof createMockRouter > ;
2025} ) {
2126 return {
2227 config : {
2328 errorHandler : options ?. errorHandler ?? undefined ,
2429 warnHandler : options ?. warnHandler ?? undefined ,
30+ globalProperties : {
31+ $router : options ?. router ?? undefined ,
32+ } as Record < string , unknown > ,
2533 } ,
2634 } ;
2735}
@@ -636,3 +644,81 @@ describe('flareVue captureWarnings', () => {
636644 expect ( mockReportMessage . mock . calls [ 1 ] [ 0 ] ) . toBe ( 'Second warning' ) ;
637645 } ) ;
638646} ) ;
647+
648+ describe ( 'flareVue route context' , ( ) => {
649+ const mockRoute = {
650+ name : 'user-profile' ,
651+ path : '/users/42' ,
652+ fullPath : '/users/42?tab=settings' ,
653+ params : { id : '42' } ,
654+ query : { tab : 'settings' } ,
655+ hash : '' ,
656+ matched : [ { name : 'AppLayout' } , { name : 'UserProfile' } ] ,
657+ } ;
658+
659+ test ( 'includes route context when Vue Router is present' , ( ) => {
660+ const app = createMockApp ( { router : createMockRouter ( mockRoute ) } ) ;
661+ ( flareVue as Function ) ( app ) ;
662+
663+ callHandler ( app , new Error ( 'test' ) , null , 'setup function' ) ;
664+
665+ const context = mockReport . mock . calls [ 0 ] [ 1 ] ;
666+ expect ( context . vue . route ) . toEqual ( {
667+ name : 'user-profile' ,
668+ path : '/users/42' ,
669+ fullPath : '/users/42?tab=settings' ,
670+ params : { id : '42' } ,
671+ query : { tab : 'settings' } ,
672+ hash : '' ,
673+ matched : [ 'AppLayout' , 'UserProfile' ] ,
674+ } ) ;
675+ } ) ;
676+
677+ test ( 'does not include route context when Vue Router is not present' , ( ) => {
678+ const app = createMockApp ( ) ;
679+ ( flareVue as Function ) ( app ) ;
680+
681+ callHandler ( app , new Error ( 'test' ) , null , 'setup function' ) ;
682+
683+ const context = mockReport . mock . calls [ 0 ] [ 1 ] ;
684+ expect ( context . vue . route ) . toBeUndefined ( ) ;
685+ } ) ;
686+
687+ test ( 'includes route context in warning reports when Vue Router is present' , ( ) => {
688+ const app = createMockApp ( { router : createMockRouter ( mockRoute ) } ) ;
689+ ( flareVue as Function ) ( app , { captureWarnings : true } satisfies FlareVueOptions ) ;
690+
691+ app . config . warnHandler ! ( 'Invalid prop' , null , 'trace' ) ;
692+
693+ const context = mockReportMessage . mock . calls [ 0 ] [ 1 ] ;
694+ expect ( context . vue . route ) . toEqual ( {
695+ name : 'user-profile' ,
696+ path : '/users/42' ,
697+ fullPath : '/users/42?tab=settings' ,
698+ params : { id : '42' } ,
699+ query : { tab : 'settings' } ,
700+ hash : '' ,
701+ matched : [ 'AppLayout' , 'UserProfile' ] ,
702+ } ) ;
703+ } ) ;
704+
705+ test ( 'does not include route context in warning reports when Vue Router is not present' , ( ) => {
706+ const app = createMockApp ( ) ;
707+ ( flareVue as Function ) ( app , { captureWarnings : true } satisfies FlareVueOptions ) ;
708+
709+ app . config . warnHandler ! ( 'Warning' , null , '' ) ;
710+
711+ const context = mockReportMessage . mock . calls [ 0 ] [ 1 ] ;
712+ expect ( context . vue . route ) . toBeUndefined ( ) ;
713+ } ) ;
714+
715+ test ( 'route context is available to beforeSubmit hook' , ( ) => {
716+ const beforeSubmit = vi . fn ( ( { context } : { context : FlareVueContext } ) => context ) ;
717+ const app = createMockApp ( { router : createMockRouter ( mockRoute ) } ) ;
718+ ( flareVue as Function ) ( app , { beforeSubmit } satisfies FlareVueOptions ) ;
719+
720+ callHandler ( app , new Error ( 'test' ) , null , 'setup function' ) ;
721+
722+ expect ( beforeSubmit . mock . calls [ 0 ] [ 0 ] . context . vue . route ?. name ) . toBe ( 'user-profile' ) ;
723+ } ) ;
724+ } ) ;
0 commit comments