-
Notifications
You must be signed in to change notification settings - Fork 410
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: onValueChange
not called for values with 3 or less characters
#842
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 |
---|---|---|
|
@@ -439,27 +439,6 @@ describe('NumberFormat as input', () => { | |
}); | ||
}); | ||
|
||
it('should not call onValueChange if no formatting is applied', async () => { | ||
const mockOnValueChange = vi.fn(); | ||
|
||
const { rerender } = await render(<NumericFormat value="" onValueChange={mockOnValueChange} />); | ||
|
||
expect(mockOnValueChange).not.toHaveBeenCalled(); | ||
|
||
rerender(<NumericFormat value={NaN} onValueChange={mockOnValueChange} />); | ||
expect(mockOnValueChange).not.toHaveBeenCalled(); | ||
|
||
rerender(<NumericFormat value={1234} onValueChange={mockOnValueChange} />); | ||
expect(mockOnValueChange).not.toHaveBeenCalled(); | ||
|
||
rerender(<NumericFormat value={1234} onValueChange={mockOnValueChange} thousandSeparator />); | ||
expect(mockOnValueChange.mock.lastCall[0]).toEqual({ | ||
formattedValue: '1,234', | ||
value: '1234', | ||
floatValue: 1234, | ||
}); | ||
}); | ||
|
||
Comment on lines
-442
to
-462
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 doesn't seem to be a correct test, why should |
||
it('should always call setState when input is not on focus and value formatting is changed from outside', async () => { | ||
const { input, user, rerender } = await render( | ||
<NumericFormat value="1.1" valueIsNumericString />, | ||
|
@@ -526,7 +505,7 @@ describe('NumberFormat as input', () => { | |
expect(source).toEqual('event'); | ||
}); | ||
|
||
it('should call onValueChange when value changes', async () => { | ||
it('should call onValueChange when value changes via user input', async () => { | ||
const mockOnValueChange = vi.fn(); | ||
|
||
const { input, user, rerender } = await render( | ||
|
@@ -545,8 +524,73 @@ describe('NumberFormat as input', () => { | |
); | ||
expect(mockOnValueChange).toHaveBeenCalled(); | ||
|
||
mockOnValueChange.mockReset(); | ||
|
||
await simulateKeyInput(user, input, '5', 0); | ||
expect(input).toHaveValue('51,234'); | ||
expect(mockOnValueChange).toHaveBeenCalled(); | ||
|
||
await simulateKeyInput(user, input, '{Backspace}', 6); | ||
expect(input).toHaveValue('5,123'); | ||
expect(mockOnValueChange).toHaveBeenCalled(); | ||
|
||
mockOnValueChange.mockReset(); | ||
|
||
await simulateKeyInput(user, input, '{Backspace}', 5); | ||
expect(input).toHaveValue('512'); | ||
expect(mockOnValueChange).toHaveBeenCalled(); | ||
|
||
mockOnValueChange.mockReset(); | ||
|
||
await simulateKeyInput(user, input, '{Backspace}', 4); | ||
expect(input).toHaveValue('51'); | ||
expect(mockOnValueChange).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onValueChange when value changes via props', async () => { | ||
const mockOnValueChange = vi.fn(); | ||
|
||
const { input, rerender } = await render( | ||
<NumericFormat value="1234" valueIsNumericString={true} onValueChange={mockOnValueChange} />, | ||
); | ||
|
||
expect(input).toHaveValue('1234'); | ||
|
||
rerender( | ||
<NumericFormat | ||
onValueChange={mockOnValueChange} | ||
thousandSeparator | ||
value="1234" | ||
valueIsNumericString={true} | ||
/>, | ||
); | ||
expect(mockOnValueChange).toHaveBeenCalled(); | ||
|
||
mockOnValueChange.mockReset(); | ||
|
||
rerender( | ||
<NumericFormat | ||
onValueChange={mockOnValueChange} | ||
thousandSeparator | ||
value="123" | ||
valueIsNumericString={true} | ||
/>, | ||
); | ||
expect(mockOnValueChange).toHaveBeenCalled(); | ||
|
||
mockOnValueChange.mockReset(); | ||
|
||
rerender( | ||
<NumericFormat | ||
onValueChange={mockOnValueChange} | ||
thousandSeparator | ||
value="12" | ||
valueIsNumericString={true} | ||
/>, | ||
); | ||
expect(mockOnValueChange).toHaveBeenCalled(); | ||
|
||
console.log({ input }); | ||
}); | ||
|
||
it('should treat Infinity value as empty string', async () => { | ||
|
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.
This behaviour contradicts what is described in the documentation:
value
is a prop, so any change should trigger theonValueChange
. If the original intent of the function was to trigger on formatting changes, it should have been better described and/or be a different prop.