Enzyme: Shallow(<SomeComponent />).is(SomeComponent) returns false

Created on 12 Jan 2017  路  14Comments  路  Source: enzymejs/enzyme

Basically, this fails:

it('should be true', () => {
  const SomeComponent = () => (<div></div>);
  expect(shallow(<SomeComponent />).is(SomeComponent)).to.be.true;
});

while this works:

  // require('jsdom')...
  it('should be true', () => {
    const SomeComponent = () => (<div></div>);
    expect(mount(<SomeComponent />).is(SomeComponent)).to.be.true;
  });

Am I wrong to assume the first one should work too?

mount bug help wanted

All 14 comments

This seems like a duplicate of #506.

Looks like the underline reason for this problem is the same as #506 but it manifests differently.

@hesher the mounted component isn't a SomeComponent. it's a div.

The strange part imo is that the mount test passes.

enzyme doesn't have a test case for mounting a Composite Component.

if I put the following test, the test is failed only in mount.

    it('should handle Composite Components', () => {
      const Hello = () => <div>foo</div>;
      const wrapper = mount(<Hello />);
      // const wrapper = shallow(<Hello />);
      expect(wrapper.is('div')).to.equal(true);
    });

Should we fixed it?

@ljharb I think we can fix this in mount by checking if the root node is a composite and passing the results of childrenOfInst to the predicate instead of the node itself.

@aweary that seems reasonable.

I would like to work on this issue if no one has picked up.

This will be my first contribution to enzyme. Please guide me on this. Thanks

@hanumanthan Please do! I'm not sure what guidance you need.

@ljharb @hanumanthan Is anybody working on this?

Not that I'm aware of; please feel free to submit a PR!

@ljharb All right thanks. On it.

@ljharb childrenOfInst returns an empty array with this:

  is(selector) {
    const predicate = buildInstPredicate(selector);
    const cb = n => (typeof typeOfNode(getNode(n)) === 'function' ? predicate(childrenOfInst(getNode(n))) : predicate(n));
    return this.single('is', cb);
  }
it.only('should handle Composite Components', () => {
      const Hello = () => <div>foo</div>;
      const wrapper = mount(<Hello />);
      expect(wrapper.is('div')).to.equal(true);
    });

Or am I missing something? Thanks.

I'd expect that test to pass; I'm not sure what you're asking otherwise.

@ljharb Yeah I get the idea. I am actually following on @aweary suggestion by passing result of childrenOfInst to predicate but its returning an empty array.

 is(selector) {
    const predicate = buildInstPredicate(selector);
    const cb = n => (typeof typeOfNode(getNode(n)) === 'function' ? predicate(childrenOfInst(getNode(n))) : predicate(n));
    return this.single('is', cb);
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

SandroMachado picture SandroMachado  路  3Comments

aweary picture aweary  路  3Comments

abe903 picture abe903  路  3Comments

ivanbtrujillo picture ivanbtrujillo  路  3Comments

mattkauffman23 picture mattkauffman23  路  3Comments