Skip to content

Instantly share code, notes, and snippets.

@tommyh
Created November 20, 2013 00:12
Show Gist options
  • Save tommyh/7555008 to your computer and use it in GitHub Desktop.
Save tommyh/7555008 to your computer and use it in GitHub Desktop.
window.JasmineReact = {};
JasmineReact.renderComponent = function(component, container){
var done = false,
callback = function(){ done = true; },
isDone = function(){ return done; };
if(typeof container === "undefined" ){
container = document.getElementById("jasmine_content");
}
component = React.renderComponent(component, container, callback);
waitsFor(isDone, "React.renderComponent to async render to the container", 1000);
return component;
};
JasmineReact.unmountComponent = function(component){
return React.unmountComponentAtNode(component.getDOMNode().parentNode);
};
JasmineReact.spyOnClass = function(klass, methodName){
var klassProto = JasmineReact.classPrototype(klass),
spyObj = spyOn(klassProto, methodName);
this.reactSpies_ = this.reactSpies_ || [];
this.reactSpies_.push(spyObj);
// TODO: is this conditional correct?
if(klassProto.__reactAutoBindMap){
klassProto.__reactAutoBindMap[methodName] = spyObj;
}
return spyObj;
};
JasmineReact.addMethodToClass =function(klass, methodName, methodDefinition){
var klassProto = JasmineReact.classPrototype(klass);
klassProto[methodName] = methodDefinition;
return klassProto;
};
JasmineReact.classPrototype = function(klass){
var componentConstructor = klass.componentConstructor;
if(typeof componentConstructor === "undefined"){
throw("A component constructor could not be found for this class. Are you sure you passed in a the component definition for a React component?")
}
return componentConstructor.prototype;
};
JasmineReact.setClassDisplayName = function(klass, displayName){
var originalName = klass.componentConstructor.displayName;
klass.componentConstructor.displayName = displayName;
this.reactClassNameOverrides_ = this.reactClassNameOverrides_ || [];
this.reactClassNameOverrides_.push({klass: klass, originalName: originalName});
};
JasmineReact.removeAllSpies = function(){
if(!this.reactSpies_){
return;
}
for (var i = 0; i < this.reactSpies_.length; i++) {
var spy = this.reactSpies_[i];
if(spy.baseObj.__reactAutoBindMap){
spy.baseObj.__reactAutoBindMap[spy.methodName] = spy.originalValue;
}
}
this.reactSpies_ = [];
};
JasmineReact.resetAllClassNames = function(){
if(!this.reactClassNameOverrides_){
return;
}
for (var i = 0; i < this.reactClassNameOverrides_.length; i++) {
var obj = this.reactClassNameOverrides_[i];
obj.klass.componentConstructor.displayName = obj.originalName;
}
this.reactClassNameOverrides_ = [];
};
JasmineReact.clearJasmineContent = function(){
var jasmineContentEl = document.getElementById("jasmine_content");
if(jasmineContentEl){
React.unmountComponentAtNode(jasmineContentEl);
jasmineContentEl.innerHTML = "";
}
};
afterEach(function(){
JasmineReact.removeAllSpies();
JasmineReact.resetAllClassNames();
JasmineReact.clearJasmineContent();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment