Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State should allow to check against primitives like [] #102

Open
weblogixx opened this issue Aug 29, 2016 · 1 comment
Open

State should allow to check against primitives like [] #102

weblogixx opened this issue Aug 29, 2016 · 1 comment

Comments

@weblogixx
Copy link

Hi all,

first of all, thank you for this plugin. It makes testing with enzyme a little less painful 👍.
I just wanted to try the .state check on a fairly simple component that currently looks like this:

class MyComponent extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      items: []
    };
  }
}

The test looks like this:

describe('MyComponent', function() {

  describe('when initializing the component', function() {
    it('should set a default state', function() {
      const component = shallow(<Component />);
      expect(component).to.have.state('items', []);
    });
  });
});

_Current behaviour:_
AssertionError: expected to have a 'items' state with the value [], but the value was [].

_Expected Behaviour:_
I would expect the output to be true, e.g. a deep check should happen instead of a regular one.

_Additional Input:_
It would be nice if we could also write something like this if the first parameter of state is a plain object to deep check everything:

expect(MyComponent).to.have.state({
  items: []
});

This would be equivalent to the currently used function below:

expect(MyComponent.state()).to.deep.equal({
  items: []
});

Thank you again for writing this beauty, hope the feedback is helpful for you.

@ayrton
Copy link
Contributor

ayrton commented Nov 4, 2016

chai-enzyme doesn't do any deep comparisons by default, nor does chai afaik, you have to explicitly add the flag:

const component = shallow(<Component />);
expect(component).to.have.state('items').deep.equal([]);

I agree that the following would be nice to have:

expect(MyComponent).to.have.state({ foo: 'foo' });
expect(MyComponent).to.have.deep.state({ items: [] });

Similar to props, PRs are welcome.

@ayrton ayrton changed the title Bug (Feature-Request?): State should allow to check against primitives like [] State should allow to check against primitives like [] Nov 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants