From e402faf7cd3373fcee593e88d1260f803e461fe2 Mon Sep 17 00:00:00 2001 From: Randall Randall Date: Thu, 3 Jul 2014 16:35:25 +0000 Subject: [PATCH] ensure that test fails React's Invariant Violation if components are swapped out from under the root; also lots of miscellaneous surgery to avoid jsdom's Wrong Document. Signed-off-by: Brandon Tilley --- test/unit/test_store_watch_mixin.js | 122 ++++++++++++++++++---------- 1 file changed, 77 insertions(+), 45 deletions(-) diff --git a/test/unit/test_store_watch_mixin.js b/test/unit/test_store_watch_mixin.js index 8949034..3e73bab 100644 --- a/test/unit/test_store_watch_mixin.js +++ b/test/unit/test_store_watch_mixin.js @@ -5,59 +5,86 @@ var Fluxxor = require("../../"), var chai = require("chai"), expect = chai.expect; -function createComponent(React) { - var Component = React.createClass({ - mixins: [StoreWatchMixin("Store1", "Store2")], - - getStateFromFlux: function() { - this.getStateCalls = this.getStateCalls || 0; - this.getStateCalls++; - return { - store1state: this.props.flux.store("Store1").getState(), - store2state: this.props.flux.store("Store2").getState() - }; - }, - - render: function() { - return React.DOM.div(null, [ - React.DOM.span({key: 1}, String(this.state.store1state.value)), - React.DOM.span({key: 2}, String(this.state.store2state.value)) - ]); - } - }); - - return Component; -} - -var Store = Fluxxor.createStore({ - actions: { - "ACTION": "handleAction" - }, - - initialize: function() { - this.value = 0; - }, - - handleAction: function() { - this.value++; - this.emit("change"); - }, - - getState: function() { - return { value: this.value }; - } -}); - describe("FluxMixin", function() { - var React, TestUtils, Comp, flux; + var SwappedComponent, createComponent, React, TestUtils, Comp, FluxMixin, FluxChildMixin, flux; beforeEach(function() { + global.window = jsdom.jsdom().createWindow(""); global.document = window.document; global.navigator = window.navigator; + for (var i in require.cache) { + if(true) { + delete require.cache[i]; + } + } React = require("react/addons"); TestUtils = React.addons.TestUtils; - Comp = createComponent(React); + FluxMixin = Fluxxor.FluxMixin(React); + FluxChildMixin = Fluxxor.FluxChildMixin(React); + + SwappedComponent = React.createClass({ + mixins: [FluxChildMixin, StoreWatchMixin("Store1")], + + getStateFromFlux: function() { + return { + store1state: this.getFlux().store("Store1").getState(), + }; + }, + + render: function() { + return React.DOM.div(null, [ + React.DOM.span({key: 1}, String(this.state.store1state.value)), + ]); + } + }); + + createComponent = function createComponent(React) { + var Component = React.createClass({ + mixins: [FluxMixin, StoreWatchMixin("Store1", "Store2")], + + getStateFromFlux: function() { + this.getStateCalls = this.getStateCalls || 0; + this.getStateCalls++; + return { + store1state: this.getFlux().store("Store1").getState(), + store2state: this.getFlux().store("Store2").getState() + }; + }, + + render: function() { + if(this.state.store1state.value === 0) { + return React.DOM.div(null, SwappedComponent()); + } + return React.DOM.div(null, [ + React.DOM.span({key: 1}, String(this.state.store1state.value)), + React.DOM.span({key: 2}, String(this.state.store2state.value)) + ]); + } + }); + + return Component; + }; + + var Store = Fluxxor.createStore({ + actions: { + "ACTION": "handleAction" + }, + + initialize: function() { + this.value = 0; + }, + + handleAction: function() { + this.value++; + this.emit("change"); + }, + + getState: function() { + return { value: this.value }; + } + }); + var stores = { Store1: new Store(), Store2: new Store() @@ -67,7 +94,10 @@ describe("FluxMixin", function() { this.dispatch("ACTION", {}); } }; + flux = new Fluxxor.Flux(stores, actions); + + Comp = createComponent(React); }); afterEach(function() { @@ -92,6 +122,7 @@ describe("FluxMixin", function() { }); }); + it("throws when attempting to mix in the function directly", function() { var Comp = React.createClass({ mixins: [Fluxxor.StoreWatchMixin], @@ -101,4 +132,5 @@ describe("FluxMixin", function() { React.renderComponentToString(Comp()); }).to.throw(/StoreWatchMixin.*function/); }); + });