import { mount } from 'enzyme'; import React from 'react'; import { MemoryRouter, StaticRouter, Switch } from 'react-router'; import { Redirect } from './Redirect'; declare let __BROWSER__: boolean; // for MemoryRouter Redirect needs to be inside for `from` to work // `from` is required so we dont redirect twice (otherwise react-router warns) // not required for StaticRouter as it only renders once describe('', () => { test('Redirect on client', () => { __BROWSER__ = true; const router = mount( ); expect(router.find('Router').props().history.location.pathname).toBe('/two'); }); test('Redirect on client when `to` is location descriptor', () => { __BROWSER__ = true; const router = mount( ); expect(router.find('Router').props().history.location.pathname).toBe('/two'); expect(router.find('Router').props().history.location.search).toBe('?foo=bar'); }); test('Signal server redirect (internal path)', () => { __BROWSER__ = false; const staticContext = {}; mount( ); expect(staticContext.action).toBe('REPLACE'); expect(staticContext.location.pathname).toBe('/two'); }); test('Signal server redirect (external path)', () => { __BROWSER__ = false; const staticContext = {}; mount( ); expect(staticContext.action).toBe('REPLACE'); expect(staticContext.externalPath).toBe('https://google.com'); expect(staticContext.location).toBe(undefined); }); test('Signal server redirect when `to` is object', () => { __BROWSER__ = false; const staticContext = {}; mount( ); expect(staticContext.action).toBe('REPLACE'); expect(staticContext.location.pathname).toBe('/two'); expect(staticContext.location.search).toBe('?foo=bar'); expect(staticContext.url).toBe('/two?foo=bar'); }); test('Signal server redirect with status', () => { __BROWSER__ = false; const staticContext = {}; mount( ); expect(staticContext.action).toBe('REPLACE'); expect(staticContext.location.pathname).toBe('/two'); expect(staticContext.statusCode).toBe(301); }); });