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

Backport input fix #8575

Merged
merged 3 commits into from
May 20, 2017
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
165 changes: 165 additions & 0 deletions src/renderers/dom/client/__tests__/inputValueTracking-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/
'use strict';

var React = require('React');
var ReactTestUtils = require('ReactTestUtils');
var inputValueTracking = require('inputValueTracking');

describe('inputValueTracking', function() {
var input, checkbox, mockComponent;

beforeEach(function() {
input = document.createElement('input');
input.type = 'text';
checkbox = document.createElement('input');
checkbox.type = 'checkbox';
mockComponent = { _hostNode: input, _wrapperState: {} };
});

it('should attach tracker to wrapper state', function() {
inputValueTracking.track(mockComponent);

expect(
mockComponent._wrapperState.hasOwnProperty('valueTracker')
).toBe(true);
});

it('should define `value` on the instance node', function() {
inputValueTracking.track(mockComponent);

expect(
input.hasOwnProperty('value')
).toBe(true);
});

it('should define `checked` on the instance node', function() {
mockComponent._hostNode = checkbox;
inputValueTracking.track(mockComponent);

expect(checkbox.hasOwnProperty('checked')).toBe(true);
});

it('should initialize with the current value', function() {
input.value ='foo';

inputValueTracking.track(mockComponent);

var tracker = mockComponent._wrapperState.valueTracker;

expect(tracker.getValue()).toEqual('foo');
});

it('should initialize with the current `checked`', function() {
mockComponent._hostNode = checkbox;
checkbox.checked = true;
inputValueTracking.track(mockComponent);

var tracker = mockComponent._wrapperState.valueTracker;

expect(tracker.getValue()).toEqual('true');
});

it('should track value changes', function() {
input.value ='foo';

inputValueTracking.track(mockComponent);

var tracker = mockComponent._wrapperState.valueTracker;

input.value ='bar';
expect(tracker.getValue()).toEqual('bar');
});

it('should tracked`checked` changes', function() {
mockComponent._hostNode = checkbox;
checkbox.checked = true;
inputValueTracking.track(mockComponent);

var tracker = mockComponent._wrapperState.valueTracker;

checkbox.checked = false;
expect(tracker.getValue()).toEqual('false');
});

it('should update value manually', function() {
input.value ='foo';
inputValueTracking.track(mockComponent);

var tracker = mockComponent._wrapperState.valueTracker;

tracker.setValue('bar');
expect(tracker.getValue()).toEqual('bar');
});

it('should coerce value to a string', function() {
input.value ='foo';
inputValueTracking.track(mockComponent);

var tracker = mockComponent._wrapperState.valueTracker;

tracker.setValue(500);
expect(tracker.getValue()).toEqual('500');
});

it('should update value if it changed and return result', function() {
inputValueTracking.track(mockComponent);
input.value ='foo';

var tracker = mockComponent._wrapperState.valueTracker;

expect(
inputValueTracking.updateValueIfChanged(mockComponent)
).toBe(false);

tracker.setValue('bar');

expect(
inputValueTracking.updateValueIfChanged(mockComponent)
).toBe(true);

expect(tracker.getValue()).toEqual('foo');
});

it('should track value and return true when updating untracked instance', function() {
input.value ='foo';

expect(
inputValueTracking.updateValueIfChanged(mockComponent)
)
.toBe(true);

var tracker = mockComponent._wrapperState.valueTracker;
expect(tracker.getValue()).toEqual('foo');
});

it('should return tracker from node', function() {
var node = ReactTestUtils.renderIntoDocument(<input type="text" defaultValue="foo" />);
var tracker = inputValueTracking._getTrackerFromNode(node);
expect(tracker.getValue()).toEqual('foo');
});

it('should stop tracking', function() {
inputValueTracking.track(mockComponent);

expect(
mockComponent._wrapperState.hasOwnProperty('valueTracker')
).toBe(true);

inputValueTracking.stopTracking(mockComponent);

expect(
mockComponent._wrapperState.hasOwnProperty('valueTracker')
).toBe(false);

expect(input.hasOwnProperty('value')).toBe(false);
});
});
Loading