Skip to content

Commit

Permalink
ensure that test fails React's Invariant Violation if components are …
Browse files Browse the repository at this point in the history
…swapped out from under the root; also lots of miscellaneous surgery to avoid jsdom's Wrong Document.

Signed-off-by: Brandon Tilley <[email protected]>
  • Loading branch information
randallsquared authored and Brandon Tilley committed Jul 9, 2014
1 parent 212f3a9 commit e402faf
Showing 1 changed file with 77 additions and 45 deletions.
122 changes: 77 additions & 45 deletions test/unit/test_store_watch_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<html><body></body></html>");
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()
Expand All @@ -67,7 +94,10 @@ describe("FluxMixin", function() {
this.dispatch("ACTION", {});
}
};

flux = new Fluxxor.Flux(stores, actions);

Comp = createComponent(React);
});

afterEach(function() {
Expand All @@ -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],
Expand All @@ -101,4 +132,5 @@ describe("FluxMixin", function() {
React.renderComponentToString(Comp());
}).to.throw(/StoreWatchMixin.*function/);
});

});

0 comments on commit e402faf

Please sign in to comment.