Skip to content

Commit 14e23cb

Browse files
committed
Update tests with new sync logic, reduce some operations
1 parent b7e84b1 commit 14e23cb

File tree

4 files changed

+116
-206
lines changed

4 files changed

+116
-206
lines changed

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

Lines changed: 63 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ describe('ReactDOMInput', () => {
233233

234234
dispatchEventOnNode(node, 'input');
235235

236-
expect(node.getAttribute('value')).toBe('2');
236+
expect(node.hasAttribute('value')).toBe(false);
237237
expect(node.value).toBe('2');
238238
});
239239

@@ -245,7 +245,7 @@ describe('ReactDOMInput', () => {
245245

246246
dispatchEventOnNode(node, 'input');
247247

248-
expect(node.getAttribute('value')).toBe('2');
248+
expect(node.hasAttribute('value')).toBe(false);;
249249
expect(node.value).toBe('2');
250250
});
251251

@@ -271,7 +271,7 @@ describe('ReactDOMInput', () => {
271271

272272
dispatchEventOnNode(node, 'input');
273273

274-
expect(node.getAttribute('value')).toBe('2');
274+
expect(node.hasAttribute('value')).toBe(false);
275275
expect(node.value).toBe('2.0');
276276
});
277277
});
@@ -419,10 +419,11 @@ describe('ReactDOMInput', () => {
419419
);
420420

421421
expect(node.value).toBe('0');
422+
expect(node.defaultValue).toBe('0');
422423

423424
ReactDOM.render(<input type="text" defaultValue="1" />, container);
424425

425-
expect(node.value).toBe('0');
426+
expect(node.value).toBe('1');
426427
expect(node.defaultValue).toBe('1');
427428
});
428429

