Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): Include queryParams in redirectTo #1526

Merged
merged 5 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions packages/router/src/__tests__/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { render, waitFor, act } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'

import { Router, Route, Private, Redirect, navigate, routes } from '../'
import { resetNamedRoutes } from '../named-routes'

// SETUP
const HomePage = () => <h1>Home Page</h1>
const LoginPage = () => <h1>Login Page</h1>
const AboutPage = () => <h1>About Page</h1>
const PrivatePage = () => <h1>Private Page</h1>
const RedirectPage = () => <Redirect to="/about" />
Expand All @@ -17,6 +19,7 @@ const mockAuth = (isAuthenticated = false) => {

beforeEach(() => {
window.history.pushState({}, null, '/')
resetNamedRoutes()
})

test('inits routes and navigates as expected', async () => {
Expand Down Expand Up @@ -67,8 +70,9 @@ test('unauthenticated user is redirected away from private page', async () => {
const TestRouter = () => (
<Router useAuth={window.__REDWOOD__USE_AUTH}>
<Route path="/" page={HomePage} name="home" />
<Route path="/login" page={LoginPage} name="login" />
<Route path="/about" page={AboutPage} name="about" />
<Private unauthenticated="home">
<Private unauthenticated="login">
<Route path="/private" page={PrivatePage} name="private" />
</Private>
</Router>
Expand All @@ -79,11 +83,44 @@ test('unauthenticated user is redirected away from private page', async () => {
await waitFor(() => screen.getByText(/Home Page/i))

// navigate to private page
// should redirect to home
// should redirect to login
act(() => navigate(routes.private()))

await waitFor(() => {
expect(screen.queryByText(/Private Page/i)).not.toBeInTheDocument()
expect(window.location.pathname).toBe('/login')
expect(window.location.search).toBe('?redirectTo=/private')
screen.getByText(/Login Page/i)
})
})

test('unauthenticated user is redirected including search params', async () => {
mockAuth(false)
const TestRouter = () => (
<Router useAuth={window.__REDWOOD__USE_AUTH}>
<Route path="/" page={HomePage} name="home" />
<Route path="/login" page={LoginPage} name="login" />
<Private unauthenticated="login">
<Route path="/private" page={PrivatePage} name="private" />
</Private>
</Router>
)
const screen = render(<TestRouter />)

// starts on home page
await waitFor(() => screen.getByText(/Home Page/i))

// navigate to private page
// should redirect to login
act(() => navigate(routes.private({ bazinga: 'yeah' })))

await waitFor(() => {
expect(screen.queryByText(/Private Page/i)).not.toBeInTheDocument()
screen.getByText(/Home Page/i)
expect(window.location.pathname).toBe('/login')
expect(window.location.search).toBe(
`?redirectTo=/private${encodeURIComponent('?bazinga=yeah')}`
)
screen.getByText(/Login Page/i)
})
})

Expand Down
4 changes: 4 additions & 0 deletions packages/router/src/named-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { validatePath, replaceParams } from './internal'
let namedRoutes = {}
let namedRoutesDone = false

export const resetNamedRoutes = () => {
namedRoutesDone = false
}

const mapNamedRoutes = (routes) => {
if (namedRoutesDone) {
return namedRoutes
Expand Down
4 changes: 3 additions & 1 deletion packages/router/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ const PrivatePageLoader = ({
} else {
return (
<Redirect
to={`${unauthenticatedRoute()}?redirectTo=${window.location.pathname}`}
to={`${unauthenticatedRoute()}?redirectTo=${
window.location.pathname
}${encodeURIComponent(window.location.search)}`}
/>
)
}
Expand Down