Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert committed Sep 25, 2023
1 parent 04563ae commit f4fa2ce
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions packages/mui-base/src/useSlider/useSlider.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { createRenderer, screen, fireEvent } from '@mui-internal/test-utils';
import { useSlider } from './useSlider';

describe('useSlider', () => {
const { render } = createRenderer();
describe('getRootProps', () => {
it('forwards external props including event handlers', () => {
const rootRef = React.createRef();

const handleClick = spy();

function Test() {
const { getRootProps } = useSlider({
rootRef,
marks: [
{
label: 'One',
value: 1,
},
],
});

return (
<div {...getRootProps({ 'data-testid': 'test-slider-root', onClick: handleClick })} />
);
}

render(<Test />);

const slider = screen.getByTestId('test-slider-root');
expect(slider).not.to.equal(null);
expect(rootRef.current).to.deep.equal(slider);

fireEvent.click(slider);
expect(handleClick.callCount).to.equal(1);
});
});

describe('getHiddenInputProps', () => {
function Test(
props = {
slotProps: {
input: {},
},
},
) {
const { getRootProps, getThumbProps, getHiddenInputProps } = useSlider({
marks: [
{
label: 'One',
value: 1,
},
],
});

return (
<div {...getRootProps()}>
<div {...getThumbProps()}>
<input
value={1}
{...getHiddenInputProps({ 'data-testid': 'test-input', ...props.slotProps.input })}
/>
</div>
</div>
);
}

it('forwards external props including event handlers', () => {
const handleClick = spy();
render(
<Test
slotProps={{
input: {
onClick: handleClick,
},
}}
/>,
);

const input = screen.getByTestId('test-input');
expect(input).not.to.equal(null);

fireEvent.click(input);
expect(handleClick.callCount).to.equal(1);
});
});
});

0 comments on commit f4fa2ce

Please sign in to comment.