Skip to content

Commit

Permalink
Add some Input type range test case
Browse files Browse the repository at this point in the history
Added 2 tests, first test onChange and second onInput on range
  • Loading branch information
whs-dot-hk authored May 30, 2018
1 parent 79a740c commit 5dc4073
Showing 1 changed file with 70 additions and 12 deletions.
82 changes: 70 additions & 12 deletions packages/react-dom/src/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1307,11 +1307,11 @@ describe('ReactDOMInput', () => {
},
set: function(val) {
value = '' + val;
log.push('set property value');
log.push('set property value to ' + value);
},
});
spyOnDevAndProd(el, 'setAttribute').and.callFake(function(name) {
log.push('set attribute ' + name);
spyOnDevAndProd(el, 'setAttribute').and.callFake(function(name, v) {
log.push('set attribute ' + name + ' to ' + (v === '' ? '""' : v));
});
}
return el;
Expand All @@ -1325,20 +1325,78 @@ describe('ReactDOMInput', () => {
min="0"
max="100"
step="1"
/>,
/>
);

expect(log).toEqual([
'set attribute type',
'set attribute min',
'set attribute max',
'set attribute step',
'set property value',
'set attribute value',
'set attribute checked',
'set attribute checked',
'set attribute type to range',
'set attribute min to 0',
'set attribute max to 100',
'set attribute step to 1',
'set property value to 0',
'set attribute value to 0',
'set attribute checked to ""',
'set attribute checked to ""',
]);
});

describe('setting a range to max', () => {
function initTestInput() {
let input;

class Input extends React.Component {
state = {value: 0};
a = null;
_onChange = e => this.setState({value: e.target.value});
render() {
return (
<input
value={this.state.value}
onChange={this._onChange}
type="range"
min="0"
max="10"
step="1"
/>
);
}
}

const stub = ReactTestUtils.renderIntoDocument(<Input />);
input = ReactDOM.findDOMNode(stub);

return input;
}

it('test onChange on range', () => {
let input = initTestInput();

ReactTestUtils.Simulate.change(input, {target: {value: 10}});
expect(input.getAttribute('value')).toBe('' + 10);
});

it('test onInput on range', () => {
let input = initTestInput();

const setUntrackedInputValue = Object.getOwnPropertyDescriptor(
HTMLInputElement.prototype,
'value',
).set;

function simulateInput(elem, value) {
const inputEvent = new Event('input', {
bubbles: true,
});
setUntrackedInputValue.call(elem, value);
elem.dispatchEvent(inputEvent);
}

simulateInput(input,10);

expect(input.value).toBe('' + 10);
});
});

it('sets value properly with type coming later in props', () => {
const input = ReactTestUtils.renderIntoDocument(
<input value="hi" type="radio" />,
Expand Down

0 comments on commit 5dc4073

Please sign in to comment.