import * as React from 'react'; import { useEffect } from 'react'; import expect from 'expect'; import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import { Routes, Route } from 'react-router-dom'; import { CoreAdminContext } from '../core'; import { useLocation } from './useLocation'; import { RedirectionSideEffect, useRedirect } from './useRedirect'; import { testDataProvider } from '../dataProvider'; import { Identifier, RaRecord } from '../types'; import { TestMemoryRouter } from './TestMemoryRouter'; import { UseRedirect } from './useRedirect.stories'; const Redirect = ({ redirectTo, resource = '', id = undefined, data = undefined, state = undefined, }: { redirectTo: RedirectionSideEffect; resource?: string; id?: Identifier; data?: Partial; state?: object; }) => { const redirect = useRedirect(); useEffect(() => { redirect(redirectTo, resource, id, data, state); }, [resource, data, id, redirect, redirectTo, state]); return null; }; const Component = () => { const location = useLocation(); return (
); }; describe('useRedirect', () => { it('should redirect to the path with query string', async () => { render( } /> } /> ); await waitFor(() => { expect(screen.queryByDisplayValue('?bar=baz')).not.toBeNull(); }); }); it('should redirect to the path with state', async () => { render( } /> } /> ); await waitFor(() => { expect( screen.queryByDisplayValue( JSON.stringify({ _scrollToTop: true, bar: 'baz' }) ) ).not.toBeNull(); }); }); it('should support absolute URLs', () => { const oldLocation = window.location; // @ts-ignore delete window.location; // @ts-ignore window.location = { href: '' }; render( ); expect(window.location.href).toBe('https://google.com'); window.location = oldLocation; }); it('should support functions that returns local absolute URLs with a leading /', async () => { render( ); fireEvent.click(await screen.findByText('Relative url')); await screen.findByText('Admin dashboard'); fireEvent.click(await screen.findByText('Home')); fireEvent.click( await screen.findByText('Relative url from a function') ); await screen.findByText('Admin dashboard'); }); });