|
1 | 1 | import * as React from 'react'; |
2 | 2 | import expect from 'expect'; |
3 | 3 | import { render, screen, waitFor } from '@testing-library/react'; |
4 | | -import { Route, Routes } from 'react-router-dom'; |
| 4 | +import { Route, Routes, useLocation } from 'react-router-dom'; |
5 | 5 | import { QueryClientProvider, QueryClient } from '@tanstack/react-query'; |
6 | 6 |
|
7 | | -import { useHandleAuthCallback } from './useHandleAuthCallback'; |
| 7 | +import { |
| 8 | + PreviousLocationStorageKey, |
| 9 | + useHandleAuthCallback, |
| 10 | +} from './useHandleAuthCallback'; |
8 | 11 | import { AuthContext } from './AuthContext'; |
9 | 12 | import { AuthProvider } from '../types'; |
10 | 13 |
|
@@ -43,6 +46,7 @@ const authProvider: AuthProvider = { |
43 | 46 | describe('useHandleAuthCallback', () => { |
44 | 47 | afterEach(() => { |
45 | 48 | jest.clearAllMocks(); |
| 49 | + localStorage.removeItem(PreviousLocationStorageKey); |
46 | 50 | }); |
47 | 51 |
|
48 | 52 | it('should redirect to the home route by default when the callback was successfully handled', async () => { |
@@ -92,6 +96,33 @@ describe('useHandleAuthCallback', () => { |
92 | 96 | await screen.findByText('Test'); |
93 | 97 | }); |
94 | 98 |
|
| 99 | + it('should redirect to the previous location without its source param', async () => { |
| 100 | + localStorage.setItem( |
| 101 | + PreviousLocationStorageKey, |
| 102 | + `/test?source=${encodeURIComponent( |
| 103 | + JSON.stringify({ title: 'Hello' }) |
| 104 | + )}&foo=bar` |
| 105 | + ); |
| 106 | + const Test = () => <div>Test{useLocation().search}</div>; |
| 107 | + render( |
| 108 | + <TestMemoryRouter initialEntries={['/auth-callback']}> |
| 109 | + <AuthContext.Provider value={authProvider}> |
| 110 | + <QueryClientProvider client={new QueryClient()}> |
| 111 | + <Routes> |
| 112 | + <Route path="/" element={<div>Home</div>} /> |
| 113 | + <Route path="/test" element={<Test />} /> |
| 114 | + <Route |
| 115 | + path="/auth-callback" |
| 116 | + element={<TestComponent />} |
| 117 | + /> |
| 118 | + </Routes> |
| 119 | + </QueryClientProvider> |
| 120 | + </AuthContext.Provider> |
| 121 | + </TestMemoryRouter> |
| 122 | + ); |
| 123 | + await screen.findByText('Test?foo=bar'); |
| 124 | + }); |
| 125 | + |
95 | 126 | it('should use custom useQuery options such as onError', async () => { |
96 | 127 | render( |
97 | 128 | <TestMemoryRouter initialEntries={['/auth-callback']}> |
|
0 commit comments