Skip to content

Commit bfd6104

Browse files
test(InputBase): add caret position preservation test
1 parent 0dcf7b5 commit bfd6104

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

packages/mui-material/src/InputBase/InputBase.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import PropTypes from 'prop-types';
33
import { expect } from 'chai';
4-
import { spy } from 'sinon';
4+
import sinon, { spy } from 'sinon';
55
import { act, createRenderer, fireEvent, screen, reactMajor } from '@mui/internal-test-utils';
66
import { ThemeProvider } from '@emotion/react';
77
import FormControl, { useFormControl } from '@mui/material/FormControl';
@@ -694,6 +694,32 @@ describe('<InputBase />', () => {
694694
});
695695
});
696696

697+
describe('caret behavior', () => {
698+
it('should preserve caret position when clicking on the Root wrapper', () => {
699+
const clock = sinon.useFakeTimers();
700+
701+
const { container } = render(<InputBase defaultValue="hello" />);
702+
const input = container.querySelector('input');
703+
704+
act(() => {
705+
input.setSelectionRange(2, 2);
706+
});
707+
708+
const wrapper = input.parentElement;
709+
710+
fireEvent.click(wrapper);
711+
712+
act(() => {
713+
clock.runAll();
714+
});
715+
716+
expect(input.selectionStart).to.equal(2);
717+
expect(input.selectionEnd).to.equal(2);
718+
719+
clock.restore();
720+
});
721+
});
722+
697723
describe('prop: focused', () => {
698724
it('should render correct border color with `ThemeProvider` imported from `@emotion/react`', function test() {
699725
if (window.navigator.userAgent.includes('jsdom')) {

0 commit comments

Comments
 (0)