Skip to content

Commit 230772f

Browse files
authored
[tests] Fix ReactDOMAttribute-test (facebook#35654)
In facebook#35646 I thought there was a bug in trusted types, but the bug is in jsdom. For trusted types we still want to check the coersion and throw for a good dev warning, but prod will also throw becuase the browser will implicitly coerce to a string. This ensures there's no behavior difference between dev and prod. So the right fix is to add in the JSDOM hack that's used in `ReactDOMSelect-test.js`.
1 parent 90b2dd4 commit 230772f

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

packages/react-dom/src/__tests__/ReactDOMAttribute-test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
'use strict';
1111

12+
// Fix JSDOM. setAttribute is supposed to throw on things that can't be implicitly toStringed.
13+
const setAttribute = Element.prototype.setAttribute;
14+
Element.prototype.setAttribute = function (name, value) {
15+
// eslint-disable-next-line react-internal/safe-string-coercion
16+
return setAttribute.call(this, name, '' + value);
17+
};
18+
1219
describe('ReactDOM unknown attribute', () => {
1320
let React;
1421
let ReactDOMClient;
@@ -171,13 +178,7 @@ describe('ReactDOM unknown attribute', () => {
171178
const test = () =>
172179
testUnknownAttributeAssignment(new TemporalLike(), null);
173180

174-
if (gate('enableTrustedTypesIntegration') && !__DEV__) {
175-
// TODO: this still throws in DEV even though it's not toString'd in prod.
176-
await expect(test).rejects.toThrowError('2020-01-01');
177-
} else {
178-
await expect(test).rejects.toThrowError(new TypeError('prod message'));
179-
}
180-
181+
await expect(test).rejects.toThrowError(new TypeError('prod message'));
181182
assertConsoleErrorDev([
182183
'The provided `unknown` attribute is an unsupported type TemporalLike.' +
183184
' This value must be coerced to a string before using it here.\n' +

packages/shared/CheckStringCoercion.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ export function checkAttributeStringCoercion(
7676
attributeName: string,
7777
): void | string {
7878
if (__DEV__) {
79-
// TODO: for enableTrustedTypesIntegration we don't toString this
80-
// so we shouldn't need the DEV warning.
8179
if (willCoercionThrow(value)) {
8280
console.error(
8381
'The provided `%s` attribute is an unsupported type %s.' +

0 commit comments

Comments
 (0)