react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / components / __tests__ / ReactDOMInput-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";1213/*jshint evil:true */1415var emptyFunction = require('emptyFunction');16var mocks = require('mocks');1718describe('ReactDOMInput', function() {19var React;20var ReactLink;21var ReactTestUtils;2223beforeEach(function() {24React = require('React');25ReactLink = require('ReactLink');26ReactTestUtils = require('ReactTestUtils');27});2829it('should display `defaultValue` of number 0', function() {30var stub = <input type="text" defaultValue={0} />;31stub = ReactTestUtils.renderIntoDocument(stub);32var node = stub.getDOMNode();3334expect(node.value).toBe('0');35});3637it('should display "true" for `defaultValue` of `true`', function() {38var stub = <input type="text" defaultValue={true} />;39stub = ReactTestUtils.renderIntoDocument(stub);40var node = stub.getDOMNode();4142expect(node.value).toBe('true');43});4445it('should display "false" for `defaultValue` of `false`', function() {46var stub = <input type="text" defaultValue={false} />;47stub = ReactTestUtils.renderIntoDocument(stub);48var node = stub.getDOMNode();4950expect(node.value).toBe('false');51});5253it('should display "foobar" for `defaultValue` of `objToString`', function() {54var objToString = {55toString: function() {56return "foobar";57}58};5960var stub = <input type="text" defaultValue={objToString} />;61stub = ReactTestUtils.renderIntoDocument(stub);62var node = stub.getDOMNode();6364expect(node.value).toBe('foobar');65});6667it('should display `value` of number 0', function() {68var stub = <input type="text" value={0} />;69stub = ReactTestUtils.renderIntoDocument(stub);70var node = stub.getDOMNode();7172expect(node.value).toBe('0');73});7475it('should allow setting `value` to `true`', function() {76var stub = <input type="text" value="yolo" onChange={emptyFunction} />;77stub = ReactTestUtils.renderIntoDocument(stub);78var node = stub.getDOMNode();7980expect(node.value).toBe('yolo');8182stub.replaceProps({value: true, onChange: emptyFunction});83expect(node.value).toEqual('true');84});8586it("should allow setting `value` to `false`", function() {87var stub = <input type="text" value="yolo" onChange={emptyFunction} />;88stub = ReactTestUtils.renderIntoDocument(stub);89var node = stub.getDOMNode();9091expect(node.value).toBe('yolo');9293stub.replaceProps({value: false});94expect(node.value).toEqual('false');95});9697it('should allow setting `value` to `objToString`', function() {98var stub = <input type="text" value="foo" onChange={emptyFunction} />;99stub = ReactTestUtils.renderIntoDocument(stub);100var node = stub.getDOMNode();101102expect(node.value).toBe('foo');103104var objToString = {105toString: function() {106return "foobar";107}108};109110stub.replaceProps({value: objToString, onChange: emptyFunction});111expect(node.value).toEqual('foobar');112});113114it('should properly control a value of number `0`', function() {115var stub = <input type="text" value={0} onChange={emptyFunction} />;116stub = ReactTestUtils.renderIntoDocument(stub);117var node = stub.getDOMNode();118119node.value = 'giraffe';120ReactTestUtils.Simulate.change(node);121expect(node.value).toBe('0');122});123124it('should not set a value for submit buttons unnecessarily', function() {125var stub = <input type="submit" />;126stub = ReactTestUtils.renderIntoDocument(stub);127var node = stub.getDOMNode();128129// The value shouldn't be '', or else the button will have no text; it130// should have the default "Submit" or "Submit Query" label. Most browsers131// report this as not having a `value` attribute at all; IE reports it as132// the actual label that the user sees.133expect(134!node.hasAttribute('value') || node.getAttribute('value').length > 0135).toBe(true);136});137138it('should control radio buttons', function() {139var RadioGroup = React.createClass({140render: function() {141return (142<div>143<input144ref="a"145type="radio"146name="fruit"147checked={true}148onChange={emptyFunction}149/>A150<input151ref="b"152type="radio"153name="fruit"154onChange={emptyFunction}155/>B156157<form>158<input159ref="c"160type="radio"161name="fruit"162defaultChecked={true}163onChange={emptyFunction}164/>165</form>166</div>167);168}169});170171var stub = ReactTestUtils.renderIntoDocument(<RadioGroup />);172var aNode = stub.refs.a.getDOMNode();173var bNode = stub.refs.b.getDOMNode();174var cNode = stub.refs.c.getDOMNode();175176expect(aNode.checked).toBe(true);177expect(bNode.checked).toBe(false);178// c is in a separate form and shouldn't be affected at all here179expect(cNode.checked).toBe(true);180181bNode.checked = true;182// This next line isn't necessary in a proper browser environment, but183// jsdom doesn't uncheck the others in a group (which makes this whole test184// a little less effective)185aNode.checked = false;186expect(cNode.checked).toBe(true);187188// Now let's run the actual ReactDOMInput change event handler189ReactTestUtils.Simulate.change(bNode);190191// The original state should have been restored192expect(aNode.checked).toBe(true);193expect(cNode.checked).toBe(true);194});195196it('should support ReactLink', function() {197var container = document.createElement('div');198var link = new ReactLink('yolo', mocks.getMockFunction());199var instance = <input type="text" valueLink={link} />;200201instance = React.render(instance, container);202203expect(instance.getDOMNode().value).toBe('yolo');204expect(link.value).toBe('yolo');205expect(link.requestChange.mock.calls.length).toBe(0);206207instance.getDOMNode().value = 'test';208ReactTestUtils.Simulate.change(instance.getDOMNode());209210expect(link.requestChange.mock.calls.length).toBe(1);211expect(link.requestChange.mock.calls[0][0]).toEqual('test');212});213214it('should warn with value and no onChange handler', function() {215var oldWarn = console.warn;216try {217console.warn = mocks.getMockFunction();218219var node = document.createElement('div');220var link = new ReactLink('yolo', mocks.getMockFunction());221React.render(<input type="text" valueLink={link} />, node);222expect(console.warn.mock.calls.length).toBe(0);223224React.render(225<input type="text" value="zoink" onChange={mocks.getMockFunction()} />,226node227);228expect(console.warn.mock.calls.length).toBe(0);229230React.render(231<input type="text" value="zoink" readOnly={true} />,232node233);234expect(console.warn.mock.calls.length).toBe(0);235236React.render(<input type="text" value="zoink" />, node);237expect(console.warn.mock.calls.length).toBe(1);238239React.render(240<input type="text" value="zoink" readOnly={false} />,241node242);243expect(console.warn.mock.calls.length).toBe(2);244} finally {245console.warn = oldWarn;246}247});248249it('should throw if both value and valueLink are provided', function() {250// Silences console.error messages251// ReactErrorUtils.guard is applied to all methods of a React component252// and calls console.error in __DEV__ (true for test environment)253spyOn(console, 'error');254255var node = document.createElement('div');256var link = new ReactLink('yolo', mocks.getMockFunction());257var instance = <input type="text" valueLink={link} />;258259expect(React.render.bind(React, instance, node)).not.toThrow();260261instance =262<input263type="text"264valueLink={link}265value="test"266onChange={emptyFunction}267/>;268expect(React.render.bind(React, instance, node)).toThrow();269270instance = <input type="text" valueLink={link} onChange={emptyFunction} />;271expect(React.render.bind(React, instance, node)).toThrow();272273});274275it('should support checkedLink', function() {276var container = document.createElement('div');277var link = new ReactLink(true, mocks.getMockFunction());278var instance = <input type="checkbox" checkedLink={link} />;279280instance = React.render(instance, container);281282expect(instance.getDOMNode().checked).toBe(true);283expect(link.value).toBe(true);284expect(link.requestChange.mock.calls.length).toBe(0);285286instance.getDOMNode().checked = false;287ReactTestUtils.Simulate.change(instance.getDOMNode());288289expect(link.requestChange.mock.calls.length).toBe(1);290expect(link.requestChange.mock.calls[0][0]).toEqual(false);291});292293it('should warn with checked and no onChange handler', function() {294var oldWarn = console.warn;295try {296console.warn = mocks.getMockFunction();297298var node = document.createElement('div');299var link = new ReactLink(true, mocks.getMockFunction());300React.render(<input type="checkbox" checkedLink={link} />, node);301expect(console.warn.mock.calls.length).toBe(0);302303React.render(304<input305type="checkbox"306checked="false"307onChange={mocks.getMockFunction()}308/>,309node310);311expect(console.warn.mock.calls.length).toBe(0);312313React.render(314<input type="checkbox" checked="false" readOnly={true} />,315node316);317expect(console.warn.mock.calls.length).toBe(0);318319React.render(<input type="checkbox" checked="false" />, node);320expect(console.warn.mock.calls.length).toBe(1);321322React.render(323<input type="checkbox" checked="false" readOnly={false} />,324node325);326expect(console.warn.mock.calls.length).toBe(2);327} finally {328console.warn = oldWarn;329}330});331332it('should throw if both checked and checkedLink are provided', function() {333// Silences console.error messages334// ReactErrorUtils.guard is applied to all methods of a React component335// and calls console.error in __DEV__ (true for test environment)336spyOn(console, 'error');337338var node = document.createElement('div');339var link = new ReactLink(true, mocks.getMockFunction());340var instance = <input type="checkbox" checkedLink={link} />;341342expect(React.render.bind(React, instance, node)).not.toThrow();343344instance =345<input346type="checkbox"347checkedLink={link}348checked="false"349onChange={emptyFunction}350/>;351expect(React.render.bind(React, instance, node)).toThrow();352353instance =354<input type="checkbox" checkedLink={link} onChange={emptyFunction} />;355expect(React.render.bind(React, instance, node)).toThrow();356357});358359it('should throw if both checkedLink and valueLink are provided', function() {360// Silences console.error messages361// ReactErrorUtils.guard is applied to all methods of a React component362// and calls console.error in __DEV__ (true for test environment)363spyOn(console, 'error');364365var node = document.createElement('div');366var link = new ReactLink(true, mocks.getMockFunction());367var instance = <input type="checkbox" checkedLink={link} />;368369expect(React.render.bind(React, instance, node)).not.toThrow();370371instance = <input type="checkbox" valueLink={link} />;372expect(React.render.bind(React, instance, node)).not.toThrow();373374instance =375<input type="checkbox" checkedLink={link} valueLink={emptyFunction} />;376expect(React.render.bind(React, instance, node)).toThrow();377});378});379380381