how to use dive in wrapper function to enzyme











up vote
1
down vote

favorite
1












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;
};


enter image description here



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);
});









share|improve this question
























  • 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















up vote
1
down vote

favorite
1












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;
};


enter image description here



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);
});









share|improve this question
























  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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;
};


enter image description here



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);
});









share|improve this question















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;
};


enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











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',
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%2f53199169%2fhow-to-use-dive-in-wrapper-function-to-enzyme%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini