-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[useMediaQuery][utils] Remove usage of React 18 APIs (#29870)
- Loading branch information
Showing
6 changed files
with
61 additions
and
210 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
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 |
---|---|---|
@@ -1,97 +1,36 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { expect } from 'chai'; | ||
import { createRenderer, screen } from 'test/utils'; | ||
import { createRenderer } from 'test/utils'; | ||
import useId from './useId'; | ||
|
||
const TestComponent = ({ id: idProp }) => { | ||
const id = useId(idProp); | ||
return <span>{id}</span>; | ||
}; | ||
|
||
TestComponent.propTypes = { | ||
id: PropTypes.string, | ||
}; | ||
|
||
describe('useId', () => { | ||
const { render, renderToString } = createRenderer(); | ||
const { render } = createRenderer(); | ||
|
||
it('returns the provided ID', () => { | ||
const TestComponent = ({ id: idProp }) => { | ||
const id = useId(idProp); | ||
return <span data-testid="target" id={id} />; | ||
}; | ||
const { hydrate } = renderToString(<TestComponent id="some-id" />); | ||
const { setProps } = hydrate(); | ||
const { getByText, setProps } = render(<TestComponent id="some-id" />); | ||
|
||
expect(screen.getByTestId('target')).to.have.property('id', 'some-id'); | ||
expect(getByText('some-id')).not.to.equal(null); | ||
|
||
setProps({ id: 'another-id' }); | ||
|
||
expect(screen.getByTestId('target')).to.have.property('id', 'another-id'); | ||
expect(getByText('another-id')).not.to.equal(null); | ||
}); | ||
|
||
it("generates an ID if one isn't provided", () => { | ||
const TestComponent = ({ id: idProp }) => { | ||
const id = useId(idProp); | ||
return <span data-testid="target" id={id} />; | ||
}; | ||
const { hydrate } = renderToString(<TestComponent />); | ||
const { setProps } = hydrate(); | ||
const { getByText, setProps } = render(<TestComponent />); | ||
|
||
expect(screen.getByTestId('target').id).not.to.equal(''); | ||
expect(getByText(/^mui-[0-9]+$/)).not.to.equal(null); | ||
|
||
setProps({ id: 'another-id' }); | ||
expect(screen.getByTestId('target')).to.have.property('id', 'another-id'); | ||
}); | ||
|
||
it('can be suffixed', () => { | ||
function Widget() { | ||
const id = useId(); | ||
const labelId = `${id}-label`; | ||
|
||
return ( | ||
<React.Fragment> | ||
<span data-testid="labelable" aria-labelledby={labelId} /> | ||
<span data-testid="label" id={labelId}> | ||
Label | ||
</span> | ||
</React.Fragment> | ||
); | ||
} | ||
render(<Widget />); | ||
|
||
expect(screen.getByTestId('labelable')).to.have.attr( | ||
'aria-labelledby', | ||
screen.getByTestId('label').id, | ||
); | ||
}); | ||
|
||
it('can be used in in IDREF attributes', () => { | ||
function Widget() { | ||
const labelPartA = useId(); | ||
const labelPartB = useId(); | ||
|
||
return ( | ||
<React.Fragment> | ||
<span data-testid="labelable" aria-labelledby={`${labelPartA} ${labelPartB}`} /> | ||
<span data-testid="labelA" id={labelPartA}> | ||
A | ||
</span> | ||
<span data-testid="labelB" id={labelPartB}> | ||
B | ||
</span> | ||
</React.Fragment> | ||
); | ||
} | ||
render(<Widget />); | ||
|
||
expect(screen.getByTestId('labelable')).to.have.attr( | ||
'aria-labelledby', | ||
`${screen.getByTestId('labelA').id} ${screen.getByTestId('labelB').id}`, | ||
); | ||
}); | ||
|
||
it('provides an ID on server in React 18', function test() { | ||
if (React.useId === undefined) { | ||
this.skip(); | ||
} | ||
const TestComponent = () => { | ||
const id = useId(); | ||
return <span data-testid="target" id={id} />; | ||
}; | ||
renderToString(<TestComponent />); | ||
|
||
expect(screen.getByTestId('target').id).not.to.equal(''); | ||
expect(getByText('another-id')).not.to.equal(null); | ||
}); | ||
}); |
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