forked from facebook/react
/
ReactDOMAttribute-test.js
130 lines (112 loc) · 4.58 KB
/
ReactDOMAttribute-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
describe('ReactDOM unknown attribute', () => {
let React;
let ReactDOM;
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
});
function testUnknownAttributeRemoval(givenValue) {
const el = document.createElement('div');
ReactDOM.render(<div unknown="something" />, el);
expect(el.firstChild.getAttribute('unknown')).toBe('something');
ReactDOM.render(<div unknown={givenValue} />, el);
expect(el.firstChild.hasAttribute('unknown')).toBe(false);
}
function testUnknownAttributeAssignment(givenValue, expectedDOMValue) {
const el = document.createElement('div');
ReactDOM.render(<div unknown="something" />, el);
expect(el.firstChild.getAttribute('unknown')).toBe('something');
ReactDOM.render(<div unknown={givenValue} />, el);
expect(el.firstChild.getAttribute('unknown')).toBe(expectedDOMValue);
}
describe('unknown attributes', () => {
it('removes values null and undefined', () => {
testUnknownAttributeRemoval(null);
testUnknownAttributeRemoval(undefined);
});
it('changes values true, false to null, and also warns once', () => {
expect(() => testUnknownAttributeAssignment(true, null)).toErrorDev(
'Received `true` for a non-boolean attribute `unknown`.\n\n' +
'If you want to write it to the DOM, pass a string instead: ' +
'unknown="true" or unknown={value.toString()}.\n' +
' in div (at **)',
);
testUnknownAttributeAssignment(false, null);
});
it('removes unknown attributes that were rendered but are now missing', () => {
const el = document.createElement('div');
ReactDOM.render(<div unknown="something" />, el);
expect(el.firstChild.getAttribute('unknown')).toBe('something');
ReactDOM.render(<div />, el);
expect(el.firstChild.hasAttribute('unknown')).toBe(false);
});
it('passes through strings', () => {
testUnknownAttributeAssignment('a string', 'a string');
});
it('coerces numbers to strings', () => {
testUnknownAttributeAssignment(0, '0');
testUnknownAttributeAssignment(-1, '-1');
testUnknownAttributeAssignment(42, '42');
testUnknownAttributeAssignment(9000.99, '9000.99');
});
it('coerces NaN to strings and warns', () => {
expect(() => testUnknownAttributeAssignment(NaN, 'NaN')).toErrorDev(
'Warning: Received NaN for the `unknown` attribute. ' +
'If this is expected, cast the value to a string.\n' +
' in div (at **)',
);
});
it('coerces objects to strings and warns', () => {
const lol = {
toString() {
return 'lol';
},
};
testUnknownAttributeAssignment({hello: 'world'}, '[object Object]');
testUnknownAttributeAssignment(lol, 'lol');
});
it('removes symbols and warns', () => {
expect(() => testUnknownAttributeRemoval(Symbol('foo'))).toErrorDev(
'Warning: Invalid value for prop `unknown` on <div> tag. Either remove it ' +
'from the element, or pass a string or number value to keep it ' +
'in the DOM. For details, see https://reactjs.org/link/attribute-behavior \n' +
' in div (at **)',
);
});
it('removes functions and warns', () => {
expect(() =>
testUnknownAttributeRemoval(function someFunction() {}),
).toErrorDev(
'Warning: Invalid value for prop `unknown` on <div> tag. Either remove ' +
'it from the element, or pass a string or number value to ' +
'keep it in the DOM. For details, see ' +
'https://reactjs.org/link/attribute-behavior \n' +
' in div (at **)',
);
});
it('allows camelCase unknown attributes and warns', () => {
const el = document.createElement('div');
expect(() =>
ReactDOM.render(<div helloWorld="something" />, el),
).toErrorDev(
'React does not recognize the `helloWorld` prop on a DOM element. ' +
'If you intentionally want it to appear in the DOM as a custom ' +
'attribute, spell it as lowercase `helloworld` instead. ' +
'If you accidentally passed it from a parent component, remove ' +
'it from the DOM element.\n' +
' in div (at **)',
);
expect(el.firstChild.getAttribute('helloworld')).toBe('something');
});
});
});