@@ -16,7 +16,7 @@ afterEach(() => {
1616 cleanup ( )
1717} )
1818
19- async function setupAndRun ( {
19+ function setup ( {
2020 beforeLoad,
2121 loader,
2222 head,
@@ -78,6 +78,10 @@ async function setupAndRun({
7878
7979 render ( < RouterProvider router = { router } /> )
8080
81+ return { select, router }
82+ }
83+
84+ async function run ( { select } : ReturnType < typeof setup > , opts ?: { } ) {
8185 // navigate to /posts
8286 const link = await waitFor ( ( ) => screen . getByRole ( 'link' , { name : 'Posts' } ) )
8387 const before = select . mock . calls . length
@@ -93,30 +97,37 @@ async function setupAndRun({
9397
9498describe ( "Store doesn't update *too many* times during navigation" , ( ) => {
9599 test ( 'async loader, async beforeLoad, pendingMs' , async ( ) => {
96- const updates = await setupAndRun ( {
100+ const params = setup ( {
97101 beforeLoad : ( ) =>
98102 new Promise < void > ( ( resolve ) => setTimeout ( resolve , 100 ) ) ,
99103 loader : ( ) => new Promise < void > ( ( resolve ) => setTimeout ( resolve , 100 ) ) ,
100104 defaultPendingMs : 100 ,
101105 defaultPendingMinMs : 300 ,
102106 } )
103107
108+ const updates = await run ( params )
109+
104110 // This number should be as small as possible to minimize the amount of work
105111 // that needs to be done during a navigation.
106112 // Any change that increases this number should be investigated.
107113 expect ( updates ) . toBe ( 19 )
108114 } )
109115
110- test ( 'redirection' , async ( ) => {
111- const updates = await setupAndRun ( {
112- beforeLoad : ( ) => {
116+ test ( 'redirection in preload ' , async ( ) => {
117+ const { select , router } = setup ( {
118+ loader : ( ) => {
113119 throw redirect ( { to : '/other' } )
114120 } ,
115121 } )
116122
123+ const before = select . mock . calls . length
124+ await router . preloadRoute ( { to : '/posts' } )
125+ const after = select . mock . calls . length
126+ const updates = after - before
127+
117128 // This number should be as small as possible to minimize the amount of work
118129 // that needs to be done during a navigation.
119130 // Any change that increases this number should be investigated.
120- expect ( updates ) . toBe ( 26 )
131+ expect ( updates ) . toBe ( 8 )
121132 } )
122133} )
0 commit comments