|
1 | 1 | import * as React from 'react';
|
2 |
| -import PropTypes from 'prop-types'; |
3 | 2 | import { expect } from 'chai';
|
4 |
| -import { createRenderer } from 'test/utils'; |
| 3 | +import { createRenderer, screen } from 'test/utils'; |
5 | 4 | import useId from './useId';
|
6 | 5 |
|
7 |
| -const TestComponent = ({ id: idProp }) => { |
8 |
| - const id = useId(idProp); |
9 |
| - return <span>{id}</span>; |
10 |
| -}; |
11 |
| - |
12 |
| -TestComponent.propTypes = { |
13 |
| - id: PropTypes.string, |
14 |
| -}; |
15 |
| - |
16 | 6 | describe('useId', () => {
|
17 |
| - const { render } = createRenderer(); |
| 7 | + const { render, renderToString } = createRenderer(); |
18 | 8 |
|
19 | 9 | it('returns the provided ID', () => {
|
20 |
| - const { getByText, setProps } = render(<TestComponent id="some-id" />); |
| 10 | + const TestComponent = ({ id: idProp }) => { |
| 11 | + const id = useId(idProp); |
| 12 | + return <span data-testid="target" id={id} />; |
| 13 | + }; |
| 14 | + const { hydrate } = renderToString(<TestComponent id="some-id" />); |
| 15 | + const { setProps } = hydrate(); |
21 | 16 |
|
22 |
| - expect(getByText('some-id')).not.to.equal(null); |
| 17 | + expect(screen.getByTestId('target')).to.have.property('id', 'some-id'); |
23 | 18 |
|
24 | 19 | setProps({ id: 'another-id' });
|
25 |
| - expect(getByText('another-id')).not.to.equal(null); |
| 20 | + |
| 21 | + expect(screen.getByTestId('target')).to.have.property('id', 'another-id'); |
26 | 22 | });
|
27 | 23 |
|
28 | 24 | it("generates an ID if one isn't provided", () => {
|
29 |
| - const { getByText, setProps } = render(<TestComponent />); |
| 25 | + const TestComponent = ({ id: idProp }) => { |
| 26 | + const id = useId(idProp); |
| 27 | + return <span data-testid="target" id={id} />; |
| 28 | + }; |
| 29 | + const { hydrate } = renderToString(<TestComponent />); |
| 30 | + const { setProps } = hydrate(); |
30 | 31 |
|
31 |
| - expect(getByText(/^mui-[0-9]+$/)).not.to.equal(null); |
| 32 | + expect(screen.getByTestId('target').id).not.to.equal(''); |
32 | 33 |
|
33 | 34 | setProps({ id: 'another-id' });
|
34 |
| - expect(getByText('another-id')).not.to.equal(null); |
| 35 | + expect(screen.getByTestId('target')).to.have.property('id', 'another-id'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('can be suffixed', () => { |
| 39 | + function Widget() { |
| 40 | + const id = useId(); |
| 41 | + const labelId = `${id}-label`; |
| 42 | + |
| 43 | + return ( |
| 44 | + <React.Fragment> |
| 45 | + <span data-testid="labelable" aria-labelledby={labelId} /> |
| 46 | + <span data-testid="label" id={labelId}> |
| 47 | + Label |
| 48 | + </span> |
| 49 | + </React.Fragment> |
| 50 | + ); |
| 51 | + } |
| 52 | + render(<Widget />); |
| 53 | + |
| 54 | + expect(screen.getByTestId('labelable')).to.have.attr( |
| 55 | + 'aria-labelledby', |
| 56 | + screen.getByTestId('label').id, |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('can be used in in IDREF attributes', () => { |
| 61 | + function Widget() { |
| 62 | + const labelPartA = useId(); |
| 63 | + const labelPartB = useId(); |
| 64 | + |
| 65 | + return ( |
| 66 | + <React.Fragment> |
| 67 | + <span data-testid="labelable" aria-labelledby={`${labelPartA} ${labelPartB}`} /> |
| 68 | + <span data-testid="labelA" id={labelPartA}> |
| 69 | + A |
| 70 | + </span> |
| 71 | + <span data-testid="labelB" id={labelPartB}> |
| 72 | + B |
| 73 | + </span> |
| 74 | + </React.Fragment> |
| 75 | + ); |
| 76 | + } |
| 77 | + render(<Widget />); |
| 78 | + |
| 79 | + expect(screen.getByTestId('labelable')).to.have.attr( |
| 80 | + 'aria-labelledby', |
| 81 | + `${screen.getByTestId('labelA').id} ${screen.getByTestId('labelB').id}`, |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('provides an ID on server in React 18', function test() { |
| 86 | + if (React.useId === undefined) { |
| 87 | + this.skip(); |
| 88 | + } |
| 89 | + const TestComponent = () => { |
| 90 | + const id = useId(); |
| 91 | + return <span data-testid="target" id={id} />; |
| 92 | + }; |
| 93 | + renderToString(<TestComponent />); |
| 94 | + |
| 95 | + expect(screen.getByTestId('target').id).not.to.equal(''); |
35 | 96 | });
|
36 | 97 | });
|
0 commit comments