Commit 1aea452f authored by Moody Salem's avatar Moody Salem

Add a test and improve our redirects

parent e311e2fc
......@@ -98,6 +98,34 @@ function GoogleAnalyticsReporter({ location: { pathname, search } }: RouteCompon
return null
}
// Redirects to swap but only replace the pathname
function RedirectPathToSwapOnly({ location }: RouteComponentProps) {
return <Redirect to={{ ...location, pathname: '/swap' }} />
}
// Redirects from the /swap/:outputCurrency path to the /swap?outputCurrency=:outputCurrency format
function RedirectToSwap(props: RouteComponentProps<{ outputCurrency: string }>) {
const {
location: { search },
match: {
params: { outputCurrency }
}
} = props
return (
<Redirect
to={{
...props.location,
pathname: '/swap',
search:
search && search.length > 1
? `${search}&outputCurrency=${outputCurrency}`
: `?outputCurrency=${outputCurrency}`
}}
/>
)
}
export default function App() {
return (
<>
......@@ -115,6 +143,7 @@ export default function App() {
<NavigationTabs />
<Switch>
<Route exact strict path="/swap" component={Swap} />
<Route exact strict path="/swap/:outputCurrency" component={RedirectToSwap} />
<Route exact strict path="/send" component={Send} />
<Route exact strict path="/find" component={Find} />
<Route exact strict path="/create" component={Create} />
......@@ -153,7 +182,7 @@ export default function App() {
}
}}
/>
<Redirect to="/swap" />
<Route component={RedirectPathToSwapOnly} />
</Switch>
</Body>
</Web3ReactManager>
......
......@@ -33,6 +33,22 @@ describe('swap reducer', () => {
})
})
test('does not duplicate eth for invalid output token', () => {
store.dispatch(
setDefaultsFromURL({
chainId: ChainId.MAINNET,
queryString: '?outputCurrency=invalid'
})
)
expect(store.getState()).toEqual({
[Field.INPUT]: { address: '' },
[Field.OUTPUT]: { address: WETH[ChainId.MAINNET].address },
typedValue: '',
independentField: Field.INPUT
})
})
test('output ETH only', () => {
store.dispatch(
setDefaultsFromURL({
......
......@@ -54,10 +54,10 @@ export default createReducer<SwapState>(initialState, builder =>
let inputCurrency = parseCurrencyFromURLParameter(parsedQs.inputCurrency, chainId)
let outputCurrency = parseCurrencyFromURLParameter(parsedQs.outputCurrency, chainId)
if (inputCurrency === outputCurrency) {
if (typeof parsedQs.inputCurrency === 'string') {
outputCurrency = ''
} else if (typeof parsedQs.outputCurrency === 'string') {
if (typeof parsedQs.outputCurrency === 'string') {
inputCurrency = ''
} else {
outputCurrency = ''
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment