Created
August 26, 2017 22:37
-
-
Save Tahseenm/47e8eaecba00d16153b5cba7d85bebe2 to your computer and use it in GitHub Desktop.
React Component testing examples with Mocha, expect and React-Test-Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import ShallowRenderer from 'react-test-renderer/shallow' | |
import expect from 'expect' | |
import expectJSX from 'expect-jsx' | |
expect.extend(expectJSX) | |
// Component | |
const Greeting = ({greeting}) => ( | |
<main className="greeting-wrap"> | |
<p>Greeting</p> | |
<h1>{greeting}</h1> | |
</main> | |
) | |
describe('Greeting', () => { | |
let greeting | |
beforeEach(() => { | |
const renderer = new ShallowRenderer() | |
renderer.render(<Greeting greeting="Hello World!" />) | |
greeting = renderer.getRenderOutput() | |
}) | |
it('should be the correct element type', () => { | |
expect(greeting.type).toBe('main') | |
}) | |
it('should render the correct greeting message', () => { | |
expect(greeting).toIncludeJSX(<h1>Hello World!</h1>) | |
}) | |
it('should have the right class', () => { | |
expect(greeting.props.className).toMatch(/greeting-wrap/) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment