import React from 'react' ; import { MemoryRouter , Route } from 'react-router' ; import LocationInterceptor from './helpers/location_interceptor' ; import urlParser from 'url' ; import { ProxyProps } from './helpers/proxy_types' ; export function createRouterProxy ( ) { const RouterProxy = ( props ) => { const { nextProxy , onFixtureUpdate } = props ; const { value : NextProxy , next } = nextProxy as any ; const { route , url , locationState , provideRouterProps } = props . proxyConfig ; const nextProxyEl = < NextProxy { ... props } nextProxy = { next ( ) } / > ; if ( locationState && ! url ) { throw new Error ( 'Must provide a url in fixture to use locationState' ) ; } if ( ! url ) { return nextProxyEl ; } const handleUrlChange = ( url ) => { onFixtureUpdate ( { url } ) ; } ; const handleLocationStateChange = ( locationState ) => { onFixtureUpdate ( { locationState } ) ; } ; const location = buildLocation ( url , locationState ) ; return ( < MemoryRouter keyLength = { 0 } initialEntries = { [ location ] } > < LocationInterceptor onUrlChange = { handleUrlChange } onLocationStateChange = { handleLocationStateChange } > { route ? ( < Route path = { route } render = { ( routerProps ) => { if ( provideRouterProps ) { const newProxyProps = { ... props , fixture : { ... props . fixture , props : { ... ( props . fixture as any ) . props , ... routerProps } } } ; return < NextProxy { ... newProxyProps } nextProxy = { next ( ) } / > ; } return nextProxyEl ; } } />) : (nextProxyEl)} < /LocationInterceptor> < /MemoryRouter>); } ; return RouterProxy ; } function buildLocation ( url , locationState ) { const { pathname , search , hash } = urlParser . parse ( url ) ; return { pathname , search , hash , key : 'mocked' , state : locationState } ; }