tommyh / jasmine-react

Unit test your React.js applications with Jasmine
MIT License
225 stars 23 forks source link

Still maintained? #8

Open colindresj opened 9 years ago

colindresj commented 9 years ago

Just wanted to check in to see if this library is still being maintained. I'd like to reliably use it, but notice there's a long standing open PR. Would be happy to help out if that's needed

jpiv commented 9 years ago

+1

varunshah1106 commented 9 years ago

Is this still maintained? Any alternatives?

jpiv commented 9 years ago

I went with karma mocha chai and stubbed the methods manually

varunshah1106 commented 9 years ago

I have a problem stubbing the mixing. Could you give me a sample of how you did that as a reference?

jpiv commented 9 years ago
const TestUtils = require('react/lib/ReactTestUtils');
const React = require('react');

const ReactMethodStub = {
    _context: this,

    _spyOn: function (componentClass, method) {
        const classFunction = componentClass.prototype[method];
        delete componentClass.prototype[method]
        componentClass.prototype[method] = sinon.spy(function () {
            classFunction.apply(ReactMethodStub._context, arguments);
        });
        return componentClass.prototype[method];
    },

    stubComponent: function (componentClass, methods, props) {
        if(Array.isArray(methods)) {
            methods.forEach(function (method) {
                this._spyOn(componentClass, method);
            }, this);
        } else {
            this._spyOn(componentClass, methods);
        }
        const StubbedComponent = TestUtils.renderIntoDocument(React.createElement(componentClass, props));
        this._context = StubbedComponent;
        return StubbedComponent;
    }
};

module.exports = ReactMethodStub;
jpiv commented 9 years ago

I basically go through the methods on the component and replace all the methods specified with Sinon spy wrapped function. I then render the component using TestUtils and send it back so if you were to invoke a method on the stubbed component via calling it or stimulating a DOM event you have access to all the useful sinon spy properties i.e. callCount, called, calledOnce ... ext