Skip to content

Instantly share code, notes, and snippets.

@Tahseenm
Created August 26, 2017 22:37
Show Gist options
  • Save Tahseenm/47e8eaecba00d16153b5cba7d85bebe2 to your computer and use it in GitHub Desktop.
Save Tahseenm/47e8eaecba00d16153b5cba7d85bebe2 to your computer and use it in GitHub Desktop.
React Component testing examples with Mocha, expect and React-Test-Utils
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