all files / src/__tests__/ index.spec.js

100% Statements 68/68
100% Branches 4/4
100% Functions 16/16
100% Lines 51/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91   11×       13×                                                                        
const wrap = require('../index');
const lifeCycleMethods = require('../lifeCycleMethods');
const assert = require('assert');
const React = require('react');
const ReactTestUtils = require('react-addons-test-utils');
 
class EmptyMockComponent extends React.Component {}
class GoodMockComponent extends React.Component {}
class BadMockComponent extends React.Component {}
 
lifeCycleMethods.forEach((methodName) => {
    GoodMockComponent.prototype[methodName] = (first, second) => {
        return first + second;
    };
 
    BadMockComponent.prototype[methodName] = () => {
        throw new Error(`${methodName} Error`);
    };
});
 
describe('The react-safe-component Test Suite', () => {
    lifeCycleMethods.forEach((methodName) => {
        describe(`When we get wrap the ${methodName}() method`, () => {
            it('Should instantiate with the original arguments', () => {
                const Component = wrap(GoodMockComponent);
                const component = new Component();
                const value = component[methodName]('foo', 'bar');
 
                assert.equal(value, 'foobar');
            });
 
            it('Should not throw an error if there is one', () => {
                const Component = wrap(BadMockComponent);
                const component = new Component();
 
                assert.doesNotThrow(component[methodName], Error);
            });
 
            it('Should not stub the method if it does not exist', () => {
                const Component = wrap(EmptyMockComponent);
                const component = new Component();
 
                assert.ok(!component[methodName]);
            });
 
            if (methodName === 'shouldComponentUpdate') {
                it('Should return a boolean for componentShouldUpdate on error', () => {
                    const Component = wrap(BadMockComponent);
                    const component = new Component();
 
                    assert.equal(typeof component[methodName](), 'boolean');
                });
            }
 
            if (methodName === 'render') {
                it('Should return a message wrapped in a react element if there is an error', () => {
                    const Component = wrap(BadMockComponent);
                    const renderer = ReactTestUtils.createRenderer();
 
                    renderer.render(
                        React.createElement(Component)
                    );
 
                    const { props } = renderer.getRenderOutput();
 
                    assert.equal(props.className, 'react__safecomponent-error');
                    assert.equal(props.children, 'Oops... an error has occured');
                });
 
                it('Should return a custom error', () => {
                    BadMockComponent.prototype.renderSafeComponentError = () => {
                        return 'Custom Error!';
                    };
 
                    const Component = wrap(BadMockComponent);
                    const renderer = ReactTestUtils.createRenderer();
 
                    renderer.render(
                        React.createElement(Component)
                    );
 
                    const { props } = renderer.getRenderOutput();
 
                    assert.equal(props.className, 'react__safecomponent-error');
                    assert.equal(props.children, 'Custom Error!');
                });
            }
        });
    });
});