-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add readonly functionality to slider (#12410)
* feat: add readonly functionality to slider * fix: cursor following review Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
6d9f602
commit 31a69e2
Showing
3 changed files
with
118 additions
and
6 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
89 changes: 89 additions & 0 deletions
89
packages/react/src/components/Slider/__tests__/Slider-test.js
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,89 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import Slider from '../Slider'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
describe('Slider', () => { | ||
describe('behaves as expected - Component API', () => { | ||
it('should respect work normally when not readonly prop', () => { | ||
const onChange = jest.fn(); | ||
const onClick = jest.fn(); | ||
|
||
render( | ||
<Slider | ||
id="Slider-1" | ||
className="extra-class" | ||
label="Slider label" | ||
onClick={onClick} | ||
onChange={onChange} | ||
value={1} | ||
min={1} | ||
max={3} | ||
step={1} | ||
/> | ||
); | ||
|
||
// Click events should fire | ||
const theSlider = screen.getByRole('slider'); | ||
userEvent.click(theSlider); | ||
expect(onClick).toHaveBeenCalledTimes(1); | ||
|
||
userEvent.type(theSlider, '{arrowright}'); | ||
|
||
expect(onChange).toHaveBeenCalledTimes(1); | ||
expect(onChange).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
value: 2, | ||
}) | ||
); | ||
|
||
const theInput = screen.getByRole('spinbutton'); | ||
userEvent.type(theInput, '{selectall}3'); | ||
expect(onChange).toHaveBeenCalledTimes(2); | ||
expect(onChange).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
value: 3, | ||
}) | ||
); | ||
}); | ||
|
||
it('should respect readOnly prop', () => { | ||
const onChange = jest.fn(); | ||
const onClick = jest.fn(); | ||
|
||
render( | ||
<Slider | ||
id="Slider-1" | ||
className="extra-class" | ||
label="Slider label" | ||
readOnly={true} | ||
onClick={onClick} | ||
onChange={onChange} | ||
value={1} | ||
min={1} | ||
max={3} | ||
step={1} | ||
/> | ||
); | ||
|
||
// Click events should fire | ||
const theSlider = screen.getByRole('slider'); | ||
userEvent.click(theSlider); | ||
expect(onClick).toHaveBeenCalledTimes(1); | ||
|
||
userEvent.type(theSlider, '{arrowright}'); | ||
|
||
const theInput = screen.getByRole('spinbutton'); | ||
userEvent.type(theInput, '{selectall}3'); | ||
|
||
expect(onChange).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
}); |
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