Enzyme: shallow->setProps: props not being set

Created on 11 Dec 2018  路  5Comments  路  Source: enzymejs/enzyme

Current behavior

When running setProps. This results in it not being set in the most simple of scenarios:

simplified down to a basic component / test

import React from 'react';
import {configure, shallow} from 'enzyme';

import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });

const Component = ({prop1}) => <div>{prop1}</div>

describe.only('Component', () => {
  it('should set the props', () => {
    const component = shallow(<Component />);

    component.setProps({prop1: 'prop1'});

    expect(component.prop('prop1')).toEqual('prop1');
    expect(component.props()).toEqual({prop1: 'prop1'});
  });
});

result:

image

Expected behavior

shallow -> component
-> set props
-> props -> the props that were set

Your environment

Running this inside of jest (23.6.0) on Node (8.14.0).

API

  • [x] shallow
  • [ ] mount
  • [ ] render

Version

| library | version
| ------------------- | -------
| enzyme | 3.7.0
| react | 16.3.0
| react-dom | 16.3.0
| react-test-renderer | n/a
| adapter (below) | 1.7.0

Adapter

  • [ ] enzyme-adapter-react-16
  • [x] enzyme-adapter-react-16.3
  • [ ] enzyme-adapter-react-16.2
  • [ ] enzyme-adapter-react-16.1
  • [ ] enzyme-adapter-react-15
  • [ ] enzyme-adapter-react-15.4
  • [ ] enzyme-adapter-react-14
  • [ ] enzyme-adapter-react-13
  • [ ] enzyme-adapter-react-helper
  • [ ] others ( )
question

Most helpful comment

These answers don't help. What is .setProps() for if it's not expected to do anything?

All 5 comments

This is because you're confused about what the wrapper is - it's not Component, it's what component renders - in this case, a div with no props.

@ljharb
From docs:

shallow(node[, options]) => ShallowWrapper, ShallowWrapper: The wrapper instance around the rendered output.

...'it's not Component, it's what component renders'

@pablo-bl right - the rendered output. The docs agree with my comment.

These answers don't help. What is .setProps() for if it's not expected to do anything?

It certainly is expected to do something - it sets the props on the rendered element, not on the element whose render method is being evaluated. (for shallow)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

modemuser picture modemuser  路  3Comments

AdamYahid picture AdamYahid  路  3Comments

ahuth picture ahuth  路  3Comments

ivanbtrujillo picture ivanbtrujillo  路  3Comments

SandroMachado picture SandroMachado  路  3Comments