How do I mock the implementation of material-ui withStyles?
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
add a comment |
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
add a comment |
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
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
reactjs mocking jestjs material-ui
edited Nov 6 '18 at 7:19
skyboyer
3,44111128
3,44111128
asked Jul 3 '18 at 18:27
Developer DavoDeveloper Davo
380414
380414
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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)
add a comment |
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.
add a comment |
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);
The question is about mockingwithStyles
for testing. It doesn't look like your answer addresses tests or mocking.
– stone
Nov 13 '18 at 1:41
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
answered Jul 5 '18 at 18:38
Developer DavoDeveloper Davo
380414
380414
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 13 '18 at 7:20
stonestone
4,5453859
4,5453859
add a comment |
add a comment |
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);
The question is about mockingwithStyles
for testing. It doesn't look like your answer addresses tests or mocking.
– stone
Nov 13 '18 at 1:41
add a comment |
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);
The question is about mockingwithStyles
for testing. It doesn't look like your answer addresses tests or mocking.
– stone
Nov 13 '18 at 1:41
add a comment |
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);
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);
answered Nov 13 '18 at 1:36
user2600410user2600410
111
111
The question is about mockingwithStyles
for testing. It doesn't look like your answer addresses tests or mocking.
– stone
Nov 13 '18 at 1:41
add a comment |
The question is about mockingwithStyles
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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