-
Notifications
You must be signed in to change notification settings - Fork 145
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
fix: SwapSlippageInput was visually resetting to default value on error #1250
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,8 +50,11 @@ describe('SwapSettingsSlippageInput', () => { | |
}); | ||
|
||
it('uses provided defaultSlippage', () => { | ||
mockLifecycleStatus = { statusName: 'error', statusData: {} }; | ||
render(<SwapSettingsSlippageInput defaultSlippage={1.5} />); | ||
mockLifecycleStatus = { | ||
statusName: 'error', | ||
statusData: { isMissingRequiredField: false, maxSlippage: 1.5 }, | ||
}; | ||
render(<SwapSettingsSlippageInput />); | ||
expect(screen.getByRole('textbox')).toHaveValue('1.5'); | ||
}); | ||
|
||
|
@@ -73,25 +76,6 @@ describe('SwapSettingsSlippageInput', () => { | |
expect(screen.getByRole('textbox')).toBeDisabled(); | ||
}); | ||
|
||
it('switches between Auto and Custom modes', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have replacement tests for using the values from the config? and why did we remove this test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests were using the old defaultSlippage input param. I was going to remove and write new ones but we are still getting 100% test coverage 👀 |
||
mockLifecycleStatus = { statusName: 'error', statusData: {} }; | ||
render(<SwapSettingsSlippageInput defaultSlippage={1.5} />); | ||
const input = screen.getByRole('textbox'); | ||
expect(input).toBeDisabled(); | ||
expect(input).toHaveValue('1.5'); | ||
fireEvent.click(screen.getByRole('button', { name: 'Custom' })); | ||
expect(input).not.toBeDisabled(); | ||
fireEvent.click(screen.getByRole('button', { name: 'Auto' })); | ||
expect(input).toBeDisabled(); | ||
expect(input).toHaveValue('1.5'); | ||
expect(mockSetLifecycleStatus).toHaveBeenCalledWith({ | ||
statusName: 'slippageChange', | ||
statusData: { | ||
maxSlippage: 1.5, | ||
}, | ||
}); | ||
}); | ||
|
||
it('prevents invalid input', () => { | ||
render(<SwapSettingsSlippageInput />); | ||
fireEvent.click(screen.getByRole('button', { name: 'Custom' })); | ||
|
@@ -168,7 +152,7 @@ describe('SwapSettingsSlippageInput', () => { | |
maxSlippage: 4.5, | ||
}, | ||
}; | ||
render(<SwapSettingsSlippageInput defaultSlippage={3} />); | ||
render(<SwapSettingsSlippageInput />); | ||
expect(screen.getByRole('button', { name: 'Custom' })).toHaveClass( | ||
'bg-ock-inverse text-ock-primary shadow-ock-default', | ||
); | ||
|
@@ -184,7 +168,7 @@ describe('SwapSettingsSlippageInput', () => { | |
}); | ||
|
||
it('updates slippage when switching from Custom to Auto', () => { | ||
render(<SwapSettingsSlippageInput defaultSlippage={3} />); | ||
render(<SwapSettingsSlippageInput />); | ||
fireEvent.click(screen.getByRole('button', { name: 'Custom' })); | ||
fireEvent.change(screen.getByRole('textbox'), { target: { value: '5' } }); | ||
fireEvent.click(screen.getByRole('button', { name: 'Auto' })); | ||
|
@@ -198,7 +182,7 @@ describe('SwapSettingsSlippageInput', () => { | |
}); | ||
|
||
it('maintains custom value when in Custom mode', () => { | ||
render(<SwapSettingsSlippageInput defaultSlippage={3} />); | ||
render(<SwapSettingsSlippageInput />); | ||
fireEvent.click(screen.getByRole('button', { name: 'Custom' })); | ||
fireEvent.change(screen.getByRole('textbox'), { target: { value: '5' } }); | ||
expect(screen.getByRole('textbox')).toHaveValue('5'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { useCallback, useState } from 'react'; | ||
import { background, border, cn, color, pressable } from '../../styles/theme'; | ||
import { DEFAULT_MAX_SLIPPAGE } from '../constants'; | ||
import type { SwapSettingsSlippageInputReact } from '../types'; | ||
import { useSwapContext } from './SwapProvider'; | ||
|
||
|
@@ -10,21 +11,17 @@ const SLIPPAGE_SETTINGS = { | |
|
||
export function SwapSettingsSlippageInput({ | ||
className, | ||
defaultSlippage = 3, | ||
}: SwapSettingsSlippageInputReact) { | ||
const { updateLifecycleStatus, lifecycleStatus } = useSwapContext(); | ||
const getMaxSlippage = useCallback(() => { | ||
if (lifecycleStatus.statusName !== 'error') { | ||
return lifecycleStatus.statusData.maxSlippage; | ||
} | ||
return defaultSlippage; | ||
}, [lifecycleStatus.statusName, lifecycleStatus.statusData, defaultSlippage]); | ||
return lifecycleStatus.statusData.maxSlippage; | ||
}, [lifecycleStatus.statusData]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side note, for next PR, I don't think we even need or am I missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, we can remove it! I suppose the only benefit is minor performance improvements using memoization from |
||
|
||
// Set initial slippage values to match previous selection or default, | ||
// ensuring consistency when dropdown is reopened | ||
const [slippage, setSlippage] = useState(getMaxSlippage()); | ||
const [slippageSetting, setSlippageSetting] = useState( | ||
getMaxSlippage() === defaultSlippage | ||
getMaxSlippage() === DEFAULT_MAX_SLIPPAGE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use The default max slippage can be customized using the config parameter. It might be nice to add and track the
Thoughts? @0xAlec There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
? SLIPPAGE_SETTINGS.AUTO | ||
: SLIPPAGE_SETTINGS.CUSTOM, | ||
); | ||
|
@@ -66,10 +63,10 @@ export function SwapSettingsSlippageInput({ | |
(setting: string) => { | ||
setSlippageSetting(setting); | ||
if (setting === SLIPPAGE_SETTINGS.AUTO) { | ||
updateSlippage(defaultSlippage); | ||
updateSlippage(DEFAULT_MAX_SLIPPAGE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update to use shared const |
||
} | ||
}, | ||
[updateSlippage, defaultSlippage], | ||
[updateSlippage], | ||
); | ||
|
||
return ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,7 +274,6 @@ export type SwapSettingsSlippageDescriptionReact = { | |
*/ | ||
export type SwapSettingsSlippageInputReact = { | ||
className?: string; // Optional className override for top div element. | ||
defaultSlippage?: number; // Optional default slippage value in pecentage. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are now using Config from Leo's change this morning |
||
}; | ||
|
||
export type SwapSettingsSlippageLayoutReact = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are now using Config from Leo's change this morning