title: How to test react component with hooks using react testing library
body: <p>I am trying to test a component which use useTheme hook provided by emotion.js. The theme is set during the app initialization.</p>
  
  <p>Now when I write my test cases, the useTheme hook is unable to fetch the styles data as only header component is getting mounted for testing. How to mock the data provided by hooks.</p>
  
  <p>app.js</p>
  
  <pre><code>import { theme } from '../src/utils/styles/themes/default';
  
  const AppRoot = ({ path, Router }) =&gt; {
    const routeRenderFunction = (props) =&gt; &lt;RouteHandler route={props} /&gt;;
    return (
          &lt;ThemeProvider theme={theme}&gt;
            &lt;Router location={path} context={{}}&gt;
              &lt;Switch&gt;
                {routePatterns.map((routePattern) =&gt; (
                  &lt;Route key={routePattern} path={routePattern} render={routeRenderFunction} /&gt;
                ))}
              &lt;/Switch&gt;
            &lt;/Router&gt;          
          &lt;/ThemeProvider&gt;      
    );
  };
  </code></pre>
  
  <p>header.js</p>
  
  <pre><code>import React from "react";
  import { css } from "emotion";
  import { useTheme } from "emotion-theming";
  import * as styles from "./Header.style";
  
  const Header = ({userName = 'Becky Parsons', clinicName = 'The University of Southampton'}) =&gt; {
    const theme = useTheme();
  
    return (
      &lt;div className={css(styles.accountDetails(theme))}&gt;
        &lt;div className={css(styles.accountContainer)}&gt;
          &lt;div className={css(styles.accountSpecifics)}&gt;
            &lt;h5 className={css(styles.accountDetailsH5(theme))} data-testid="user-name"&gt;{userName}&lt;/h5&gt;
            &lt;h6 className={css(styles.accountDetailsH6(theme))} data-testid="clinic-name"&gt;
              {clinicName}
            &lt;/h6&gt;
          &lt;/div&gt;
          &lt;div className={css(styles.avatar)} /&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    );
  };
  
  export default Header;
  </code></pre>
  
  <p>header.test.js</p>
  
  <pre><code>import React from 'react'
  import {render} from '@testing-library/react';
  import Header from './Header';
  
  
  test('Check if header component loads', () =&gt; {    
      const { getByTestId } = render(&lt;Header userName='Becky Parsons' clinicName='The University of Southampton'/&gt;);
      expect(getByTestId('user-name').textContent).toBe('Becky Parsons');
      expect(getByTestId('clinic-name').textContent).toBe('The University of Southampton');
    })
  </code></pre>
  
  <p>header.style.js</p>
  
  <pre><code>export const accountDetails = theme =&gt; ({
    height: '70px',
    backgroundColor: theme.header.backgroundColor,
    textAlign: 'right',
    padding: '10px 40px'
  });
  export const accountContainer = {
    display: 'flex',
    justifyContent: 'flex-end'
  };
  </code></pre>
  
  <p>Error: Uncaught [TypeError: Cannot read property 'backgroundColor' of undefined]</p>