Skip to content

Commit

Permalink
[Tests] make wrappingComponent tests more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Apr 3, 2019
1 parent 3045286 commit 8254e90
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
18 changes: 10 additions & 8 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Expand Up @@ -274,14 +274,15 @@ describeWithDOM('mount', () => {

wrap()
.withOverrides(() => getAdapter(), () => ({
isCustomComponent: undefined,
RootFinder: undefined,
wrapWithWrappingComponent: undefined,
createMountRenderer: (...args) => {
const renderer = realCreateMountRenderer(...args);
delete renderer.getWrappingComponentRenderer;
renderer.getNode = () => null;
return renderer;
},
isCustomComponent: undefined,
}))
.describe('with an old adapter', () => {
it('renders fine when wrappingComponent is not passed', () => {
Expand All @@ -297,15 +298,16 @@ describeWithDOM('mount', () => {
});
});

itIf(is('<=0.13'), 'throws an error if wrappingComponent is passed', () => {
class WrappingComponent extends React.Component {
render() {
const { children } = this.props;
return children;
}
class RendersChildren extends React.Component {
render() {
const { children } = this.props;
return children;
}
}

itIf(is('<=0.13'), 'throws an error if wrappingComponent is passed', () => {
expect(() => mount(<div />, {
wrappingComponent: WrappingComponent,
wrappingComponent: RendersChildren,
})).to.throw('your adapter does not support `wrappingComponent`. Try upgrading it!');
});

Expand Down
35 changes: 33 additions & 2 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -283,14 +283,19 @@ describe('shallow', () => {

wrap()
.withOverrides(() => getAdapter(), () => ({
isCustomComponent: undefined,
RootFinder: undefined,
wrapWithWrappingComponent: undefined,
isCustomComponent: undefined,
}))
.describe('with an old adapter', () => {
it('renders fine when wrappingComponent is not passed', () => {
const wrapper = shallow(<MyComponent />);
expect(wrapper.type()).to.equal('div');
expect(wrapper.debug()).to.equal(`<div>
<div>
Context says:${' '}
</div>
<More />
</div>`);
});

it('throws an error if wrappingComponent is passed', () => {
Expand All @@ -300,6 +305,32 @@ describe('shallow', () => {
});
});
});

class RendersChildren extends React.Component {
render() {
const { children } = this.props;
return children;
}
}

itIf.skip(is('<=0.13'), 'throws an error if wrappingComponent is passed', () => {
expect(() => shallow(<div />, {
wrappingComponent: RendersChildren,
})).to.throw('your adapter does not support `wrappingComponent`. Try upgrading it!');
});

describeIf.skip(is('>= 16.3'), 'uses the isValidElementType from the Adapter to validate the prop type of Component', () => {
const Foo = () => null;
const Bar = () => null;
wrap()
.withConsoleThrows()
.withOverride(() => getAdapter(), 'isValidElementType', () => val => val === Foo)
.it('with isValidElementType defined on the Adapter', () => {
expect(() => {
shallow(<Bar />);
}).to.throw('Warning: Failed prop type: Component must be a valid element type!\n in WrapperComponent');
});
});
});

describe('context', () => {
Expand Down
16 changes: 16 additions & 0 deletions packages/enzyme-test-suite/test/_helpers/index.jsx
Expand Up @@ -30,6 +30,14 @@ describeIf.only = (test, a, b) => {
}
};

describeIf.skip = (test, a, b) => {
if (typeof test !== 'boolean') {
throw new TypeError(`a boolean is required, you passed a ${typeof test}`);
}

describeIf(false, a, b);
};

/**
* Simple wrapper around mocha it which allows a boolean to be passed in first which
* determines whether or not the test will be run
Expand Down Expand Up @@ -58,6 +66,14 @@ itIf.only = (test, a, b) => {
}
};

itIf.skip = (test, a, b) => {
if (typeof test !== 'boolean') {
throw new TypeError(`a boolean is required, you passed a ${typeof test}`);
}

itIf(false, a, b);
};

/**
* Simple wrapper around mocha it which allows an array of possible values to test against.
* Each test will be wrapped in a try/catch block to handle any errors.
Expand Down

0 comments on commit 8254e90

Please sign in to comment.