how to use dive in wrapper function to enzyme
up vote
1
down vote
favorite
I want to make a wrapper function which could take a component as an argument and return it in rendered from with theme. Like below one. but it continued to throw error wrapper:
error : ShallowWrapper::dive() can only be called on components
beforeEach(() => {
component = shallowWithTheme(<SomeComponent value />);
});
test('The text should be something text', () => {
expect(component.text()).toBe('Something text');
});
Wrapper function shallowWithTheme
export const shallowWithTheme = (children, options) => {
const wrapper = shallow(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options).dive();
return wrapper;
};
Edited
export const shallowWithTheme = (children, options) => {
const wrapper = shallow(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options);
const instance = wrapper.root().instance();
return wrapper.shallow({ context: instance.getChildContext() }).dive();
};
Error:
instance.getChildContext is not a function
Update:
After doing hours of finding and hit and trial I ended up using following solution which I don't think is the right way to do it because you have to import your child component in order to find them and access their property. In previous method which is not working with current version. I was able to dive once from root level. So If I have to run a test on two child component I have to use like
expect(wrapper.find(SearchButton).dive().props().isDisabled).toBe(false);
});
expect(wrapper.find(SearchInput).dive().props().isDisabled).toBe(false);
});
util.js
export const shallowWithTheme = children => (
shallow(children, { theme })
);
test.js
it('should enable the search button while the input is not empty', () => {
const wrapper = shallowWithTheme(<HomepageSearch />);
wrapper.setState({ orderSearch: '111' });
expect(wrapper.find(SearchButton).dive().props().isDisabled).toBe(false);
});
reactjs enzyme styled-components
add a comment |
up vote
1
down vote
favorite
I want to make a wrapper function which could take a component as an argument and return it in rendered from with theme. Like below one. but it continued to throw error wrapper:
error : ShallowWrapper::dive() can only be called on components
beforeEach(() => {
component = shallowWithTheme(<SomeComponent value />);
});
test('The text should be something text', () => {
expect(component.text()).toBe('Something text');
});
Wrapper function shallowWithTheme
export const shallowWithTheme = (children, options) => {
const wrapper = shallow(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options).dive();
return wrapper;
};
Edited
export const shallowWithTheme = (children, options) => {
const wrapper = shallow(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options);
const instance = wrapper.root().instance();
return wrapper.shallow({ context: instance.getChildContext() }).dive();
};
Error:
instance.getChildContext is not a function
Update:
After doing hours of finding and hit and trial I ended up using following solution which I don't think is the right way to do it because you have to import your child component in order to find them and access their property. In previous method which is not working with current version. I was able to dive once from root level. So If I have to run a test on two child component I have to use like
expect(wrapper.find(SearchButton).dive().props().isDisabled).toBe(false);
});
expect(wrapper.find(SearchInput).dive().props().isDisabled).toBe(false);
});
util.js
export const shallowWithTheme = children => (
shallow(children, { theme })
);
test.js
it('should enable the search button while the input is not empty', () => {
const wrapper = shallowWithTheme(<HomepageSearch />);
wrapper.setState({ orderSearch: '111' });
expect(wrapper.find(SearchButton).dive().props().isDisabled).toBe(false);
});
reactjs enzyme styled-components
You want to dive into your children but shallow will not render more than 1 level deep. You'd need to use mount. That said,ThemeProvider
is not a component, it's more of context provider. You can find some workarounds here: github.com/styled-components/styled-components/issues/624
– Mrchief
Nov 8 at 1:55
@Mrchief I have tried the some method described in the github issue you mention in your comment. I found most of the solution is based on the previous version and seem not working with version 4
– Carlos
Nov 8 at 2:00
I have added my question to explain what I have tried and what is current issue I am facing.
– Carlos
Nov 8 at 2:01
Yeah, seems like v4 makes it hard to test. I guess your best bet is to create a mock provider for testing.
– Mrchief
Nov 8 at 2:04
besides it'll not help to solve, it seems to be rootcause: github.com/airbnb/enzyme/issues/1647
– skyboyer
Nov 18 at 12:14
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to make a wrapper function which could take a component as an argument and return it in rendered from with theme. Like below one. but it continued to throw error wrapper:
error : ShallowWrapper::dive() can only be called on components
beforeEach(() => {
component = shallowWithTheme(<SomeComponent value />);
});
test('The text should be something text', () => {
expect(component.text()).toBe('Something text');
});
Wrapper function shallowWithTheme
export const shallowWithTheme = (children, options) => {
const wrapper = shallow(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options).dive();
return wrapper;
};
Edited
export const shallowWithTheme = (children, options) => {
const wrapper = shallow(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options);
const instance = wrapper.root().instance();
return wrapper.shallow({ context: instance.getChildContext() }).dive();
};
Error:
instance.getChildContext is not a function
Update:
After doing hours of finding and hit and trial I ended up using following solution which I don't think is the right way to do it because you have to import your child component in order to find them and access their property. In previous method which is not working with current version. I was able to dive once from root level. So If I have to run a test on two child component I have to use like
expect(wrapper.find(SearchButton).dive().props().isDisabled).toBe(false);
});
expect(wrapper.find(SearchInput).dive().props().isDisabled).toBe(false);
});
util.js
export const shallowWithTheme = children => (
shallow(children, { theme })
);
test.js
it('should enable the search button while the input is not empty', () => {
const wrapper = shallowWithTheme(<HomepageSearch />);
wrapper.setState({ orderSearch: '111' });
expect(wrapper.find(SearchButton).dive().props().isDisabled).toBe(false);
});
reactjs enzyme styled-components
I want to make a wrapper function which could take a component as an argument and return it in rendered from with theme. Like below one. but it continued to throw error wrapper:
error : ShallowWrapper::dive() can only be called on components
beforeEach(() => {
component = shallowWithTheme(<SomeComponent value />);
});
test('The text should be something text', () => {
expect(component.text()).toBe('Something text');
});
Wrapper function shallowWithTheme
export const shallowWithTheme = (children, options) => {
const wrapper = shallow(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options).dive();
return wrapper;
};
Edited
export const shallowWithTheme = (children, options) => {
const wrapper = shallow(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options);
const instance = wrapper.root().instance();
return wrapper.shallow({ context: instance.getChildContext() }).dive();
};
Error:
instance.getChildContext is not a function
Update:
After doing hours of finding and hit and trial I ended up using following solution which I don't think is the right way to do it because you have to import your child component in order to find them and access their property. In previous method which is not working with current version. I was able to dive once from root level. So If I have to run a test on two child component I have to use like
expect(wrapper.find(SearchButton).dive().props().isDisabled).toBe(false);
});
expect(wrapper.find(SearchInput).dive().props().isDisabled).toBe(false);
});
util.js
export const shallowWithTheme = children => (
shallow(children, { theme })
);
test.js
it('should enable the search button while the input is not empty', () => {
const wrapper = shallowWithTheme(<HomepageSearch />);
wrapper.setState({ orderSearch: '111' });
expect(wrapper.find(SearchButton).dive().props().isDisabled).toBe(false);
});
reactjs enzyme styled-components
reactjs enzyme styled-components
edited Nov 14 at 2:17
asked Nov 7 at 22:57
Carlos
2,3641758119
2,3641758119
You want to dive into your children but shallow will not render more than 1 level deep. You'd need to use mount. That said,ThemeProvider
is not a component, it's more of context provider. You can find some workarounds here: github.com/styled-components/styled-components/issues/624
– Mrchief
Nov 8 at 1:55
@Mrchief I have tried the some method described in the github issue you mention in your comment. I found most of the solution is based on the previous version and seem not working with version 4
– Carlos
Nov 8 at 2:00
I have added my question to explain what I have tried and what is current issue I am facing.
– Carlos
Nov 8 at 2:01
Yeah, seems like v4 makes it hard to test. I guess your best bet is to create a mock provider for testing.
– Mrchief
Nov 8 at 2:04
besides it'll not help to solve, it seems to be rootcause: github.com/airbnb/enzyme/issues/1647
– skyboyer
Nov 18 at 12:14
add a comment |
You want to dive into your children but shallow will not render more than 1 level deep. You'd need to use mount. That said,ThemeProvider
is not a component, it's more of context provider. You can find some workarounds here: github.com/styled-components/styled-components/issues/624
– Mrchief
Nov 8 at 1:55
@Mrchief I have tried the some method described in the github issue you mention in your comment. I found most of the solution is based on the previous version and seem not working with version 4
– Carlos
Nov 8 at 2:00
I have added my question to explain what I have tried and what is current issue I am facing.
– Carlos
Nov 8 at 2:01
Yeah, seems like v4 makes it hard to test. I guess your best bet is to create a mock provider for testing.
– Mrchief
Nov 8 at 2:04
besides it'll not help to solve, it seems to be rootcause: github.com/airbnb/enzyme/issues/1647
– skyboyer
Nov 18 at 12:14
You want to dive into your children but shallow will not render more than 1 level deep. You'd need to use mount. That said,
ThemeProvider
is not a component, it's more of context provider. You can find some workarounds here: github.com/styled-components/styled-components/issues/624– Mrchief
Nov 8 at 1:55
You want to dive into your children but shallow will not render more than 1 level deep. You'd need to use mount. That said,
ThemeProvider
is not a component, it's more of context provider. You can find some workarounds here: github.com/styled-components/styled-components/issues/624– Mrchief
Nov 8 at 1:55
@Mrchief I have tried the some method described in the github issue you mention in your comment. I found most of the solution is based on the previous version and seem not working with version 4
– Carlos
Nov 8 at 2:00
@Mrchief I have tried the some method described in the github issue you mention in your comment. I found most of the solution is based on the previous version and seem not working with version 4
– Carlos
Nov 8 at 2:00
I have added my question to explain what I have tried and what is current issue I am facing.
– Carlos
Nov 8 at 2:01
I have added my question to explain what I have tried and what is current issue I am facing.
– Carlos
Nov 8 at 2:01
Yeah, seems like v4 makes it hard to test. I guess your best bet is to create a mock provider for testing.
– Mrchief
Nov 8 at 2:04
Yeah, seems like v4 makes it hard to test. I guess your best bet is to create a mock provider for testing.
– Mrchief
Nov 8 at 2:04
besides it'll not help to solve, it seems to be rootcause: github.com/airbnb/enzyme/issues/1647
– skyboyer
Nov 18 at 12:14
besides it'll not help to solve, it seems to be rootcause: github.com/airbnb/enzyme/issues/1647
– skyboyer
Nov 18 at 12:14
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53199169%2fhow-to-use-dive-in-wrapper-function-to-enzyme%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
You want to dive into your children but shallow will not render more than 1 level deep. You'd need to use mount. That said,
ThemeProvider
is not a component, it's more of context provider. You can find some workarounds here: github.com/styled-components/styled-components/issues/624– Mrchief
Nov 8 at 1:55
@Mrchief I have tried the some method described in the github issue you mention in your comment. I found most of the solution is based on the previous version and seem not working with version 4
– Carlos
Nov 8 at 2:00
I have added my question to explain what I have tried and what is current issue I am facing.
– Carlos
Nov 8 at 2:01
Yeah, seems like v4 makes it hard to test. I guess your best bet is to create a mock provider for testing.
– Mrchief
Nov 8 at 2:04
besides it'll not help to solve, it seems to be rootcause: github.com/airbnb/enzyme/issues/1647
– skyboyer
Nov 18 at 12:14