Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove initOption special case #26595

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/react-dom-bindings/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
updateInput,
restoreControlledInputState,
} from './ReactDOMInput';
import {initOption, validateOptionProps} from './ReactDOMOption';
import {validateOptionProps} from './ReactDOMOption';
import {
validateSelectProps,
initSelect,
Expand Down Expand Up @@ -995,7 +995,6 @@ export function setInitialProperties(
}
}
}
initOption(domElement, props);
return;
}
case 'dialog': {
Expand Down
8 changes: 0 additions & 8 deletions packages/react-dom-bindings/src/client/ReactDOMOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import {Children} from 'react';
import {getToStringValue, toString} from './ToStringValue';

let didWarnSelectedSetOnOption = false;
let didWarnInvalidChild = false;
Expand Down Expand Up @@ -59,10 +58,3 @@ export function validateOptionProps(element: Element, props: Object) {
}
}
}

export function initOption(element: Element, props: Object) {
// value="" should make a value attribute (#6219)
if (props.value != null) {
element.setAttribute('value', toString(getToStringValue(props.value)));
}
}
29 changes: 18 additions & 11 deletions packages/react-dom/src/__tests__/ReactDOMSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

'use strict';

// Fix JSDOM. setAttribute is supposed to throw on things that can't be implicitly toStringed.
Copy link
Collaborator Author

@sebmarkbage sebmarkbage Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little hacky but it's making the TemporalLike objects throw without us explicitly tostringing in React which we shouldn't need to, since there can be Trusted Types involved. Maybe a newer JSDOM would help.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we unset this in an afterAll so it's just for this file? Or move it to setupEnvironment.js (or setupTests.js? I don't know what the difference is)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it a new document for every test suite anyway? I think we rely on that anyway.

I wanted to colocate this fix so that we can choose to fix the tests instead if we need to.

const setAttribute = Element.prototype.setAttribute;
Element.prototype.setAttribute = function (name, value) {
// eslint-disable-next-line react-internal/safe-string-coercion
return setAttribute.call(this, name, '' + value);
};

describe('ReactDOMSelect', () => {
let React;
let ReactDOM;
Expand Down Expand Up @@ -849,7 +856,7 @@ describe('ReactDOMSelect', () => {
});

describe('When given a Symbol value', () => {
it('treats initial Symbol value as an empty string', () => {
it('treats initial Symbol value as missing', () => {
let node;

expect(() => {
Expand All @@ -862,10 +869,10 @@ describe('ReactDOMSelect', () => {
);
}).toErrorDev('Invalid value for prop `value`');

expect(node.value).toBe('');
expect(node.value).toBe('A Symbol!');
});

it('treats updated Symbol value as an empty string', () => {
it('treats updated Symbol value as missing', () => {
let node;

expect(() => {
Expand All @@ -888,7 +895,7 @@ describe('ReactDOMSelect', () => {
</select>,
);

expect(node.value).toBe('');
expect(node.value).toBe('A Symbol!');
});

it('treats initial Symbol defaultValue as an empty string', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These defaultValue tests are bad tests because they test what the current selection is with the first option, but since the first option is selected when there is no match, they don't fail if it just doesn't match.

Expand All @@ -904,7 +911,7 @@ describe('ReactDOMSelect', () => {
);
}).toErrorDev('Invalid value for prop `value`');

expect(node.value).toBe('');
expect(node.value).toBe('A Symbol!');
});

it('treats updated Symbol defaultValue as an empty string', () => {
Expand All @@ -930,12 +937,12 @@ describe('ReactDOMSelect', () => {
</select>,
);

expect(node.value).toBe('');
expect(node.value).toBe('A Symbol!');
});
});

describe('When given a function value', () => {
it('treats initial function value as an empty string', () => {
it('treats initial function value as missing', () => {
let node;

expect(() => {
Expand All @@ -948,7 +955,7 @@ describe('ReactDOMSelect', () => {
);
}).toErrorDev('Invalid value for prop `value`');

expect(node.value).toBe('');
expect(node.value).toBe('A function!');
});

it('treats initial function defaultValue as an empty string', () => {
Expand All @@ -964,7 +971,7 @@ describe('ReactDOMSelect', () => {
);
}).toErrorDev('Invalid value for prop `value`');

expect(node.value).toBe('');
expect(node.value).toBe('A function!');
});

it('treats updated function value as an empty string', () => {
Expand All @@ -990,7 +997,7 @@ describe('ReactDOMSelect', () => {
</select>,
);

expect(node.value).toBe('');
expect(node.value).toBe('A function!');
});

it('treats updated function defaultValue as an empty string', () => {
Expand All @@ -1016,7 +1023,7 @@ describe('ReactDOMSelect', () => {
</select>,
);

expect(node.value).toBe('');
expect(node.value).toBe('A function!');
});
});

Expand Down