react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / components / __tests__ / ReactDOMSelect-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 mocks = require('mocks');1617describe('ReactDOMSelect', function() {18var React;19var ReactLink;20var ReactTestUtils;2122beforeEach(function() {23React = require('React');24ReactLink = require('ReactLink');25ReactTestUtils = require('ReactTestUtils');26});2728it('should allow setting `defaultValue`', function() {29var stub =30<select defaultValue="giraffe">31<option value="monkey">A monkey!</option>32<option value="giraffe">A giraffe!</option>33<option value="gorilla">A gorilla!</option>34</select>;35stub = ReactTestUtils.renderIntoDocument(stub);36var node = stub.getDOMNode();3738expect(node.value).toBe('giraffe');3940// Changing `defaultValue` should do nothing.41stub.setProps({defaultValue: 'gorilla'});42expect(node.value).toEqual('giraffe');43});4445it('should not control when using `defaultValue`', function() {46var stub =47<select defaultValue="giraffe">48<option value="monkey">A monkey!</option>49<option value="giraffe">A giraffe!</option>50<option value="gorilla">A gorilla!</option>51</select>;52stub = ReactTestUtils.renderIntoDocument(stub);53var node = stub.getDOMNode();5455expect(node.value).toBe('giraffe');5657node.value = 'monkey';58stub.forceUpdate();59// Uncontrolled selects shouldn't change the value after first mounting60expect(node.value).toEqual('monkey');61});6263it('should allow setting `defaultValue` with multiple', function() {64var stub =65<select multiple={true} defaultValue={['giraffe', 'gorilla']}>66<option value="monkey">A monkey!</option>67<option value="giraffe">A giraffe!</option>68<option value="gorilla">A gorilla!</option>69</select>;70stub = ReactTestUtils.renderIntoDocument(stub);71var node = stub.getDOMNode();7273expect(node.options[0].selected).toBe(false); // monkey74expect(node.options[1].selected).toBe(true); // giraffe75expect(node.options[2].selected).toBe(true); // gorilla7677// Changing `defaultValue` should do nothing.78stub.setProps({defaultValue: ['monkey']});7980expect(node.options[0].selected).toBe(false); // monkey81expect(node.options[1].selected).toBe(true); // giraffe82expect(node.options[2].selected).toBe(true); // gorilla83});8485it('should allow setting `value`', function() {86var stub =87<select value="giraffe">88<option value="monkey">A monkey!</option>89<option value="giraffe">A giraffe!</option>90<option value="gorilla">A gorilla!</option>91</select>;92stub = ReactTestUtils.renderIntoDocument(stub);93var node = stub.getDOMNode();9495expect(node.value).toBe('giraffe');9697// Changing the `value` prop should change the selected option.98stub.setProps({value: 'gorilla'});99expect(node.value).toEqual('gorilla');100});101102it('should allow setting `value` with multiple', function() {103var stub =104<select multiple={true} value={['giraffe', 'gorilla']}>105<option value="monkey">A monkey!</option>106<option value="giraffe">A giraffe!</option>107<option value="gorilla">A gorilla!</option>108</select>;109stub = ReactTestUtils.renderIntoDocument(stub);110var node = stub.getDOMNode();111112expect(node.options[0].selected).toBe(false); // monkey113expect(node.options[1].selected).toBe(true); // giraffe114expect(node.options[2].selected).toBe(true); // gorilla115116// Changing the `value` prop should change the selected options.117stub.setProps({value: ['monkey']});118119expect(node.options[0].selected).toBe(true); // monkey120expect(node.options[1].selected).toBe(false); // giraffe121expect(node.options[2].selected).toBe(false); // gorilla122});123124it('should not select other options automatically', function() {125var stub =126<select multiple={true} value={['12']}>127<option value="1">one</option>128<option value="2">two</option>129<option value="12">twelve</option>130</select>;131stub = ReactTestUtils.renderIntoDocument(stub);132var node = stub.getDOMNode();133134expect(node.options[0].selected).toBe(false); // one135expect(node.options[1].selected).toBe(false); // two136expect(node.options[2].selected).toBe(true); // twelve137});138139it('should allow setting `value` with `objectToString`', function() {140var objectToString = {141animal: "giraffe",142toString: function() {143return this.animal;144}145};146147var stub =148<select multiple={true} value={[objectToString]}>149<option value="monkey">A monkey!</option>150<option value="giraffe">A giraffe!</option>151<option value="gorilla">A gorilla!</option>152</select>;153stub = ReactTestUtils.renderIntoDocument(stub);154var node = stub.getDOMNode();155156expect(node.options[0].selected).toBe(false); // monkey157expect(node.options[1].selected).toBe(true); // giraffe158expect(node.options[2].selected).toBe(false); // gorilla159160// Changing the `value` prop should change the selected options.161objectToString.animal = "monkey";162stub.forceUpdate();163164expect(node.options[0].selected).toBe(true); // monkey165expect(node.options[1].selected).toBe(false); // giraffe166expect(node.options[2].selected).toBe(false); // gorilla167});168169it('should allow switching to multiple', function() {170var stub =171<select defaultValue="giraffe">172<option value="monkey">A monkey!</option>173<option value="giraffe">A giraffe!</option>174<option value="gorilla">A gorilla!</option>175</select>;176stub = ReactTestUtils.renderIntoDocument(stub);177var node = stub.getDOMNode();178179expect(node.options[0].selected).toBe(false); // monkey180expect(node.options[1].selected).toBe(true); // giraffe181expect(node.options[2].selected).toBe(false); // gorilla182183// When making it multiple, giraffe should still be selected184stub.setProps({multiple: true, defaultValue: null});185186expect(node.options[0].selected).toBe(false); // monkey187expect(node.options[1].selected).toBe(true); // giraffe188expect(node.options[2].selected).toBe(false); // gorilla189});190191it('should allow switching from multiple', function() {192var stub =193<select multiple={true} defaultValue={['giraffe', 'gorilla']}>194<option value="monkey">A monkey!</option>195<option value="giraffe">A giraffe!</option>196<option value="gorilla">A gorilla!</option>197</select>;198stub = ReactTestUtils.renderIntoDocument(stub);199var node = stub.getDOMNode();200201expect(node.options[0].selected).toBe(false); // monkey202expect(node.options[1].selected).toBe(true); // giraffe203expect(node.options[2].selected).toBe(true); // gorilla204205// When removing multiple, giraffe should still be selected (but gorilla206// will no longer be)207stub.setProps({multiple: false, defaultValue: null});208209expect(node.options[0].selected).toBe(false); // monkey210expect(node.options[1].selected).toBe(true); // giraffe211expect(node.options[2].selected).toBe(false); // gorilla212});213214it('should support ReactLink', function() {215var link = new ReactLink('giraffe', mocks.getMockFunction());216var stub =217<select valueLink={link}>218<option value="monkey">A monkey!</option>219<option value="giraffe">A giraffe!</option>220<option value="gorilla">A gorilla!</option>221</select>;222stub = ReactTestUtils.renderIntoDocument(stub);223var node = stub.getDOMNode();224225expect(node.options[0].selected).toBe(false); // monkey226expect(node.options[1].selected).toBe(true); // giraffe227expect(node.options[2].selected).toBe(false); // gorilla228expect(link.requestChange.mock.calls.length).toBe(0);229230node.options[1].selected = false;231node.options[2].selected = true;232ReactTestUtils.Simulate.change(node);233234expect(link.requestChange.mock.calls.length).toBe(1);235expect(link.requestChange.mock.calls[0][0]).toEqual('gorilla');236237});238});239240241