JavaScript How to use Enzyme Shallow with Jest snapshots enzyme shallow with context,enzyme shallow with props,enzyme shallow

I'm trying to use shallow from enzyme and also use snapshots from jest together.

The problem I'm facing is that I need to change something from the state using setState() from enzyme and then match the result with the snapshot.

See the code of my component:

....

getPrevProduct = () => {
  const key = this.state.indexCurrent > 0 &&
   this.productsKeys[this.state.indexCurrent - 1];

  let product = null;

  if (key) {
    product = this.props.products[key];
  }

  return product;
}

renderPrev = () => this.getPrevProduct() && <PrevButton />;

render() {
  return (
    <div>
      ...
      {this.renderPrev()}
      ...
    <div>
  )
}

The previous code checks if the product, passed from props in the currentIndex, exists.

That's why I need Enzyme to change the state. Then, if it matches, <PrevButton /> has to be rendered.

This test is when I want to match the component with the snapshot:

const nextProps = {
  products: {
    test: 'test'
  }
};

const tree = create(<Component nextProps={nextProps} />).toJSON();

expect(tree).toMatchSnapshot();

But I need to change the state. How I can do that? This doesn't work:

it('should render component with prev button', () => {
  const nextProps = {
    products: {
      test: 'test'
    }
  };
  const instance = shallow(<Component nextProps={nextProps} />).instance()

  instance.setState({
    indexCurrent: 1
  });

  const tree = create(<Component nextProps={nextProps} />).toJSON();

  expect(tree).toMatchSnapshot();
});

I also tried to save the declaration of the component into a variable like the next uncompleted code and use it in shallow and create:

const component = <Component nextProps={nextProps} />;

 shallow(component).instance()).instance()
 create(component).toJSON()`

I also tried with no luck enzyme-to-json.

What would you do?

Answer:1

Probably, its not the way enzyme-to-json is supposed to be used. Try this way :

import { shallowToJson  } from 'enzyme-to-json';
import { shallow } from 'enzyme';

and then in your test :

const wrapper = shallow(<Component />);
expect(shallowToJson(wrapper)).toMatchSnapshot();
Answer:2

I have a little problem. I have a checkbox for showing div. I can choose from 6 different checkboxes to select which div I want to show. When check checkboxes I show div and send to server checked ...

I have a little problem. I have a checkbox for showing div. I can choose from 6 different checkboxes to select which div I want to show. When check checkboxes I show div and send to server checked ...

  1. splice from array javascript
  2. splice from array js
  3. splice from array of objects
  4. splice from array typescript
  5. splice from array
  6. splice from array angularjs
  7. splice from array by value
  8. splice from array jquery
  9. array slice using value
  10. splice array from index to end
  11. splice array from index
  12. splice element from array javascript
  13. splice item from array javascript
  14. splice object from array javascript
  15. splice object from array typescript
  16. splice value from array javascript
  17. splice element from array
  18. splice item from array
  19. splice remove from array
  20. splice data from array

I have the following g:submitButton: <g:submitButton name="next" class="signup-button skyblue-btn pull-right pl-pr-36" value="${message(code:"buttons.ok")}" onclick="return showSpinnerSignUp()"/&...

I have the following g:submitButton: <g:submitButton name="next" class="signup-button skyblue-btn pull-right pl-pr-36" value="${message(code:"buttons.ok")}" onclick="return showSpinnerSignUp()"/&...

  1. input type button onclick doesn't work

When importing a basic component I have webpack crash on me. I'm unsure as to why this is happening but it seems to be a missing lib within the ag-grid-react package... Has anyone run into this before?...

When importing a basic component I have webpack crash on me. I'm unsure as to why this is happening but it seems to be a missing lib within the ag-grid-react package... Has anyone run into this before?...

Am new to javascript i foud this one in w3schools.com - Entering pincode in search box will show in <p> tag !! How to do ? any help appreciated. Thanks in advance HTML : <form class="...

Am new to javascript i foud this one in w3schools.com - Entering pincode in search box will show in <p> tag !! How to do ? any help appreciated. Thanks in advance HTML : <form class="...