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

All snapshots are empty #9

Closed
astricus opened this issue May 14, 2020 · 2 comments
Closed

All snapshots are empty #9

astricus opened this issue May 14, 2020 · 2 comments

Comments

@astricus
Copy link
Contributor

All shapshots in the project are actually empty objects ShallowWrapper {}

@astricus
Copy link
Contributor Author

This issue is the same as #7802. To fix it enzyme-to-json should be added

@estebanrao
Copy link

Hi! I'm adding UT to my copy of this master project and run through this problem by checking that changes made to element properties were being tracked correctly by the snapshot check:

This is my GO TO CHECKOUT button:

<CustomButton
  as={Link}
  to="/checkout"
  onClick={() => dispatch(toggleCartHidden())}
>
  GO TO CHECKOUT
</CustomButton>

Note that I'm using the to shorthand instead of using history push, and wanted to make sure that if someone changes the /checkout by mistake, this issue was being tracked, but, to my surprise, it wasn't. That's when I realice that the snapshot file was "empty" (same with snapshot file in the cart-dropdown folder in this project).

After a lot of searching and trying I find that there's no need to use the enzyme-to-json serialiser. It will create a correct snapshot for the .toMatchSnapshot() method but it will not work for methods like .find() inside tests.

All I did was add the .debug() method before .toMatchSnapshot() and it did the trick. Since I wasn't able to find much documentation about this online it would be great to know if this is a working, stable solution.

This is my component:

import React from 'react';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { Link } from 'react-router-dom';

import CustomButton from '../custom-button/custom-button.component';
import CartItem from '../cart-item/cart-item.component';
import { selectCartItems } from '../../redux/cart/cart.selectors';
import { toggleCartHidden } from '../../redux/cart/cart.actions';

import {
  CartDropDownContainer,
  CartItemsContainer,
  EmptyMessage,
} from './cart-dropdown.styles';

export const CartDropdown = ({ cartItems, dispatch }) => {
  return (
    <CartDropDownContainer>
      <CartItemsContainer>
        {cartItems.length ? (
          cartItems.map((cartItem) => (
            <CartItem key={cartItem.id} item={cartItem} />
          ))
        ) : (
          <EmptyMessage>Your cart is empty</EmptyMessage>
        )}
      </CartItemsContainer>
<CustomButton
  as={Link}
  to="/checkout"
  onClick={() => dispatch(toggleCartHidden())}
>
  GO TO CHECKOUT
</CustomButton>
    </CartDropDownContainer>
  );
};

const mapStateToProps = createStructuredSelector({
  cartItems: selectCartItems,
});

export default connect(mapStateToProps)(CartDropdown);

And this my test file (running correctly)

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

import { CartDropdown } from './cart-dropdown.component';
import CartItem from '../cart-item/cart-item.component';

const mockProps = {
  cartItems: [],
  dispatch: jest.fn(),
};
let wrapper;

beforeEach(() => {
  wrapper = shallow(<CartDropdown {...mockProps} />);
});

describe('CartDropdown', () => {
  it('should render CardDropdown component correctly with empty message', () => {
    expect(wrapper.debug()).toMatchSnapshot(); // First snapshot created with EmptyMessage element
    expect(wrapper.find('EmptyMessage').length).toEqual(1);
  });

  it('should render CardDropdown component correctly with cart items passed in props', () => {
    wrapper.setProps({ cartItems: [{ id: 'a' }, { id: 'b' }] });
    expect(wrapper.debug()).toMatchSnapshot(); // Second snapshot created with CartItem elements
    expect(wrapper.find(CartItem).length).toEqual(2);
  });
});

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants