|
1 | 1 | import * as React from 'react';
|
2 | 2 | import { expect } from 'chai';
|
3 |
| -import { createRenderer } from '@mui-internal/test-utils'; |
| 3 | +import { spy } from 'sinon'; |
| 4 | +import { createRenderer, act } from '@mui-internal/test-utils'; |
4 | 5 | import { unstable_capitalize as capitalize } from '@mui/utils';
|
5 | 6 | import { PopperProps } from '@mui/base';
|
6 | 7 | import { ThemeProvider } from '@mui/joy/styles';
|
@@ -130,4 +131,51 @@ describe('<Tooltip />', () => {
|
130 | 131 | });
|
131 | 132 | });
|
132 | 133 | });
|
| 134 | + |
| 135 | + describe('focus', () => { |
| 136 | + // https://github.com/mui/mui-x/issues/12248 |
| 137 | + it('should support event handlers with extra parameters', () => { |
| 138 | + const handleFocus = spy((event, extra) => extra); |
| 139 | + const handleBlur = spy((event, ...params) => params); |
| 140 | + |
| 141 | + const TextField = React.forwardRef< |
| 142 | + HTMLDivElement, |
| 143 | + { |
| 144 | + onFocus: (event: React.FocusEvent, ...params: any[]) => void; |
| 145 | + onBlur: (event: React.FocusEvent, ...params: any[]) => void; |
| 146 | + } |
| 147 | + >(function TextField(props, ref) { |
| 148 | + const { onFocus, onBlur, ...other } = props; |
| 149 | + return ( |
| 150 | + <div ref={ref} {...other}> |
| 151 | + <input |
| 152 | + type="text" |
| 153 | + onFocus={(event) => onFocus(event, 'focus')} |
| 154 | + onBlur={(event) => onBlur(event, 'blur', 1)} |
| 155 | + /> |
| 156 | + </div> |
| 157 | + ); |
| 158 | + }); |
| 159 | + const { getByRole } = render( |
| 160 | + <Tooltip open title="test"> |
| 161 | + <TextField onFocus={handleFocus} onBlur={handleBlur} /> |
| 162 | + </Tooltip>, |
| 163 | + ); |
| 164 | + const input = getByRole('textbox'); |
| 165 | + |
| 166 | + act(() => { |
| 167 | + input.focus(); |
| 168 | + }); |
| 169 | + |
| 170 | + expect(handleFocus.callCount).to.equal(1); |
| 171 | + expect(handleFocus.returnValues[0]).to.equal('focus'); |
| 172 | + |
| 173 | + act(() => { |
| 174 | + input.blur(); |
| 175 | + }); |
| 176 | + |
| 177 | + expect(handleBlur.callCount).to.equal(1); |
| 178 | + expect(handleBlur.returnValues[0]).to.deep.equal(['blur', 1]); |
| 179 | + }); |
| 180 | + }); |
133 | 181 | });
|
0 commit comments