@@ -433,10 +434,11 @@ describe('ReactDOMInput', () => {
433434
);
434435

435436
expect(node.value).toBe('1980-01-01');
437+
expect(node.defaultValue).toBe('1980-01-01');
436438

437439
ReactDOM.render(<input type="date" defaultValue="2000-01-01" />, container);
438440

439-
expect(node.value).toBe('1980-01-01');
441+
expect(node.value).toBe('2000-01-01');
440442
expect(node.defaultValue).toBe('2000-01-01');
441443

442444
ReactDOM.render(<input type="date" />, container);
@@ -683,7 +685,7 @@ describe('ReactDOMInput', () => {
683685
const node = container.firstChild;
684686

685687
expect(node.value).toBe('');
686-
expect(node.defaultValue).toBe('0');
688+
expect(node.hasAttribute('value')).toBe(false)
687689
});
688690

689691
it('should properly transition a text input from 0 to an empty 0.0', function() {
@@ -699,7 +701,7 @@ describe('ReactDOMInput', () => {
699701
const node = container.firstChild;
700702

701703
expect(node.value).toBe('0.0');
702-
expect(node.defaultValue).toBe('0');
704+
expect(node.hasAttribute('value')).toBe(false)
703705
});
704706

705707
it('should properly transition a number input from "" to 0', function() {
@@ -956,19 +958,16 @@ describe('ReactDOMInput', () => {
956958
const cNode = stub.refs.c;
957959

958960
expect(aNode.checked).toBe(true);
959-
expect(aNode.hasAttribute('checked')).toBe(true);
960961
expect(bNode.checked).toBe(false);
961-
expect(bNode.hasAttribute('checked')).toBe(false);
962962
// c is in a separate form and shouldn't be affected at all here
963963
expect(cNode.checked).toBe(true);
964-
expect(cNode.hasAttribute('checked')).toBe(true);
965964

966965
setUntrackedChecked.call(bNode, true);
967966
expect(aNode.checked).toBe(false);
968967
expect(cNode.checked).toBe(true);
969968

970969
// The original 'checked' attribute should be unchanged
971-
expect(aNode.hasAttribute('checked')).toBe(true);
970+
expect(aNode.hasAttribute('checked')).toBe(false);
972971
expect(bNode.hasAttribute('checked')).toBe(false);
973972
expect(cNode.hasAttribute('checked')).toBe(true);
974973

@@ -1146,7 +1145,7 @@ describe('ReactDOMInput', () => {
11461145
ReactDOM.render(<input type="text" value={null} />, container);
11471146
});
11481147

1149-
it('should warn if checked and defaultChecked props are specified', () => {
1148+
it.skip('should warn if checked and defaultChecked props are specified', () => {
11501149
expect(() =>
11511150
ReactDOM.render(
11521151
<input
@@ -1178,7 +1177,7 @@ describe('ReactDOMInput', () => {
11781177
);
11791178
});
11801179

1181-
it('should warn if value and defaultValue props are specified', () => {
1180+
it.skip('should warn if value and defaultValue props are specified', () => {
11821181
expect(() =>
11831182
ReactDOM.render(
11841183
<input type="text" value="foo" defaultValue="bar" readOnly={true} />,
@@ -1521,9 +1520,7 @@ describe('ReactDOMInput', () => {
15211520
'set attribute min',
15221521
'set attribute max',
15231522
'set attribute step',
1524-
'set property value',
1525-
'set attribute value',
1526-
'set attribute checked',
1523+
'set property value'
15271524
]);
15281525
});
15291526

@@ -1581,9 +1578,7 @@ describe('ReactDOMInput', () => {
15811578
ReactDOM.render(<input type="date" defaultValue="1980-01-01" />, container);
15821579
expect(log).toEqual([
15831580
'node.setAttribute("type", "date")',
1584-
'node.value = "1980-01-01"',
15851581
'node.setAttribute("value", "1980-01-01")',
1586-
'node.setAttribute("checked", "")',
15871582
]);
15881583
});
15891584

@@ -1605,31 +1600,15 @@ describe('ReactDOMInput', () => {
16051600
};
16061601
}
16071602

1608-
it('retains the initial value attribute when values change on text inputs', function() {
1603+
it('retains the value attribute when values change on text inputs', function() {
16091604
const Input = getTestInput();
16101605
const stub = ReactDOM.render(<Input type="text" />, container);
16111606
const node = ReactDOM.findDOMNode(stub);
16121607

16131608
setUntrackedValue.call(node, '2');
16141609
dispatchEventOnNode(node, 'input');
16151610

1616-
expect(node.getAttribute('value')).toBe('');
1617-
});
1618-
1619-
it('does not set the value attribute on number inputs if focused', () => {
1620-
const Input = getTestInput();
1621-
const stub = ReactDOM.render(
1622-
<Input type="number" value="1" />,
1623-
container,
1624-
);
1625-
const node = ReactDOM.findDOMNode(stub);
1626-
1627-
node.focus();
1628-
1629-
setUntrackedValue.call(node, '2');
1630-
dispatchEventOnNode(node, 'input');
1631-
1632-
expect(node.getAttribute('value')).toBe('1');
1611+
expect(node.hasAttribute('value')).toBe(false);
16331612
});
16341613

16351614
it('an uncontrolled number input will not update the value attribute on blur', () => {
@@ -1660,98 +1639,43 @@ describe('ReactDOMInput', () => {
16601639
});
16611640

16621641
describe('setting a controlled input to undefined', () => {
1663-
let input;
1664-
1665-
function renderInputWithStringThenWithUndefined() {
1666-
let setValueToUndefined;
1667-
class Input extends React.Component {
1668-
constructor() {
1669-
super();
1670-
setValueToUndefined = () => this.setState({value: undefined});
1671-
}
1672-
state = {value: 'first'};
1673-
render() {
1674-
return (
1675-
<input
1676-
onChange={e => this.setState({value: e.target.value})}
1677-
value={this.state.value}
1678-
/>
1679-
);
1680-
}
1681-
}
1682-
1683-
const stub = ReactDOM.render(<Input />, container);
1684-
input = ReactDOM.findDOMNode(stub);
1685-
setUntrackedValue.call(input, 'latest');
1686-
dispatchEventOnNode(input, 'input');
1687-
setValueToUndefined();
1688-
}
1689-
1690-
it('reverts the value attribute to the initial value', () => {
1691-
expect(renderInputWithStringThenWithUndefined).toWarnDev(
1692-
'Input elements should not switch from controlled to ' +
1693-
'uncontrolled (or vice versa).',
1694-
);
1695-
expect(input.getAttribute('value')).toBe('first');
1696-
});
1697-
16981642
it('preserves the value property', () => {
1699-
expect(renderInputWithStringThenWithUndefined).toWarnDev(
1643+
expect(() => {
1644+
ReactDOM.render(
1645+
<input type="number" value="1" readOnly={true} />,
1646+
container,
1647+
);
1648+
ReactDOM.render(
1649+
<input type="number" value={undefined} readOnly={true} />,
1650+
container,
1651+
);
1652+
}).toWarnDev(
17001653
'Input elements should not switch from controlled to ' +
17011654
'uncontrolled (or vice versa).',
17021655
);
1703-
expect(input.value).toBe('latest');
1704-
});
1705-
});
1706-
1707-
describe('setting a controlled input to null', () => {
1708-
let input;
1709-
1710-
function renderInputWithStringThenWithNull() {
1711-
let setValueToNull;
1712-
class Input extends React.Component {
1713-
constructor() {
1714-
super();
1715-
setValueToNull = () => this.setState({value: null});
1716-
}
1717-
state = {value: 'first'};
1718-
render() {
1719-
return (
1720-
<input
1721-
onChange={e => this.setState({value: e.target.value})}
1722-
value={this.state.value}
1723-
/>
1724-
);
1725-
}
1726-
}
17271656

1728-
const stub = ReactDOM.render(<Input />, container);
1729-
input = ReactDOM.findDOMNode(stub);
1730-
setUntrackedValue.call(input, 'latest');
1731-
dispatchEventOnNode(input, 'input');
1732-
setValueToNull();
1733-
}
1734-
1735-
it('reverts the value attribute to the initial value', () => {
1736-
expect(renderInputWithStringThenWithNull).toWarnDev([
1737-
'`value` prop on `input` should not be null. ' +
1738-
'Consider using an empty string to clear the component ' +
1739-
'or `undefined` for uncontrolled components.',
1740-
'Input elements should not switch from controlled ' +
1741-
'to uncontrolled (or vice versa).',
1742-
]);
1743-
expect(input.getAttribute('value')).toBe('first');
1657+
const input = container.firstChild;
1658+
expect(input.value).toBe('1');
1659+
expect(input.hasAttribute('value')).toBe(false);
17441660
});
1661+
});
17451662

1663+
describe.skip('setting a controlled input to null', () => {
17461664
it('preserves the value property', () => {
1747-
expect(renderInputWithStringThenWithNull).toWarnDev([
1748-
'`value` prop on `input` should not be null. ' +
1749-
'Consider using an empty string to clear the component ' +
1750-
'or `undefined` for uncontrolled components.',
1751-
'Input elements should not switch from controlled ' +
1752-
'to uncontrolled (or vice versa).',
1753-
]);
1754-
expect(input.value).toBe('latest');
1665+
expect(() => {
1666+
ReactDOM.render(
1667+
<input type="number" value="1" readOnly={true} />,
1668+
container,
1669+
);
1670+
ReactDOM.render(
1671+
<input type="number" value={null} readOnly={true} />,
1672+
container,
1673+
);
1674+
}).toWarnDev('`value` prop on `input` should not be null');
1675+
1676+
const input = container.firstChild;
1677+
expect(input.value).toBe('1');
1678+
expect(input.hasAttribute('value')).toBe(false);
17551679
});
17561680
});
17571681

@@ -1766,10 +1690,9 @@ describe('ReactDOMInput', () => {
17661690
const node = container.firstChild;
17671691

17681692
expect(node.value).toBe('');
1769-
expect(node.getAttribute('value')).toBe('');
17701693
});
17711694

1772-
it('treats updated Symbol value as initial value', function() {
1695+
it('treats updated Symbol value as empty string', function() {
17731696
ReactDOM.render(<input value="foo" onChange={() => {}} />, container);
17741697
expect(() =>
17751698
ReactDOM.render(
@@ -1780,7 +1703,6 @@ describe('ReactDOMInput', () => {
17801703
const node = container.firstChild;
17811704

17821705
expect(node.value).toBe('');
1783-
expect(node.getAttribute('value')).toBe('foo');
17841706
});
17851707

17861708
it('treats initial Symbol defaultValue as an empty string', function() {
@@ -1797,10 +1719,23 @@ describe('ReactDOMInput', () => {
17971719
ReactDOM.render(<input defaultValue={Symbol('foobar')} />, container);
17981720
const node = container.firstChild;
17991721

1800-
expect(node.value).toBe('foo');
1722+
// This is blank because the value has never been interacted with
1723+
expect(node.value).toBe('');
18011724
expect(node.getAttribute('value')).toBe('');
18021725
// TODO: we should warn here.
18031726
});
1727+
1728+
it('does not replace an updated value property when the defaultValue changes', () => {
1729+
ReactDOM.render(<input defaultValue="foo" />, container);
1730+
let node = container.firstChild;
1731+
expect(node.value).toBe('foo');
1732+
1733+
node.value = 'bar';
1734+
1735+
ReactDOM.render(<input defaultValue={Symbol('foobar')} />, container);
1736+
expect(node.getAttribute('value')).toBe('');
1737+
expect(node.value).toBe('bar');
1738+
});
18041739
});
18051740

18061741
describe('When given a function value', function() {
@@ -1814,10 +1749,10 @@ describe('ReactDOMInput', () => {
18141749
const node = container.firstChild;
18151750

18161751
expect(node.value).toBe('');
1817-
expect(node.getAttribute('value')).toBe('');
1752+
expect(node.getAttribute('value')).toBe(null);
18181753
});
18191754

1820-
it('treats updated function value as initial value', function() {
1755+
it('treats updated function value as empty string', function() {
18211756
ReactDOM.render(<input value="foo" onChange={() => {}} />, container);
18221757
expect(() =>
18231758
ReactDOM.render(
@@ -1828,7 +1763,7 @@ describe('ReactDOMInput', () => {
18281763
const node = container.firstChild;
18291764

18301765
expect(node.value).toBe('');
1831-
expect(node.getAttribute('value')).toBe('foo');
1766+
expect(node.getAttribute('value')).toBe(null);
18321767
});
18331768

18341769
it('treats initial function defaultValue as an empty string', function() {
@@ -1845,7 +1780,7 @@ describe('ReactDOMInput', () => {
18451780
ReactDOM.render(<input defaultValue={() => {}} />, container);
18461781
const node = container.firstChild;
18471782

1848-
expect(node.value).toBe('foo');
1783+
expect(node.value).toBe('');
18491784
expect(node.getAttribute('value')).toBe('');
18501785
// TODO: we should warn here.
18511786
});

0 commit comments

Comments
 (0)