|
6 | 6 | createVoidRouter, |
7 | 7 | mergeDataIntoHref, |
8 | 8 | ssrProxy, |
| 9 | + submitAction, |
9 | 10 | type VoidRouterConfig, |
10 | 11 | } from '../../src/pages/client.ts'; |
11 | 12 | import { createPageNavigationEngine } from '../../src/pages/navigation-engine.ts'; |
@@ -125,6 +126,127 @@ afterEach(() => { |
125 | 126 | }); |
126 | 127 |
|
127 | 128 | describe('createVoidRouter', () => { |
| 129 | + it.each(['POST', 'PUT', 'PATCH', 'DELETE'])( |
| 130 | + 'preserves scroll for an in-place %s action', |
| 131 | + async (method) => { |
| 132 | + stubClientGlobals(); |
| 133 | + window.location.pathname = '/editor'; |
| 134 | + window.scrollY = 1800; |
| 135 | + vi.stubGlobal( |
| 136 | + 'fetch', |
| 137 | + vi.fn(async () => pageJson({ component: 'home', props: {}, url: '/editor', errors: null })), |
| 138 | + ); |
| 139 | + const { router } = await createTestRouter(); |
| 140 | + |
| 141 | + await submitAction(router, '/editor?save', { method }); |
| 142 | + |
| 143 | + expect(window.scrollTo).not.toHaveBeenCalled(); |
| 144 | + expect(history.replaceState).toHaveBeenCalledWith(expect.anything(), '', '/editor'); |
| 145 | + }, |
| 146 | + ); |
| 147 | + |
| 148 | + it.each([ |
| 149 | + ['/editor', false, undefined], |
| 150 | + ['/destination', false, undefined], |
| 151 | + ['/destination', false, false], |
| 152 | + ['/destination', true, true], |
| 153 | + ])( |
| 154 | + 'handles scroll for an action redirect to %s (preserved: %s, override: %s)', |
| 155 | + async (destination, preserved, preserveScroll) => { |
| 156 | + stubClientGlobals(); |
| 157 | + window.location.pathname = '/editor'; |
| 158 | + window.scrollY = 1800; |
| 159 | + const response = pageJson({ component: 'home', props: {}, url: destination, errors: null }); |
| 160 | + Object.defineProperties(response, { |
| 161 | + redirected: { value: true }, |
| 162 | + url: { value: `http://localhost${destination}` }, |
| 163 | + }); |
| 164 | + vi.stubGlobal( |
| 165 | + 'fetch', |
| 166 | + vi.fn(async () => response), |
| 167 | + ); |
| 168 | + const { router } = await createTestRouter(); |
| 169 | + |
| 170 | + await submitAction(router, '/editor?save', { |
| 171 | + method: 'POST', |
| 172 | + ...(preserveScroll === undefined ? {} : { preserveScroll }), |
| 173 | + }); |
| 174 | + |
| 175 | + const writeHistory = destination === '/editor' ? history.replaceState : history.pushState; |
| 176 | + expect(writeHistory).toHaveBeenCalledWith(expect.anything(), '', destination); |
| 177 | + if (preserved) { |
| 178 | + expect(window.scrollTo).not.toHaveBeenCalled(); |
| 179 | + } else { |
| 180 | + expect(window.scrollTo).toHaveBeenCalledWith(0, 0); |
| 181 | + } |
| 182 | + }, |
| 183 | + ); |
| 184 | + |
| 185 | + it('scrolls to the fragment when an action redirects to an anchor', async () => { |
| 186 | + const documentStub = stubClientGlobals(); |
| 187 | + window.location.pathname = '/editor'; |
| 188 | + const scrollIntoView = vi.fn(); |
| 189 | + const response = pageJson({ component: 'home', props: {}, url: '/destination', errors: null }); |
| 190 | + Object.defineProperties(response, { |
| 191 | + redirected: { value: true }, |
| 192 | + url: { value: 'http://localhost/destination#details' }, |
| 193 | + }); |
| 194 | + vi.stubGlobal( |
| 195 | + 'fetch', |
| 196 | + vi.fn(async () => response), |
| 197 | + ); |
| 198 | + const { router } = await createTestRouter(); |
| 199 | + vi.stubGlobal('document', { |
| 200 | + ...documentStub, |
| 201 | + getElementById: vi.fn((id: string) => (id === 'details' ? { scrollIntoView } : null)), |
| 202 | + }); |
| 203 | + |
| 204 | + await submitAction(router, '/editor?save', { method: 'POST' }); |
| 205 | + |
| 206 | + expect(scrollIntoView).toHaveBeenCalledOnce(); |
| 207 | + expect(history.pushState).toHaveBeenCalledWith(expect.anything(), '', '/destination#details'); |
| 208 | + }); |
| 209 | + |
| 210 | + it.each([undefined, false])( |
| 211 | + 'resets action scroll with override %s when leaving the page', |
| 212 | + async (preserveScroll) => { |
| 213 | + stubClientGlobals(); |
| 214 | + window.location.pathname = '/editor'; |
| 215 | + const response = pageJson({ |
| 216 | + component: 'home', |
| 217 | + props: {}, |
| 218 | + url: '/destination', |
| 219 | + errors: null, |
| 220 | + }); |
| 221 | + vi.stubGlobal( |
| 222 | + 'fetch', |
| 223 | + vi.fn(async () => response), |
| 224 | + ); |
| 225 | + const { router } = await createTestRouter(); |
| 226 | + |
| 227 | + await submitAction(router, '/destination?save', { |
| 228 | + method: 'POST', |
| 229 | + ...(preserveScroll === undefined ? {} : { preserveScroll }), |
| 230 | + }); |
| 231 | + |
| 232 | + expect(window.scrollTo).toHaveBeenCalledWith(0, 0); |
| 233 | + }, |
| 234 | + ); |
| 235 | + |
| 236 | + it('allows an in-place action to reset scroll explicitly', async () => { |
| 237 | + stubClientGlobals(); |
| 238 | + window.location.pathname = '/editor'; |
| 239 | + vi.stubGlobal( |
| 240 | + 'fetch', |
| 241 | + vi.fn(async () => pageJson({ component: 'home', props: {}, url: '/editor', errors: null })), |
| 242 | + ); |
| 243 | + const { router } = await createTestRouter(); |
| 244 | + |
| 245 | + await submitAction(router, '/editor?save', { method: 'POST', preserveScroll: false }); |
| 246 | + |
| 247 | + expect(window.scrollTo).toHaveBeenCalledWith(0, 0); |
| 248 | + }); |
| 249 | + |
128 | 250 | it('exposes the default prefetch strategy to Links', async () => { |
129 | 251 | stubClientGlobals(); |
130 | 252 | const { router } = await createTestRouter({ prefetchDefaultStrategy: ['hover', 'click'] }); |
|
0 commit comments