question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing with custom theme palette

See original GitHub issue

Problem description

If I create a custom theme and test a component that is leveraging a custom object from that theme, tests fail because that custom field is undefined, is there a documented way to test components with custom themes?

Steps to reproduce

Create a custom theme:

customTheme = {
  "direction": "ltr",
  "palette": {
     "customObject": {
         "customField1": "#FF0000"
     },
......
}

Shallow render custom component with a custom theme.

shallow(
<MuiThemeProvider theme={customTheme} >
<ComponentThatUsesCustomField1 />
</MuiThemeProvider
).dive()

Will throw an undefined error at customObject.

Versions

  • Material-UI: alpha v7
  • React: 15.6.1
  • Browser: latest

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
JavierLightcommented, Nov 16, 2018

Hello,

I’m experiencing a similar issue. I’ve wrapped a TextField in a MuiThemeProvider, so when using jest tests fail:

createMuiTheme -> createPalette -> augmentColor ()

It seems that it’s unable to get the palette in the tests, even though it’s working perfectly in the rest of scenarios (no browser console logs complaining when NODE_ENV='development', so our palette is well defined).

Is there any workaround to include this context for our tests (with shallow)? Maybe the augmentColor should not be raising an error if NODE_ENV='test' ?

Thank you very much for your help.

2reactions
stocky37commented, Sep 25, 2018

I worked around this by manually adding the custom theme in to the context like so:

import {CHANNEL} from '@material-ui/core/styles/themeListener';
import createBroadcast from 'brcast';

const broadcast = createBroadcast();
broadcast.setState(customTheme);
const wrapper = shallow(<ComponentThatUsesCustomField1 />, {
  context: {
    [CHANNEL]: broadcast,
  },
});

You can use the above logic to wrap createShallow or make it cleaner in any number of ways.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing with custom theme palette · Issue #8000 · mui/material-ui
Problem description If I create a custom theme and test a component that is leveraging a custom object from that theme, tests fail...
Read more >
Material-UI custom theme palette property yielding 'undefined ...
I encountered this today, the solution that fixed for me is this const useStyles = makeStyles((theme) => { return { ctaCopy: { color:...
Read more >
Accelerating GitHub theme creation with color tooling
... a dedicated color tool and how they use it to create new color palettes for GitHub. ... Defining tones; Choosing colors; Testing...
Read more >
Create a Customized Color Theme in Material-UI - Medium
In this article, I want to explain how to get started with Material-UI and customize a color palette theme to use for your...
Read more >
Designing a Material Theme: Color
Crafting the perfect palette with Material Design and Figma. ... as well as slots for a custom background color, surface color (for elevated ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found