Skip to content

Commit

Permalink
[Tests] .state(): flesh out test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 2, 2018
1 parent b747ce0 commit dd275fe
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3489,6 +3489,34 @@ describeWithDOM('mount', () => {
const wrapper = mount(<Foo />);
expect(wrapper.state('foo')).to.equal('foo');
});

it('throws on host nodes', () => {
const wrapper = mount(<div><span /></div>);

expect(() => wrapper.state()).to.throw(Error, 'ReactWrapper::state() can only be called on class components');
});

itIf(is('>= 16'), 'throws on Portals', () => {
const containerDiv = global.document.createElement('div');
const portal = createPortal(
<div />,
containerDiv,
);

const wrapper = mount(<div>{portal}</div>);
expect(() => wrapper.state()).to.throw(Error, 'ReactWrapper::state() can only be called on class components');
});

describeIf(is('> 0.13'), 'stateless function components', () => {
it('throws on SFCs', () => {
function Foo() {
return <div />;
}

const wrapper = mount(<Foo />);
expect(() => wrapper.state()).to.throw(Error, 'ReactWrapper::state() can only be called on class components');
});
});
});

describe('.children([selector])', () => {
Expand Down
28 changes: 28 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3182,6 +3182,34 @@ describe('shallow', () => {
const wrapper = shallow(<Foo />);
expect(wrapper.state('foo')).to.equal('foo');
});

it('throws on host nodes', () => {
const wrapper = shallow(<div><span /></div>);

expect(() => wrapper.state()).to.throw(Error, 'ShallowWrapper::state() can only be called on class components');
});

itIf(is('>= 16'), 'throws on Portals', () => {
const containerDiv = { nodeType: 1 };
const portal = createPortal(
<div />,
containerDiv,
);

const wrapper = shallow(<div>{portal}</div>);
expect(() => wrapper.state()).to.throw(Error, 'ShallowWrapper::state() can only be called on class components');
});

describeIf(is('> 0.13'), 'stateless function components', () => {
it('throws on SFCs', () => {
function Foo() {
return <div />;
}

const wrapper = shallow(<Foo />);
expect(() => wrapper.state()).to.throw(Error, 'ShallowWrapper::state() can only be called on class components');
});
});
});

describe('.children([selector])', () => {
Expand Down

0 comments on commit dd275fe

Please sign in to comment.