Skip to content

Instantly share code, notes, and snippets.

@johnnolan
Last active April 9, 2024 10:03
Show Gist options
  • Save johnnolan/1c8075a9124506d75953b540adf7a3bd to your computer and use it in GitHub Desktop.
Save johnnolan/1c8075a9124506d75953b540adf7a3bd to your computer and use it in GitHub Desktop.
Example on how to test a React Higher Order Component using react-testing-library
import React from 'react'
import {render, cleanup} from 'react-testing-library'
// withHOC.js
function withHOC (WrappedComponent) {
render() {
return (
<main>
<section>
<h1>HOC Example</h1>
</section>
<seciton>
<WrappedComponent {...this.props} />
</seciton>
</main>
)
}
}
afterEach(cleanup)
class MockApp extends React.Component {
render () {
return (
<p>
Hello from your Mock App
</p>
)
}
}
const MockWithHOC = withHOC(MockApp)
test('can render with redux with defaults', () => {
const { container, getByText } = render(
<MockWithHOC />)
expect(getByText(/HOC Example/i)).toBeInTheDocument()
expect(getByText(/Hello from your Mock App/i)).toBeInTheDocument()
expect(container).toMatchSnapshot()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment