How do I mock the implementation of material-ui withStyles?












1















How do I mock the implementation of withStyles in material-ui/core/styles.js?



Here is the test:



import React from 'react'
import { shallow } from 'enzyme'
import { withStyles } from '@material-ui/core/styles'

import TempComponent from './TempComponent'

jest.mock('@material-ui/core')

it('renders correctly', () => {
const withStylesFake = styles =>
component => (
component
)

withStyles.mockImplementation(withStylesFake)

const wrapper = shallow(<TempComponent />)
expect(wrapper).toMatchSnapshot()
})


Here is the code:



import React from 'react'
import { withStyles } from '@material-ui/core/styles'

const TempComponent = () => (
<button>Click Me!</button>
)

export default withStyles({})(TempComponent)


Here is the error:



TypeError: _styles.withStyles.mockImplementation is not a function

at Object.<anonymous>.it (src/TempComponent.snapshot.test.js:15:22)
at new Promise (<anonymous>)
at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)


This would work:



// ./__mocks__/@material-ui/core/styles
export const withStyles = styles => (
component => (
component
)
)


but this is not local to the test.










share|improve this question





























    1















    How do I mock the implementation of withStyles in material-ui/core/styles.js?



    Here is the test:



    import React from 'react'
    import { shallow } from 'enzyme'
    import { withStyles } from '@material-ui/core/styles'

    import TempComponent from './TempComponent'

    jest.mock('@material-ui/core')

    it('renders correctly', () => {
    const withStylesFake = styles =>
    component => (
    component
    )

    withStyles.mockImplementation(withStylesFake)

    const wrapper = shallow(<TempComponent />)
    expect(wrapper).toMatchSnapshot()
    })


    Here is the code:



    import React from 'react'
    import { withStyles } from '@material-ui/core/styles'

    const TempComponent = () => (
    <button>Click Me!</button>
    )

    export default withStyles({})(TempComponent)


    Here is the error:



    TypeError: _styles.withStyles.mockImplementation is not a function

    at Object.<anonymous>.it (src/TempComponent.snapshot.test.js:15:22)
    at new Promise (<anonymous>)
    at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)


    This would work:



    // ./__mocks__/@material-ui/core/styles
    export const withStyles = styles => (
    component => (
    component
    )
    )


    but this is not local to the test.










    share|improve this question



























      1












      1








      1








      How do I mock the implementation of withStyles in material-ui/core/styles.js?



      Here is the test:



      import React from 'react'
      import { shallow } from 'enzyme'
      import { withStyles } from '@material-ui/core/styles'

      import TempComponent from './TempComponent'

      jest.mock('@material-ui/core')

      it('renders correctly', () => {
      const withStylesFake = styles =>
      component => (
      component
      )

      withStyles.mockImplementation(withStylesFake)

      const wrapper = shallow(<TempComponent />)
      expect(wrapper).toMatchSnapshot()
      })


      Here is the code:



      import React from 'react'
      import { withStyles } from '@material-ui/core/styles'

      const TempComponent = () => (
      <button>Click Me!</button>
      )

      export default withStyles({})(TempComponent)


      Here is the error:



      TypeError: _styles.withStyles.mockImplementation is not a function

      at Object.<anonymous>.it (src/TempComponent.snapshot.test.js:15:22)
      at new Promise (<anonymous>)
      at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)


      This would work:



      // ./__mocks__/@material-ui/core/styles
      export const withStyles = styles => (
      component => (
      component
      )
      )


      but this is not local to the test.










      share|improve this question
















      How do I mock the implementation of withStyles in material-ui/core/styles.js?



      Here is the test:



      import React from 'react'
      import { shallow } from 'enzyme'
      import { withStyles } from '@material-ui/core/styles'

      import TempComponent from './TempComponent'

      jest.mock('@material-ui/core')

      it('renders correctly', () => {
      const withStylesFake = styles =>
      component => (
      component
      )

      withStyles.mockImplementation(withStylesFake)

      const wrapper = shallow(<TempComponent />)
      expect(wrapper).toMatchSnapshot()
      })


      Here is the code:



      import React from 'react'
      import { withStyles } from '@material-ui/core/styles'

      const TempComponent = () => (
      <button>Click Me!</button>
      )

      export default withStyles({})(TempComponent)


      Here is the error:



      TypeError: _styles.withStyles.mockImplementation is not a function

      at Object.<anonymous>.it (src/TempComponent.snapshot.test.js:15:22)
      at new Promise (<anonymous>)
      at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)


      This would work:



      // ./__mocks__/@material-ui/core/styles
      export const withStyles = styles => (
      component => (
      component
      )
      )


      but this is not local to the test.







      reactjs mocking jestjs material-ui






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 6 '18 at 7:19









      skyboyer

      3,44111128




      3,44111128










      asked Jul 3 '18 at 18:27









      Developer DavoDeveloper Davo

      380414




      380414
























          3 Answers
          3






          active

          oldest

          votes


















          2














          The way I found around this was to export the component as follows. No mocks necessary.



          Test:



          import React from 'react'
          import { shallow } from 'enzyme'

          import { TempComponent } from './TempComponent'

          it('renders correctly', () => {
          const wrapper = shallow(<TempComponent />)
          expect(wrapper).toMatchSnapshot()
          })


          Implementation:



          import React from 'react'
          import { withStyles } from '@material-ui/core/styles'

          export const TempComponent = () => (
          <button>Click Me!</button>
          )

          export default withStyles({})(TempComponent)





          share|improve this answer































            1














            Because the TempComponent import is evaluated before your test code, you'll need to mock withStyles earlier in the process. Calling mockImplementation occurs too late in the process.



            There are two ways to do that: pass a factory into jest.mock, or use a manual mock. The manual mock is working for you, but you say you want something local to the test, so you'll want to use the factory parameter. Here's how.



            Mock the styles import instead of the core import. Use the second arg to jest.mock, the "module factory parameter," to pass in a module factory function: a function that returns an object that replaces the styles import. For your purposes, that means the object needs to have a withStyles function:



            jest.mock('@material-ui/core/styles', () => ({
            withStyles: styles => component => component
            }));


            No need to import withStyles into your test or call mockImplementation; you can delete those lines.






            share|improve this answer































              0














              This is a way I used to define component style with material UI.



              import { withStyles } from '@material-ui/core/styles';

              const styles = theme => ({
              root: {
              ...theme.mixins.gutters(),
              paddingTop: theme.spacing.unit * 2,
              paddingBottom: theme.spacing.unit * 2,
              marginBottom: theme.spacing.unit * 3,
              maxWidth: theme.spacing.unit * 120,
              overflowX: 'auto',
              },
              });

              function PaperSheet() {
              return (
              <Paper className={classes.root} elevation={1} />
              );
              }

              export default withStyles(styles)(PaperSheet);





              share|improve this answer
























              • The question is about mocking withStyles for testing. It doesn't look like your answer addresses tests or mocking.

                – stone
                Nov 13 '18 at 1:41











              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51160904%2fhow-do-i-mock-the-implementation-of-material-ui-withstyles%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              The way I found around this was to export the component as follows. No mocks necessary.



              Test:



              import React from 'react'
              import { shallow } from 'enzyme'

              import { TempComponent } from './TempComponent'

              it('renders correctly', () => {
              const wrapper = shallow(<TempComponent />)
              expect(wrapper).toMatchSnapshot()
              })


              Implementation:



              import React from 'react'
              import { withStyles } from '@material-ui/core/styles'

              export const TempComponent = () => (
              <button>Click Me!</button>
              )

              export default withStyles({})(TempComponent)





              share|improve this answer




























                2














                The way I found around this was to export the component as follows. No mocks necessary.



                Test:



                import React from 'react'
                import { shallow } from 'enzyme'

                import { TempComponent } from './TempComponent'

                it('renders correctly', () => {
                const wrapper = shallow(<TempComponent />)
                expect(wrapper).toMatchSnapshot()
                })


                Implementation:



                import React from 'react'
                import { withStyles } from '@material-ui/core/styles'

                export const TempComponent = () => (
                <button>Click Me!</button>
                )

                export default withStyles({})(TempComponent)





                share|improve this answer


























                  2












                  2








                  2







                  The way I found around this was to export the component as follows. No mocks necessary.



                  Test:



                  import React from 'react'
                  import { shallow } from 'enzyme'

                  import { TempComponent } from './TempComponent'

                  it('renders correctly', () => {
                  const wrapper = shallow(<TempComponent />)
                  expect(wrapper).toMatchSnapshot()
                  })


                  Implementation:



                  import React from 'react'
                  import { withStyles } from '@material-ui/core/styles'

                  export const TempComponent = () => (
                  <button>Click Me!</button>
                  )

                  export default withStyles({})(TempComponent)





                  share|improve this answer













                  The way I found around this was to export the component as follows. No mocks necessary.



                  Test:



                  import React from 'react'
                  import { shallow } from 'enzyme'

                  import { TempComponent } from './TempComponent'

                  it('renders correctly', () => {
                  const wrapper = shallow(<TempComponent />)
                  expect(wrapper).toMatchSnapshot()
                  })


                  Implementation:



                  import React from 'react'
                  import { withStyles } from '@material-ui/core/styles'

                  export const TempComponent = () => (
                  <button>Click Me!</button>
                  )

                  export default withStyles({})(TempComponent)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 5 '18 at 18:38









                  Developer DavoDeveloper Davo

                  380414




                  380414

























                      1














                      Because the TempComponent import is evaluated before your test code, you'll need to mock withStyles earlier in the process. Calling mockImplementation occurs too late in the process.



                      There are two ways to do that: pass a factory into jest.mock, or use a manual mock. The manual mock is working for you, but you say you want something local to the test, so you'll want to use the factory parameter. Here's how.



                      Mock the styles import instead of the core import. Use the second arg to jest.mock, the "module factory parameter," to pass in a module factory function: a function that returns an object that replaces the styles import. For your purposes, that means the object needs to have a withStyles function:



                      jest.mock('@material-ui/core/styles', () => ({
                      withStyles: styles => component => component
                      }));


                      No need to import withStyles into your test or call mockImplementation; you can delete those lines.






                      share|improve this answer




























                        1














                        Because the TempComponent import is evaluated before your test code, you'll need to mock withStyles earlier in the process. Calling mockImplementation occurs too late in the process.



                        There are two ways to do that: pass a factory into jest.mock, or use a manual mock. The manual mock is working for you, but you say you want something local to the test, so you'll want to use the factory parameter. Here's how.



                        Mock the styles import instead of the core import. Use the second arg to jest.mock, the "module factory parameter," to pass in a module factory function: a function that returns an object that replaces the styles import. For your purposes, that means the object needs to have a withStyles function:



                        jest.mock('@material-ui/core/styles', () => ({
                        withStyles: styles => component => component
                        }));


                        No need to import withStyles into your test or call mockImplementation; you can delete those lines.






                        share|improve this answer


























                          1












                          1








                          1







                          Because the TempComponent import is evaluated before your test code, you'll need to mock withStyles earlier in the process. Calling mockImplementation occurs too late in the process.



                          There are two ways to do that: pass a factory into jest.mock, or use a manual mock. The manual mock is working for you, but you say you want something local to the test, so you'll want to use the factory parameter. Here's how.



                          Mock the styles import instead of the core import. Use the second arg to jest.mock, the "module factory parameter," to pass in a module factory function: a function that returns an object that replaces the styles import. For your purposes, that means the object needs to have a withStyles function:



                          jest.mock('@material-ui/core/styles', () => ({
                          withStyles: styles => component => component
                          }));


                          No need to import withStyles into your test or call mockImplementation; you can delete those lines.






                          share|improve this answer













                          Because the TempComponent import is evaluated before your test code, you'll need to mock withStyles earlier in the process. Calling mockImplementation occurs too late in the process.



                          There are two ways to do that: pass a factory into jest.mock, or use a manual mock. The manual mock is working for you, but you say you want something local to the test, so you'll want to use the factory parameter. Here's how.



                          Mock the styles import instead of the core import. Use the second arg to jest.mock, the "module factory parameter," to pass in a module factory function: a function that returns an object that replaces the styles import. For your purposes, that means the object needs to have a withStyles function:



                          jest.mock('@material-ui/core/styles', () => ({
                          withStyles: styles => component => component
                          }));


                          No need to import withStyles into your test or call mockImplementation; you can delete those lines.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 7:20









                          stonestone

                          4,5453859




                          4,5453859























                              0














                              This is a way I used to define component style with material UI.



                              import { withStyles } from '@material-ui/core/styles';

                              const styles = theme => ({
                              root: {
                              ...theme.mixins.gutters(),
                              paddingTop: theme.spacing.unit * 2,
                              paddingBottom: theme.spacing.unit * 2,
                              marginBottom: theme.spacing.unit * 3,
                              maxWidth: theme.spacing.unit * 120,
                              overflowX: 'auto',
                              },
                              });

                              function PaperSheet() {
                              return (
                              <Paper className={classes.root} elevation={1} />
                              );
                              }

                              export default withStyles(styles)(PaperSheet);





                              share|improve this answer
























                              • The question is about mocking withStyles for testing. It doesn't look like your answer addresses tests or mocking.

                                – stone
                                Nov 13 '18 at 1:41
















                              0














                              This is a way I used to define component style with material UI.



                              import { withStyles } from '@material-ui/core/styles';

                              const styles = theme => ({
                              root: {
                              ...theme.mixins.gutters(),
                              paddingTop: theme.spacing.unit * 2,
                              paddingBottom: theme.spacing.unit * 2,
                              marginBottom: theme.spacing.unit * 3,
                              maxWidth: theme.spacing.unit * 120,
                              overflowX: 'auto',
                              },
                              });

                              function PaperSheet() {
                              return (
                              <Paper className={classes.root} elevation={1} />
                              );
                              }

                              export default withStyles(styles)(PaperSheet);





                              share|improve this answer
























                              • The question is about mocking withStyles for testing. It doesn't look like your answer addresses tests or mocking.

                                – stone
                                Nov 13 '18 at 1:41














                              0












                              0








                              0







                              This is a way I used to define component style with material UI.



                              import { withStyles } from '@material-ui/core/styles';

                              const styles = theme => ({
                              root: {
                              ...theme.mixins.gutters(),
                              paddingTop: theme.spacing.unit * 2,
                              paddingBottom: theme.spacing.unit * 2,
                              marginBottom: theme.spacing.unit * 3,
                              maxWidth: theme.spacing.unit * 120,
                              overflowX: 'auto',
                              },
                              });

                              function PaperSheet() {
                              return (
                              <Paper className={classes.root} elevation={1} />
                              );
                              }

                              export default withStyles(styles)(PaperSheet);





                              share|improve this answer













                              This is a way I used to define component style with material UI.



                              import { withStyles } from '@material-ui/core/styles';

                              const styles = theme => ({
                              root: {
                              ...theme.mixins.gutters(),
                              paddingTop: theme.spacing.unit * 2,
                              paddingBottom: theme.spacing.unit * 2,
                              marginBottom: theme.spacing.unit * 3,
                              maxWidth: theme.spacing.unit * 120,
                              overflowX: 'auto',
                              },
                              });

                              function PaperSheet() {
                              return (
                              <Paper className={classes.root} elevation={1} />
                              );
                              }

                              export default withStyles(styles)(PaperSheet);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 13 '18 at 1:36









                              user2600410user2600410

                              111




                              111













                              • The question is about mocking withStyles for testing. It doesn't look like your answer addresses tests or mocking.

                                – stone
                                Nov 13 '18 at 1:41



















                              • The question is about mocking withStyles for testing. It doesn't look like your answer addresses tests or mocking.

                                – stone
                                Nov 13 '18 at 1:41

















                              The question is about mocking withStyles for testing. It doesn't look like your answer addresses tests or mocking.

                              – stone
                              Nov 13 '18 at 1:41





                              The question is about mocking withStyles for testing. It doesn't look like your answer addresses tests or mocking.

                              – stone
                              Nov 13 '18 at 1:41


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51160904%2fhow-do-i-mock-the-implementation-of-material-ui-withstyles%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues