New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing a component while using ThemeProvider #1319
Comments
Just wrap the component in a ThemeProvider in the Jest test file. |
With enzyme then I can't access to the component I'm interested in, since some operation works only on the root, in this case it would be ThemeProvider... |
You can wrap your components in a ThemeProvider with some theme
UPDATE: This approach doesn't work anymore. PLEASE DON'T USE IT. |
There was an entire issue dedicated to this question: styled-components/jest-styled-components#61 There is also a link to an issue on this repo in there that has more suggestions. But this issue was resolved with an addition to the readme there that seemingly hasn’t been added to our site yet (PRs welcome 🙂) |
Is there any solution to wrap shallow/mount with Theme in styled-components version 4? I used @VladimirPesterev solution and it worked in the previous version. |
@galina-niukhalova a (hackish) solution I've found is setting the context directly on the ThemeConsumer as follow:
|
@arka-na Thank you for your answer. But I found another solution, which works for me.
https://airbnb.io/enzyme/docs/api/ShallowWrapper/setContext.html |
Facing the same issue. following was working fine with version 2
But below one is not working after making change . Following example is using v4
I need to check the props of child component hence I need to use .dive test case
it is not able to find the props of |
@jitenderchand1 In either way you are trying to access wrapper.find('Styled(SearchButton)') |
Has anyone found a way to test ThemeProvider with v4? While @arka-na 's solution works but it's not adequate for my test purpose. |
Bump on the above, has anyone managed to get a working solution for version 4 of |
+1 |
Assuming we want consistent access to one theme, Is there any reason not to apply @arka-na 's solution above in test setup? in my jest ThemeConsumer._currentValue = theme |
The main reason: |
There is ongoing work at Enzyme to bring full support for the Context API in shallow renders. See references from styled-components/jest-styled-components#191 (comment) |
For new versions like this
|
I fix this problem following with answer in here export function mountWithTheme(child) {
return mount(child, {
wrappingComponent: ({ children }) => <ThemeProvider theme={TEST_THEME}>{children}</ThemeProvider>,
});
}
export function shallowWithTheme(child) {
return shallow(child, {
wrappingComponent: ({ children }) => <ThemeProvider theme={TEST_THEME}>{children}</ThemeProvider>,
});
} |
This works like a charm 👍 Thanks |
Am I the only person this doesn't work for? I have a feeling this is related to |
If you are using react-testing-library, this approach is considerable https://testing-library.com/docs/react-testing-library/setup#custom-render |
I do like @faizrktm's suggestion for overriding the render method if you use react-testing-library and I would suggest going that route, however if you would prefer a solution similar to the others suggested in this thread you could also achieve it using something like the following: import { render } from '@testing-library/react';
function renderWithWrapper(component, options) {
const Wrapper = ({ children }) => (
<ThemeProvider theme={TEST_THEME}>
{children}
</ThemeProvider>
);
return render(component, { wrapper: Wrapper, ...options });
} |
In case someone stumble upon this thread... the |
Hello, I've jst came across with the following solution, I'm using Styled Components 5.3.3, this is a simple example, just to test the snapshot of a component, but it could be used for any scenario, the trick is to render the component and wrap it with the ThemeProvider, obviously passing through your theme configuration: `import React from 'react'; describe("Component: StyledButton", () => {
})` |
It works!!! |
I just created a function for that instead 🤷 import "@testing-library/jest-dom";
import { render } from "@testing-library/react";
import { ThemeProvider } from "styled-components";
import { Themes } from "@theme/Theme";
import { ITheme } from "@abstractions/theme/ITheme";
const TEST_THEME = Themes.light;
export function renderWithTheme(children: React.ReactElement, theme?: ITheme) {
return render(<ThemeProvider theme={theme ? theme : TEST_THEME}>{children}</ThemeProvider>);
} |
@abdulrahimiliasu This will make it better as you now use the wrapper option of testing-lib and snapshots will be rendered without the theme provider import "@testing-library/jest-dom";
import { render, RenderOptions } from "@testing-library/react";
import { ThemeProvider } from "styled-components";
import { Themes } from "@theme/Theme";
import { ITheme } from "@abstractions/theme/ITheme";
const TEST_THEME = Themes.light;
function createWrapper(theme?: ITheme) {
return function Wrapper({ children }: PropsWithChildren): ReactElement | null {
return <ThemeProvider theme={theme ? theme : TEST_THEME}>{children}</ThemeProvider>;
}
}
interface RenderWithThemeOptions<
Q extends Queries = typeof queries,
Container extends Element | DocumentFragment = HTMLElement,
BaseElement extends Element | DocumentFragment = Container,
> extends RenderOptions<Q, Container, BaseElement> {
theme?: ITheme
}
export function renderWithTheme(children: React.ReactElement, options?: RenderWithThemeOptions) {
const { theme, ...rest } = { ...options };
return render(children, { wrapper: createWrapper(theme), ...rest });
} |
I have been reading the test utilities section of the documentation but I didn't find any information on how to test a component with Jest that is wrapped inside a ThemeProvider. The only solution I have found is to pass a theme props to the component, but this imply to pass it also to all the component children and I would like to avoid it.
Here is my SO question with details on my case.
Thanks
The text was updated successfully, but these errors were encountered: