-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from cchanxzy/feat/handle-key-steps
feat: handle arrow down and arrow up step changes
- Loading branch information
Showing
9 changed files
with
268 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
src/components/__tests__/CurrencyInput-handleKeyDown.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
import CurrencyInput from '../CurrencyInput'; | ||
|
||
const id = 'validationCustom01'; | ||
|
||
describe('<CurrencyInput /> component > handleKeyDown', () => { | ||
const onChangeSpy = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should not change value if no step prop', () => { | ||
const view = shallow( | ||
<CurrencyInput id={id} prefix="£" defaultValue={100} onChange={onChangeSpy} /> | ||
); | ||
|
||
// Arrow up | ||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowUp' }); | ||
expect(onChangeSpy).not.toBeCalled(); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£100'); | ||
|
||
// Arrow down | ||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowDown' }); | ||
expect(onChangeSpy).not.toBeCalled(); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£100'); | ||
}); | ||
|
||
it('should handle negative step', () => { | ||
const view = shallow( | ||
<CurrencyInput id={id} prefix="£" defaultValue={100} step={-2} onChange={onChangeSpy} /> | ||
); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowUp' }); | ||
expect(onChangeSpy).toHaveBeenCalledWith('98', undefined); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£98'); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowDown' }); | ||
expect(onChangeSpy).toHaveBeenCalledWith('100', undefined); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£100'); | ||
}); | ||
|
||
describe('without value ie. default 0', () => { | ||
it('should handle arrow down key', () => { | ||
const view = shallow(<CurrencyInput id={id} prefix="£" step={1} onChange={onChangeSpy} />); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowDown' }); | ||
expect(onChangeSpy).toBeCalledWith('-1', undefined); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('-£1'); | ||
}); | ||
|
||
it('should handle arrow down key', () => { | ||
const view = shallow(<CurrencyInput id={id} prefix="£" step={1} onChange={onChangeSpy} />); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowUp' }); | ||
expect(onChangeSpy).toBeCalledWith('1', undefined); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£1'); | ||
}); | ||
}); | ||
|
||
describe('with value 99 and step 1.25', () => { | ||
it('should handle arrow down key', () => { | ||
const view = shallow( | ||
<CurrencyInput id={id} prefix="£" value={99} step={1.25} onChange={onChangeSpy} /> | ||
); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowDown' }); | ||
expect(onChangeSpy).toHaveBeenCalledWith('97.75', undefined); | ||
}); | ||
|
||
it('should handle arrow up key', () => { | ||
const view = shallow( | ||
<CurrencyInput id={id} prefix="£" value={99} step={1.25} onChange={onChangeSpy} /> | ||
); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowUp' }); | ||
expect(onChangeSpy).toHaveBeenCalledWith('100.25', undefined); | ||
}); | ||
}); | ||
|
||
describe('with defaultValue 100 and step 5.5', () => { | ||
it('should handle arrow down key', () => { | ||
const view = shallow( | ||
<CurrencyInput id={id} prefix="£" defaultValue={100} step={5.5} onChange={onChangeSpy} /> | ||
); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowDown' }); | ||
expect(onChangeSpy).toBeCalledWith('94.5', undefined); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£94.5'); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowDown' }); | ||
expect(onChangeSpy).toBeCalledWith('89', undefined); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£89'); | ||
}); | ||
|
||
it('should handle arrow up key', () => { | ||
const view = shallow( | ||
<CurrencyInput id={id} prefix="£" defaultValue={100} step={5.5} onChange={onChangeSpy} /> | ||
); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowUp' }); | ||
expect(onChangeSpy).toBeCalledWith('105.5', undefined); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£105.5'); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowUp' }); | ||
expect(onChangeSpy).toBeCalledWith('111', undefined); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£111'); | ||
}); | ||
}); | ||
|
||
describe('with max length 2', () => { | ||
it('should handle negative value', () => { | ||
const view = shallow( | ||
<CurrencyInput | ||
id={id} | ||
prefix="£" | ||
defaultValue={-99} | ||
step={1} | ||
maxLength={2} | ||
onChange={onChangeSpy} | ||
/> | ||
); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowDown' }); | ||
expect(onChangeSpy).not.toBeCalled(); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('-£99'); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowUp' }); | ||
expect(onChangeSpy).toHaveBeenCalledWith('-98', undefined); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('-£98'); | ||
}); | ||
|
||
it('should handle positive value', () => { | ||
const view = shallow( | ||
<CurrencyInput | ||
id={id} | ||
prefix="£" | ||
defaultValue={99} | ||
step={1} | ||
maxLength={2} | ||
onChange={onChangeSpy} | ||
/> | ||
); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowUp' }); | ||
expect(onChangeSpy).not.toBeCalled(); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£99'); | ||
|
||
view.find(`#${id}`).simulate('keyDown', { key: 'ArrowDown' }); | ||
expect(onChangeSpy).toHaveBeenCalledWith('98', undefined); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('£98'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
import CurrencyInput from '../CurrencyInput'; | ||
|
||
const id = 'validationCustom01'; | ||
|
||
describe('<CurrencyInput /> component > maxLength', () => { | ||
const onChangeSpy = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should not allow more values than max length', () => { | ||
const view = shallow( | ||
<CurrencyInput | ||
id={id} | ||
name={name} | ||
prefix="£" | ||
onChange={onChangeSpy} | ||
maxLength={3} | ||
defaultValue={123} | ||
/> | ||
); | ||
|
||
const input = view.find(`#${id}`); | ||
expect(input.prop('value')).toBe('£123'); | ||
|
||
input.simulate('change', { target: { value: '£1234' } }); | ||
expect(onChangeSpy).not.toBeCalled(); | ||
|
||
const updatedView = view.update(); | ||
expect(updatedView.find(`#${id}`).prop('value')).toBe('£123'); | ||
}); | ||
|
||
it('should apply max length rule to negative value', () => { | ||
const view = shallow( | ||
<CurrencyInput | ||
id={id} | ||
name={name} | ||
prefix="£" | ||
onChange={onChangeSpy} | ||
maxLength={3} | ||
defaultValue={-123} | ||
/> | ||
); | ||
|
||
const input = view.find(`#${id}`); | ||
expect(input.prop('value')).toBe('-£123'); | ||
|
||
input.simulate('change', { target: { value: '-£1234' } }); | ||
expect(onChangeSpy).not.toBeCalled(); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('-£123'); | ||
|
||
input.simulate('change', { target: { value: '-£125' } }); | ||
expect(onChangeSpy).toHaveBeenCalledWith('-125', ''); | ||
expect(view.update().find(`#${id}`).prop('value')).toBe('-£125'); | ||
}); | ||
}); |
Oops, something went wrong.