react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / components / __tests__ / ReactDOMTextarea-test.js
81171 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @emails react-core9*/1011"use strict";1213var emptyFunction = require('emptyFunction');14var mocks = require('mocks');1516describe('ReactDOMTextarea', function() {17var React;18var ReactLink;19var ReactTestUtils;2021var renderTextarea;2223beforeEach(function() {24React = require('React');25ReactLink = require('ReactLink');26ReactTestUtils = require('ReactTestUtils');2728renderTextarea = function(component) {29var stub = ReactTestUtils.renderIntoDocument(component);30var node = stub.getDOMNode();31// Polyfilling the browser's quirky behavior.32node.value = node.innerHTML;33return stub;34};35});3637it('should allow setting `defaultValue`', function() {38var stub = <textarea defaultValue="giraffe" />;39stub = renderTextarea(stub);40var node = stub.getDOMNode();4142expect(node.value).toBe('giraffe');4344// Changing `defaultValue` should do nothing.45stub.replaceProps({defaultValue: 'gorilla'});46expect(node.value).toEqual('giraffe');47});4849it('should display `defaultValue` of number 0', function() {50var stub = <textarea defaultValue={0} />;51stub = renderTextarea(stub);52var node = stub.getDOMNode();5354expect(node.value).toBe('0');55});5657it('should display "false" for `defaultValue` of `false`', function() {58var stub = <textarea type="text" defaultValue={false} />;59stub = renderTextarea(stub);60var node = stub.getDOMNode();6162expect(node.value).toBe('false');63});6465it('should display "foobar" for `defaultValue` of `objToString`', function() {66var objToString = {67toString: function() {68return "foobar";69}70};7172var stub = <textarea type="text" defaultValue={objToString} />;73stub = renderTextarea(stub);74var node = stub.getDOMNode();7576expect(node.value).toBe('foobar');77});7879it('should not render value as an attribute', function() {80var stub = <textarea value="giraffe" onChange={emptyFunction} />;81stub = renderTextarea(stub);82var node = stub.getDOMNode();8384expect(node.getAttribute('value')).toBe(null);85});8687it('should display `value` of number 0', function() {88var stub = <textarea value={0} />;89stub = renderTextarea(stub);90var node = stub.getDOMNode();9192expect(node.value).toBe('0');93});9495it('should allow setting `value` to `giraffe`', function() {96var stub = <textarea value="giraffe" onChange={emptyFunction} />;97stub = renderTextarea(stub);98var node = stub.getDOMNode();99100expect(node.value).toBe('giraffe');101102stub.replaceProps({value: 'gorilla', onChange: emptyFunction});103expect(node.value).toEqual('gorilla');104});105106it('should allow setting `value` to `true`', function() {107var stub = <textarea value="giraffe" onChange={emptyFunction} />;108stub = renderTextarea(stub);109var node = stub.getDOMNode();110111expect(node.value).toBe('giraffe');112113stub.replaceProps({value: true, onChange: emptyFunction});114expect(node.value).toEqual('true');115});116117it('should allow setting `value` to `false`', function() {118var stub = <textarea value="giraffe" onChange={emptyFunction} />;119stub = renderTextarea(stub);120var node = stub.getDOMNode();121122expect(node.value).toBe('giraffe');123124stub.replaceProps({value: false});125expect(node.value).toEqual('false');126});127128it('should allow setting `value` to `objToString`', function() {129var stub = <textarea value="giraffe" onChange={emptyFunction} />;130stub = renderTextarea(stub);131var node = stub.getDOMNode();132133expect(node.value).toBe('giraffe');134135var objToString = {136toString: function() {137return "foo";138}139};140stub.replaceProps({value: objToString, onChange: emptyFunction});141expect(node.value).toEqual('foo');142});143144it('should properly control a value of number `0`', function() {145var stub = <textarea value={0} onChange={emptyFunction} />;146stub = renderTextarea(stub);147var node = stub.getDOMNode();148149node.value = 'giraffe';150ReactTestUtils.Simulate.change(node);151expect(node.value).toBe('0');152});153154it('should treat children like `defaultValue`', function() {155spyOn(console, 'warn');156157var stub = <textarea>giraffe</textarea>;158stub = renderTextarea(stub);159var node = stub.getDOMNode();160161expect(console.warn.argsForCall.length).toBe(1);162expect(node.value).toBe('giraffe');163164// Changing children should do nothing, it functions like `defaultValue`.165stub.replaceProps({children: 'gorilla'});166expect(node.value).toEqual('giraffe');167});168169it('should allow numbers as children', function() {170spyOn(console, 'warn');171var node = renderTextarea(<textarea>{17}</textarea>).getDOMNode();172expect(console.warn.argsForCall.length).toBe(1);173expect(node.value).toBe('17');174});175176it('should allow booleans as children', function() {177spyOn(console, 'warn');178var node = renderTextarea(<textarea>{false}</textarea>).getDOMNode();179expect(console.warn.argsForCall.length).toBe(1);180expect(node.value).toBe('false');181});182183it('should allow objects as children', function() {184spyOn(console, 'warn');185var obj = {186toString: function() {187return "sharkswithlasers";188}189};190var node = renderTextarea(<textarea>{obj}</textarea>).getDOMNode();191expect(console.warn.argsForCall.length).toBe(1);192expect(node.value).toBe('sharkswithlasers');193});194195it('should throw with multiple or invalid children', function() {196spyOn(console, 'warn');197198expect(function() {199ReactTestUtils.renderIntoDocument(200<textarea>{'hello'}{'there'}</textarea>201);202}).toThrow();203204expect(console.warn.argsForCall.length).toBe(1);205206var node;207expect(function() {208node = renderTextarea(<textarea><strong /></textarea>).getDOMNode();209}).not.toThrow();210211expect(node.value).toBe('[object Object]');212213expect(console.warn.argsForCall.length).toBe(2);214});215216it('should support ReactLink', function() {217var container = document.createElement('div');218var link = new ReactLink('yolo', mocks.getMockFunction());219var instance = <textarea valueLink={link} />;220221instance = React.render(instance, container);222223expect(instance.getDOMNode().value).toBe('yolo');224expect(link.value).toBe('yolo');225expect(link.requestChange.mock.calls.length).toBe(0);226227instance.getDOMNode().value = 'test';228ReactTestUtils.Simulate.change(instance.getDOMNode());229230expect(link.requestChange.mock.calls.length).toBe(1);231expect(link.requestChange.mock.calls[0][0]).toEqual('test');232});233});234235236