Daily Archives: 18.01.2018
Testing react’s dom with jest and enzyme
Install it like this:
| 1 | yarn add enzyme enzyme-adapter-react-16 --registry="https://registry.npmjs.org" | 
Regression testing of react app with jest
Example is here — https://facebook.github.io/jest/docs/en/tutorial-react.html. The main idea is this:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // Link.react.test.js import React from 'react'; import Link from '../Link.react'; import renderer from 'react-test-renderer'; test('Link changes the class when hovered', () => {   const component = renderer.create(     <Link page="http://www.facebook.com">Facebook</Link>,   );   let tree = component.toJSON();   expect(tree).toMatchSnapshot();   // manually trigger the callback   tree.props.onMouseEnter();   // re-rendering   tree = component.toJSON();   expect(tree).toMatchSnapshot();   // manually trigger the callback   tree.props.onMouseLeave();   // re-rendering   tree = component.toJSON();   expect(tree).toMatchSnapshot(); }); |