Last active
April 9, 2024 10:03
-
-
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
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 {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