Skip to content

Community Middleware

Nick Stefan edited this page Jan 25, 2016 · 11 revisions
  1. TestMethod

Setting up your legit tests to always use certain middleware is easy:

import TestUtils from 'legit-tests/no-dom';

import {TestMethod} from '../custom/middleware';

function Test(component, options={}){
    return TestUtils(component, options)
    .mixin({
        TestMethod
    });
}

export default Test;
export { Test };

###TestMethod Call and test React methods

  • .testMethod(ComponentThing, 'myMethod', function(myMethod){ ... })
  • .testMethod(ComponentThing, 'getInitialState', function(getInitialState){ ... })

The method is passed to the callback already bound to the component instance.

// ComponentThing is a React class
.testMethod(ComponentThing, 'myMethod', function(myMethod){ 
    expect( myMethod() ).to.be('returned value'); 
})

src:

import _ from 'lodash';
function testMethod(component, methodName, fn){
    var method = methodName;
    var componentName = component.displayName.toLowerCase();
    this.find(component);
    var componentInstance = this.elements[componentName];
    var bound = _.bind(component.prototype[methodName], componentInstance);
    fn(bound);
}
Clone this wiki locally