react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / __tests__ / ReactStateSetters-test.js
81155 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 React = require('React');14var ReactStateSetters = require('ReactStateSetters');15var ReactTestUtils = require('ReactTestUtils');1617var TestComponent;18var TestComponentWithMixin;1920describe('ReactStateSetters', function() {21beforeEach(function() {22require('mock-modules').dumpCache();2324TestComponent = React.createClass({25getInitialState: function() {26return {foo: 'foo'};27},2829render: function() {30return <div />;31}32});3334TestComponentWithMixin = React.createClass({35mixins: [ReactStateSetters.Mixin],3637getInitialState: function() {38return {foo: 'foo'};39},4041render: function() {42return <div />;43}44});45});4647it('createStateSetter should update state', function() {48var instance = <TestComponent />;49instance = ReactTestUtils.renderIntoDocument(instance);50expect(instance.state).toEqual({foo: 'foo'});5152var setter = ReactStateSetters.createStateSetter(53instance,54function(a, b, c) {55return {56foo: a + b + c,57bar: a * b * c58};59}60);61expect(instance.state).toEqual({foo: 'foo'});6263setter(1, 2, 3);64expect(instance.state).toEqual({foo: 6, bar: 6});6566setter(10, 11, 12);67expect(instance.state).toEqual({foo: 33, bar: 1320});68});6970it('createStateKeySetter should update state', function() {71var instance = <TestComponent />;72instance = ReactTestUtils.renderIntoDocument(instance);73expect(instance.state).toEqual({foo: 'foo'});7475var setter = ReactStateSetters.createStateKeySetter(instance, 'foo');7677expect(instance.state).toEqual({foo: 'foo'});7879setter('bar');80expect(instance.state).toEqual({foo: 'bar'});8182setter('baz');83expect(instance.state).toEqual({foo: 'baz'});84});8586it('createStateKeySetter is memoized', function() {87var instance = <TestComponent />;88instance = ReactTestUtils.renderIntoDocument(instance);89expect(instance.state).toEqual({foo: 'foo'});9091var foo1 = ReactStateSetters.createStateKeySetter(instance, 'foo');92var bar1 = ReactStateSetters.createStateKeySetter(instance, 'bar');9394var foo2 = ReactStateSetters.createStateKeySetter(instance, 'foo');95var bar2 = ReactStateSetters.createStateKeySetter(instance, 'bar');9697expect(foo2).toBe(foo1);98expect(bar2).toBe(bar1);99});100101it('createStateSetter should update state from mixin', function() {102var instance = <TestComponentWithMixin />;103instance = ReactTestUtils.renderIntoDocument(instance);104expect(instance.state).toEqual({foo: 'foo'});105106var setter = instance.createStateSetter(107function(a, b, c) {108return {109foo: a + b + c,110bar: a * b * c111};112}113);114expect(instance.state).toEqual({foo: 'foo'});115116setter(1, 2, 3);117expect(instance.state).toEqual({foo: 6, bar: 6});118119setter(10, 11, 12);120expect(instance.state).toEqual({foo: 33, bar: 1320});121});122123it('createStateKeySetter should update state with mixin', function() {124var instance = <TestComponentWithMixin />;125instance = ReactTestUtils.renderIntoDocument(instance);126expect(instance.state).toEqual({foo: 'foo'});127128var setter = instance.createStateKeySetter('foo');129130expect(instance.state).toEqual({foo: 'foo'});131132setter('bar');133expect(instance.state).toEqual({foo: 'bar'});134135setter('baz');136expect(instance.state).toEqual({foo: 'baz'});137});138139it('createStateKeySetter is memoized with mixin', function() {140var instance = <TestComponentWithMixin />;141instance = ReactTestUtils.renderIntoDocument(instance);142expect(instance.state).toEqual({foo: 'foo'});143144var foo1 = instance.createStateKeySetter('foo');145var bar1 = instance.createStateKeySetter('bar');146147var foo2 = instance.createStateKeySetter('foo');148var bar2 = instance.createStateKeySetter('bar');149150expect(foo2).toBe(foo1);151expect(bar2).toBe(bar1);152});153});154155156