Repository: facebook/react
File: packages/react/src/__tests__/ReactElementClone-test.js (JavaScript)

Code before:
      foo: '56',
    });
    const props = {
      key: undefined,
      ref: undefined,
      foo: 'ef',
    };
    const clone = React.cloneElement(element, props);
    expect(clone.type).toBe(ComponentClass);
    expect(clone.key).toBe('12');
    if (gate(flags => flags.enableRefAsProp && flags.disableStringRefs)) {
      expect(clone.props.ref).toBe('34');
      expect(() => expect(clone.ref).toBe('34')).toErrorDev(
        'Accessing element.ref was removed in React 19',
        {withoutStack: true},
      );
      expect(clone.props).toEqual({foo: 'ef', ref: '34'});
    } else if (
      gate(flags => !flags.enableRefAsProp && !flags.disableStringRefs)
    ) {
      expect(clone.ref).toBe(element.ref);
      expect(clone.props).toEqual({foo: 'ef'});
    } else if (
      gate(flags => flags.enableRefAsProp && !flags.disableStringRefs)
    ) {
      expect(clone.ref).toBe(element.ref);
      expect(clone.props).toEqual({foo: 'ef'});
    } else {
      // Not going to bother testing every possible combination.
    }
    if (__DEV__) {
      expect(Object.isFrozen(element)).toBe(true);
      expect(Object.isFrozen(element.props)).toBe(true);
    }
  });

  it('should extract null key and ref', () => {
    const element = React.createElement(ComponentClass, {
      key: '12',
      ref: '34',
      foo: '56',
    });
    const props = {
      key: null,
      ref: null,
      foo: 'ef',
    };
    const clone = React.cloneElement(element, props);
    expect(clone.type).toBe(ComponentClass);
    expect(clone.key).toBe('null');
    if (gate(flags => flags.enableRefAsProp)) {

Review comment (suggestion):
Can't access `element.ref` when `enableRefAsProp` is `true` without wrapping in `toWarnDev`

```js
expect(() => {
  expect(clone.ref).toBe(element.ref);
}).toWarnDev(' Accessing element.ref was removed in React 19');
```

Code after:

  it('warns for keys for arrays of elements in rest args', () => {
    expect(() =>
      React.cloneElement(<div />, null, [<div />, <div />]),
    ).toErrorDev('Each child in a list should have a unique "key" prop.');
  });

  it('does not warns for arrays of elements with keys', () => {
    React.cloneElement(<div />, null, [<div key="#1" />, <div key="#2" />]);
  });

  it('does not warn when the element is directly in rest args', () => {
    React.cloneElement(<div />, null, <div />, <div />);
  });

  it('does not warn when the array contains a non-element', () => {
    React.cloneElement(<div />, null, [{}, {}]);
  });

  it('should ignore key and ref warning getters', () => {
    const elementA = React.createElement('div');
    const elementB = React.cloneElement(elementA, elementA.props);
    expect(elementB.key).toBe(null);
    if (gate(flags => flags.enableRefAsProp)) {
      expect(elementB.ref).toBe(null);
    } else {
      expect(elementB.ref).toBe(null);
    }
  });

  it('should ignore undefined key and ref', () => {
    const element = React.createElement(ComponentClass, {
      key: '12',
      ref: '34',
      foo: '56',
    });
    const props = {
      key: undefined,
      ref: undefined,
      foo: 'ef',
    };
    const clone = React.cloneElement(element, props);
    expect(clone.type).toBe(ComponentClass);
    expect(clone.key).toBe('12');
    if (gate(flags => flags.enableRefAsProp && flags.disableStringRefs)) {
      expect(clone.props.ref).toBe('34');
      expect(() => expect(clone.ref).toBe('34')).toErrorDev(
        'Accessing element.ref was removed in React 19',
        {withoutStack: true},
      );
      expect(clone.props).toEqual({foo: 'ef', ref: '34'});