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

Fixed: Fix InternalRedirect component #1213

Merged
merged 3 commits into from
Aug 5, 2024
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
2 changes: 1 addition & 1 deletion frontend/src/components/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect } from "react";
import {
BrowserRouter as Router,
Route,
Routes
Routes,
} from "react-router-dom";
import axios from "axios";

Expand Down
47 changes: 47 additions & 0 deletions frontend/src/components/InternalRedirect/InternalRedirect.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { render, screen } from '@testing-library/react'
import { MemoryRouter, Route, Routes } from 'react-router-dom'
import { describe, it, expect, vi } from 'vitest'
import { InternalRedirect } from './InternalRedirect'
import { URLS } from '@/config'

// Mock the Redirect component
vi.mock('@/components/Redirect/Redirect', () => ({
default: vi.fn(({ to }) => <div data-testid="mock-redirect">{to}</div>)
}))

describe('InternalRedirect', () => {
const renderComponent = (path: string) => {
render(
<MemoryRouter initialEntries={[path]}>
<Routes>
<Route path={URLS.internalRedirect} element={<InternalRedirect />} />=
</Routes>
</MemoryRouter>
)
}

it('redirects to the correct path without search params', () => {
renderComponent('/redirect/dashboard')
expect(screen.getByTestId('mock-redirect').textContent).toBe('/dashboard')
})

it('redirects to the correct path with search params', () => {
renderComponent('/redirect/profile?id=123&tab=settings')
expect(screen.getByTestId('mock-redirect').textContent).toBe('/profile?id=123&tab=settings')
})

it('handles paths with multiple segments', () => {
renderComponent('/redirect/users/edit/42')
expect(screen.getByTestId('mock-redirect').textContent).toBe('/users/edit/42')
})

it('preserves complex search params', () => {
renderComponent('/redirect/search?q=test%20query&filter[]=a&filter[]=b')
expect(screen.getByTestId('mock-redirect').textContent).toBe('/search?q=test%20query&filter[]=a&filter[]=b')
})

it('handles redirect with empty path', () => {
renderComponent('/redirect/')
expect(screen.getByTestId('mock-redirect').textContent).toBe('/')
})
})
11 changes: 5 additions & 6 deletions frontend/src/components/InternalRedirect/InternalRedirect.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { useLocation, } from 'react-router-dom';
import Redirect from '@/components/Redirect/Redirect';

// this component is a route, so it will receive the route props
interface InternalRedirectProps extends RouteComponentProps { }
export const InternalRedirect: React.FC = () => {

export const InternalRedirect: React.FC<InternalRedirectProps> = (props) => {
const { path } = props.match.params as { path: string };
const { search } = props.location;
const location = useLocation();
const { pathname, search } = location;
const path = pathname.replace(/^\/redirect\/?/, '');

// Redirect to the experiment path
return <Redirect to={`/${path}${search}`} />;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const URLS = {
block: "/block/:slug",
experimentAbout: "/:slug/about",
experiment: "/:slug/*",
internalRedirect: "/redirect/:path",
internalRedirect: "/redirect/*",
reloadParticipant: "/participant/reload/:id/:hash",
theme: "/theme/:id",
AMLHome:
Expand Down
Loading