1+ import type { UseMdxgRouter , UseMdxgRouterOptions } from '../src/runtime/composables/useMdxgDocumentRouter'
12// @vitest -environment happy-dom
23import type { MdxgDocument , MdxgLinkResolveResult } from '../src/runtime/types'
3- import { describe , expect , it , vi } from 'vitest'
4+ import { afterEach , describe , expect , it , vi } from 'vitest'
5+ import { createApp , defineComponent , h } from 'vue'
46import { useMdxgDocumentRouter } from '../src/runtime/composables/useMdxgDocumentRouter'
57
68function docOf ( slug : string ) : MdxgDocument {
@@ -18,10 +20,29 @@ function result(docId: string, url?: string): MdxgLinkResolveResult {
1820 return { docId, document : docOf ( docId ) , url }
1921}
2022
23+ // The router registers onBeforeUnmount, so it needs a live component instance.
24+ // Each mounted app is unmounted after the test, which removes the popstate listener.
25+ const unmounts : Array < ( ) => void > = [ ]
26+
27+ afterEach ( ( ) => {
28+ unmounts . splice ( 0 ) . forEach ( unmount => unmount ( ) )
29+ } )
30+
31+ function withSetup ( opts : UseMdxgRouterOptions ) : UseMdxgRouter {
32+ let router ! : UseMdxgRouter
33+ const app = createApp ( defineComponent ( ( ) => {
34+ router = useMdxgDocumentRouter ( opts )
35+ return ( ) => h ( 'div' )
36+ } ) )
37+ app . mount ( document . createElement ( 'div' ) )
38+ unmounts . push ( ( ) => app . unmount ( ) )
39+ return router
40+ }
41+
2142describe ( 'useMdxgDocumentRouter' , ( ) => {
2243 it ( 'navigates and updates the current document' , async ( ) => {
2344 const resolver = vi . fn ( async ( ) => result ( 'b' , '/b' ) )
24- const r = useMdxgDocumentRouter ( {
45+ const r = withSetup ( {
2546 resolver,
2647 initial : { docId : 'a' , document : docOf ( 'a' ) , url : '/a' } ,
2748 } )
@@ -34,7 +55,7 @@ describe('useMdxgDocumentRouter', () => {
3455
3556 it ( 'extracts the anchor fragment from the href' , async ( ) => {
3657 const resolver = vi . fn ( async ( ) => result ( 'b' ) )
37- const r = useMdxgDocumentRouter ( {
58+ const r = withSetup ( {
3859 resolver,
3960 initial : { docId : 'a' , document : docOf ( 'a' ) } ,
4061 } )
@@ -57,7 +78,7 @@ describe('useMdxgDocumentRouter', () => {
5778 } ,
5879 } ,
5980 } )
60- const r = useMdxgDocumentRouter ( {
81+ const r = withSetup ( {
6182 resolver : async ( ) => null ,
6283 initial : { docId : 'a' , document : docOf ( 'a' ) } ,
6384 } )
@@ -67,7 +88,7 @@ describe('useMdxgDocumentRouter', () => {
6788 } )
6889
6990 it ( 'surfaces errors without leaving an empty state' , async ( ) => {
70- const r = useMdxgDocumentRouter ( {
91+ const r = withSetup ( {
7192 resolver : async ( ) => { throw new Error ( 'boom' ) } ,
7293 initial : { docId : 'a' , document : docOf ( 'a' ) } ,
7394 } )
@@ -83,14 +104,14 @@ describe('useMdxgDocumentRouter', () => {
83104 await Promise . resolve ( )
84105 return result ( 'b' )
85106 }
86- const r = useMdxgDocumentRouter ( { resolver, initial : { docId : 'a' , document : docOf ( 'a' ) } } )
107+ const r = withSetup ( { resolver, initial : { docId : 'a' , document : docOf ( 'a' ) } } )
87108 await Promise . all ( [ r . navigate ( './b.md' ) , r . navigate ( './b.md' ) , r . prefetch ( './b.md' ) ] )
88109 expect ( calls ) . toBe ( 1 )
89110 } )
90111
91112 it ( 'back() pops the stack' , async ( ) => {
92113 const resolver = vi . fn ( async input => result ( input . href . replace ( './' , '' ) . replace ( '.md' , '' ) ) )
93- const r = useMdxgDocumentRouter ( { resolver, initial : { docId : 'a' , document : docOf ( 'a' ) } } )
114+ const r = withSetup ( { resolver, initial : { docId : 'a' , document : docOf ( 'a' ) } } )
94115 await r . navigate ( './b.md' )
95116 await r . navigate ( './c.md' )
96117 expect ( r . current . value . docId ) . toBe ( 'c' )
@@ -108,7 +129,7 @@ describe('useMdxgDocumentRouter', () => {
108129 calls ++
109130 return result ( input . href )
110131 }
111- const r = useMdxgDocumentRouter ( { resolver, initial : { docId : 'a' , document : docOf ( 'a' ) } } )
132+ const r = withSetup ( { resolver, initial : { docId : 'a' , document : docOf ( 'a' ) } } )
112133 await r . navigate ( './b.md' )
113134 await r . navigate ( './c.md' )
114135 await r . navigate ( './b.md' )
0 commit comments