Skip to content

Instantly share code, notes, and snippets.

@rubygeek
Last active October 3, 2018 16:54
Show Gist options
  • Save rubygeek/aa533cd5919bd2bcb19174e7ca1e3a3b to your computer and use it in GitHub Desktop.
Save rubygeek/aa533cd5919bd2bcb19174e7ca1e3a3b to your computer and use it in GitHub Desktop.
react testing with enzyme, react 16

sample in JS

portion of package.json

  "dependencies": {
    "enzyme": "^3.6.0",
    "enzyme-adapter-react-16": "^1.5.0",

SetupTests.js

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

simple component


import React from "react";

export default class Scoreboard extends React.Component {
    render() {
        return (
		    <div>
		      <ul>
            <li> Score 1 </li>
		        <li> Score 2 </li>
	        </ul>
        </div>);
    };

}

The test

import { shallow } from 'enzyme';
import Scoreboard from "./scoreboard"

it('renders finds 2 scores', () => {
    const mountedScoreboard = shallow(<Scoreboard />);
    const scores = mountedScoreboard.find("li");
    expect(scores.length).toBe(2);
});

it('first score value is correct', () => {
    const mountedScoreboard = shallow(<Scoreboard />);
    const scores = mountedScoreboard.find("li");
    expect(scores.first().text()).toBe(" Score 1 ");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment