Skip to content

Instantly share code, notes, and snippets.

@squalvj
Created July 24, 2019 10:30
Show Gist options
  • Save squalvj/ec3ec6b624a03ee84f7e761179c9fa34 to your computer and use it in GitHub Desktop.
Save squalvj/ec3ec6b624a03ee84f7e761179c9fa34 to your computer and use it in GitHub Desktop.
Test manipulating state with jest
mport React from 'react';
import { shallow, mount, render } from 'enzyme';
import Todo from '../components/Todo';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
test('Test it', async () => {
// Render a checkbox with label in the document
const cart = [
{ name: 'Green', cost: 4 },
{ name: 'Red', cost: 8 },
{ name: 'Blue', cost: 14 }
];
const wrapper = mount(<Todo cart={cart} />);
const firstInput = wrapper.find('.name');
firstInput.simulate('change', { target: { value: 'Pink' } });
const firstCost = wrapper.find('.cost');
firstCost.simulate('change', { target: { value: 200 } });
const submitButton = wrapper.find('.addtocart');
submitButton.simulate('click');
wrapper.update();
expect(wrapper.state('price')).toBe(26);
console.log(wrapper.state());
console.log(wrapper.props().cart);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment