From 0c6f4b3bcc8dc728f4736e53e3d81d6ae4c47cb9 Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Wed, 12 Jun 2013 00:14:58 -0700 Subject: [PATCH 1/5] Set textarea value when changing content At http://jsfiddle.net/spicyj/W4QLq/, typing into the textbox would cause clicking the button to do nothing; now it should work. --- src/core/ReactDOM.js | 1 + src/core/ReactDOMTextarea.js | 63 ++++++++++++++++++++ src/core/ReactDefaultInjection.js | 4 +- src/core/__tests__/ReactDOMTextarea-test.js | 66 +++++++++++++++++++++ 4 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/core/ReactDOMTextarea.js create mode 100644 src/core/__tests__/ReactDOMTextarea-test.js diff --git a/src/core/ReactDOM.js b/src/core/ReactDOM.js index 2fe78e99a7..a3c470c1ce 100644 --- a/src/core/ReactDOM.js +++ b/src/core/ReactDOM.js @@ -115,6 +115,7 @@ var ReactDOM = objMapKeyVal({ table: false, tbody: false, td: false, + // Danger: this gets monkeypatched! See ReactDOMTextarea for more info. textarea: false, tfoot: false, th: false, diff --git a/src/core/ReactDOMTextarea.js b/src/core/ReactDOMTextarea.js new file mode 100644 index 0000000000..d193d7cffe --- /dev/null +++ b/src/core/ReactDOMTextarea.js @@ -0,0 +1,63 @@ +/** + * Copyright 2013 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @providesModule ReactDOMTextarea + */ + +"use strict"; + +var ReactCompositeComponent = require('ReactCompositeComponent'); +var ReactDOM = require('ReactDOM'); + +var invariant = require('invariant'); + +// Store a reference to the ); + + expect(stub.getDOMNode().value).toEqual('giraffe'); + stub.receiveProps({ children: null }, transaction); + expect(stub.getDOMNode().value).toEqual(''); + }); + + it("should update value with a single string child", function() { + var stub = ReactTestUtils.renderIntoDocument(); + + expect(stub.getDOMNode().value).toEqual('monkey'); + stub.receiveProps({ children: 'gorilla' }, transaction); + expect(stub.getDOMNode().value).toEqual('gorilla'); + }); + + it("should update value with a single numerical child", function() { + var stub = ReactTestUtils.renderIntoDocument(); + + expect(stub.getDOMNode().value).toEqual('17'); + stub.receiveProps({ children: 289 }, transaction); + expect(stub.getDOMNode().value).toEqual('289'); + }); + + it("should update value with the content property", function() { + var stub = ReactTestUtils.renderIntoDocument(); expect(stub.getDOMNode().value).toEqual('giraffe'); - stub.receiveProps({ children: null }, transaction); + stub.replaceProps({ children: null }); expect(stub.getDOMNode().value).toEqual(''); }); @@ -44,7 +41,7 @@ describe('ReactDOMTextarea', function() { var stub = ReactTestUtils.renderIntoDocument(); expect(stub.getDOMNode().value).toEqual('monkey'); - stub.receiveProps({ children: 'gorilla' }, transaction); + stub.replaceProps({ children: 'gorilla' }); expect(stub.getDOMNode().value).toEqual('gorilla'); }); @@ -52,7 +49,7 @@ describe('ReactDOMTextarea', function() { var stub = ReactTestUtils.renderIntoDocument(); expect(stub.getDOMNode().value).toEqual('17'); - stub.receiveProps({ children: 289 }, transaction); + stub.replaceProps({ children: 289 }); expect(stub.getDOMNode().value).toEqual('289'); }); @@ -60,7 +57,7 @@ describe('ReactDOMTextarea', function() { var stub = ReactTestUtils.renderIntoDocument(); + var node = stub.getDOMNode(); - expect(stub.getDOMNode().value).toEqual('giraffe'); - stub.replaceProps({ children: null }); - expect(stub.getDOMNode().value).toEqual(''); - }); + var nodeText = node[textContentAccessor]; + var nodeTextSetter = mocks.getMockFunction(); + Object.defineProperty(node, textContentAccessor, { + get: function() { + return nodeText; + }, + set: nodeTextSetter.mockImplementation(function(newText) { + nodeText = newText; + }) + }); - it("should update value with a single string child", function() { - var stub = ReactTestUtils.renderIntoDocument(); + expect(node.value).toEqual('giraffe'); - expect(stub.getDOMNode().value).toEqual('monkey'); stub.replaceProps({ children: 'gorilla' }); - expect(stub.getDOMNode().value).toEqual('gorilla'); - }); + expect(node.value).toEqual('gorilla'); - it("should update value with a single numerical child", function() { - var stub = ReactTestUtils.renderIntoDocument(); + stub.replaceProps({ children: 17 }); + expect(node.value).toEqual('17'); - expect(stub.getDOMNode().value).toEqual('17'); - stub.replaceProps({ children: 289 }); - expect(stub.getDOMNode().value).toEqual('289'); - }); + stub.replaceProps({ children: [42] }); + expect(node.value).toEqual('42'); - it("should update value with the content property", function() { - var stub = ReactTestUtils.renderIntoDocument( + ); + }).toThrow(); + + expect(function() { + var stub = ReactTestUtils.renderIntoDocument( + + ); + }).toThrow(); }); }); From ac5320e8871e87a01d22a69e2d9dc1ab7f781515 Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Wed, 19 Jun 2013 17:26:29 -0700 Subject: [PATCH 5/5] Remove textContent tests; they break in phantomjs --- src/core/__tests__/ReactDOMTextarea-test.js | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/src/core/__tests__/ReactDOMTextarea-test.js b/src/core/__tests__/ReactDOMTextarea-test.js index 0e600625c6..0ed7417d6a 100644 --- a/src/core/__tests__/ReactDOMTextarea-test.js +++ b/src/core/__tests__/ReactDOMTextarea-test.js @@ -19,8 +19,6 @@ "use strict"; -var mocks = require('mocks'); - describe('ReactDOMTextarea', function() { var React; var ReactTestUtils; @@ -29,25 +27,12 @@ describe('ReactDOMTextarea', function() { beforeEach(function() { React = require('React'); ReactTestUtils = require('ReactTestUtils'); - getTextContentAccessor = require('getTextContentAccessor'); }); - it("should update value and not update textContent", function() { - var textContentAccessor = getTextContentAccessor(); + it("should update value", function() { var stub = ReactTestUtils.renderIntoDocument(); var node = stub.getDOMNode(); - var nodeText = node[textContentAccessor]; - var nodeTextSetter = mocks.getMockFunction(); - Object.defineProperty(node, textContentAccessor, { - get: function() { - return nodeText; - }, - set: nodeTextSetter.mockImplementation(function(newText) { - nodeText = newText; - }) - }); - expect(node.value).toEqual('giraffe'); stub.replaceProps({ children: 'gorilla' }); @@ -64,8 +49,6 @@ describe('ReactDOMTextarea', function() { stub.replaceProps({ content: 'eggplant' }); expect(node.value).toEqual('eggplant'); - - expect(nodeTextSetter.mock.calls.length).toBe(0); }); it("should throw with multiple or invalid children", function() {