Unit test for compose HOC
How can I test this HOC with Enzyme? I've tried shallow but it covers only 40% of component.
export default compose(
withState('showDropdown', 'handleDropdown', false),
withHandlers({
handleClickOutside: props => () => {
props.handleDropdown(false)
},
menuItemClick: props => () => {
props.handleDropdown(false)
},
}),
onClickOutside,
)(AccountDropdown)
this is code of my test
const props = {
showDropdown: false,
handleDropdown: jest.fn(),
menuItemClick: jest.fn(),
onLogout: jest.fn(),
user: {},
}
const output = mount(<AccountDropdown
showDropdown={props.showDropdown}
handleDropdown={props.handleDropdown}
menuItemClick={props.menuItemClick}
onLogout={props.onLogout}
user={props.user}
/>)
console.log('output', output)
const TogleDropdown = output.find('TogleDropdown')
console.log('TogleDropdown', TogleDropdown)
expect(shallowToJson(output)).toMatchSnapshot()
also i can't test events becouse console.log output:
output ReactWrapper {}
reactjs recompose
|
show 1 more comment
How can I test this HOC with Enzyme? I've tried shallow but it covers only 40% of component.
export default compose(
withState('showDropdown', 'handleDropdown', false),
withHandlers({
handleClickOutside: props => () => {
props.handleDropdown(false)
},
menuItemClick: props => () => {
props.handleDropdown(false)
},
}),
onClickOutside,
)(AccountDropdown)
this is code of my test
const props = {
showDropdown: false,
handleDropdown: jest.fn(),
menuItemClick: jest.fn(),
onLogout: jest.fn(),
user: {},
}
const output = mount(<AccountDropdown
showDropdown={props.showDropdown}
handleDropdown={props.handleDropdown}
menuItemClick={props.menuItemClick}
onLogout={props.onLogout}
user={props.user}
/>)
console.log('output', output)
const TogleDropdown = output.find('TogleDropdown')
console.log('TogleDropdown', TogleDropdown)
expect(shallowToJson(output)).toMatchSnapshot()
also i can't test events becouse console.log output:
output ReactWrapper {}
reactjs recompose
have you useddive()
in your test?
– skyboyer
Nov 13 '18 at 12:26
i tried but it didnt help
– Эмиль Кулуев
Nov 15 '18 at 13:01
can you add test's code?
– skyboyer
Nov 15 '18 at 13:26
added test's code
– Эмиль Кулуев
Nov 16 '18 at 11:17
what'sonClickOutside
?
– Mihai Răducanu
Nov 20 '18 at 18:42
|
show 1 more comment
How can I test this HOC with Enzyme? I've tried shallow but it covers only 40% of component.
export default compose(
withState('showDropdown', 'handleDropdown', false),
withHandlers({
handleClickOutside: props => () => {
props.handleDropdown(false)
},
menuItemClick: props => () => {
props.handleDropdown(false)
},
}),
onClickOutside,
)(AccountDropdown)
this is code of my test
const props = {
showDropdown: false,
handleDropdown: jest.fn(),
menuItemClick: jest.fn(),
onLogout: jest.fn(),
user: {},
}
const output = mount(<AccountDropdown
showDropdown={props.showDropdown}
handleDropdown={props.handleDropdown}
menuItemClick={props.menuItemClick}
onLogout={props.onLogout}
user={props.user}
/>)
console.log('output', output)
const TogleDropdown = output.find('TogleDropdown')
console.log('TogleDropdown', TogleDropdown)
expect(shallowToJson(output)).toMatchSnapshot()
also i can't test events becouse console.log output:
output ReactWrapper {}
reactjs recompose
How can I test this HOC with Enzyme? I've tried shallow but it covers only 40% of component.
export default compose(
withState('showDropdown', 'handleDropdown', false),
withHandlers({
handleClickOutside: props => () => {
props.handleDropdown(false)
},
menuItemClick: props => () => {
props.handleDropdown(false)
},
}),
onClickOutside,
)(AccountDropdown)
this is code of my test
const props = {
showDropdown: false,
handleDropdown: jest.fn(),
menuItemClick: jest.fn(),
onLogout: jest.fn(),
user: {},
}
const output = mount(<AccountDropdown
showDropdown={props.showDropdown}
handleDropdown={props.handleDropdown}
menuItemClick={props.menuItemClick}
onLogout={props.onLogout}
user={props.user}
/>)
console.log('output', output)
const TogleDropdown = output.find('TogleDropdown')
console.log('TogleDropdown', TogleDropdown)
expect(shallowToJson(output)).toMatchSnapshot()
also i can't test events becouse console.log output:
output ReactWrapper {}
reactjs recompose
reactjs recompose
edited Nov 21 '18 at 20:50
Sergio Moura
4,04311631
4,04311631
asked Nov 13 '18 at 12:20
Эмиль КулуевЭмиль Кулуев
11
11
have you useddive()
in your test?
– skyboyer
Nov 13 '18 at 12:26
i tried but it didnt help
– Эмиль Кулуев
Nov 15 '18 at 13:01
can you add test's code?
– skyboyer
Nov 15 '18 at 13:26
added test's code
– Эмиль Кулуев
Nov 16 '18 at 11:17
what'sonClickOutside
?
– Mihai Răducanu
Nov 20 '18 at 18:42
|
show 1 more comment
have you useddive()
in your test?
– skyboyer
Nov 13 '18 at 12:26
i tried but it didnt help
– Эмиль Кулуев
Nov 15 '18 at 13:01
can you add test's code?
– skyboyer
Nov 15 '18 at 13:26
added test's code
– Эмиль Кулуев
Nov 16 '18 at 11:17
what'sonClickOutside
?
– Mihai Răducanu
Nov 20 '18 at 18:42
have you used
dive()
in your test?– skyboyer
Nov 13 '18 at 12:26
have you used
dive()
in your test?– skyboyer
Nov 13 '18 at 12:26
i tried but it didnt help
– Эмиль Кулуев
Nov 15 '18 at 13:01
i tried but it didnt help
– Эмиль Кулуев
Nov 15 '18 at 13:01
can you add test's code?
– skyboyer
Nov 15 '18 at 13:26
can you add test's code?
– skyboyer
Nov 15 '18 at 13:26
added test's code
– Эмиль Кулуев
Nov 16 '18 at 11:17
added test's code
– Эмиль Кулуев
Nov 16 '18 at 11:17
what's
onClickOutside
?– Mihai Răducanu
Nov 20 '18 at 18:42
what's
onClickOutside
?– Mihai Răducanu
Nov 20 '18 at 18:42
|
show 1 more comment
1 Answer
1
active
oldest
votes
Separate your code.
Give the enhancer one file and the component another.
On the enhancer, export only the generated HOC:
export default compose(
withState('showDropdown', 'handleDropdown', false),
withHandlers({
handleClickOutside: props => () => {
props.handleDropdown(false)
},
menuItemClick: props => () => {
props.handleDropdown(false)
},
}),
onClickOutside,
)
On the other file, export just the AccountDropdown
(without wrapping). Then, somewhere else, export the "combination" of them:
import enhancer from './enhancer.js';
import AccountDropdown from './AccountDropdown.jsx';
export default enhancer(AccountDropdown);
Now, on the tests, import each individual file, "enhance" the component there and find the component properly:
import enhancer from './enhancer.js';
import AccountDropdown from './AccountDropdown.jsx';
const Component = enhancer(AccountDropdown);
const props = {
showDropdown: false,
handleDropdown: jest.fn(),
menuItemClick: jest.fn(),
onLogout: jest.fn(),
user: {},
}
const output = mount(<Component {...props} />)
const ToggleDropdown = output.find(AccountDropdown) // no quotes
your ToggleDropdown
will have the component for you to test. Example:
expect(handleDropdown).to.have.property('callCount', 0);
Note that the mount
will return a ReactWrapper
, according to the docs. You can take a look at the props by using the ToggleDropdown.props()
function.
i have this error: TypeError: Cannot read property 'isReactComponent' of undefined
– Эмиль Кулуев
Nov 23 '18 at 11:11
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%2f53280900%2funit-test-for-compose-hoc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Separate your code.
Give the enhancer one file and the component another.
On the enhancer, export only the generated HOC:
export default compose(
withState('showDropdown', 'handleDropdown', false),
withHandlers({
handleClickOutside: props => () => {
props.handleDropdown(false)
},
menuItemClick: props => () => {
props.handleDropdown(false)
},
}),
onClickOutside,
)
On the other file, export just the AccountDropdown
(without wrapping). Then, somewhere else, export the "combination" of them:
import enhancer from './enhancer.js';
import AccountDropdown from './AccountDropdown.jsx';
export default enhancer(AccountDropdown);
Now, on the tests, import each individual file, "enhance" the component there and find the component properly:
import enhancer from './enhancer.js';
import AccountDropdown from './AccountDropdown.jsx';
const Component = enhancer(AccountDropdown);
const props = {
showDropdown: false,
handleDropdown: jest.fn(),
menuItemClick: jest.fn(),
onLogout: jest.fn(),
user: {},
}
const output = mount(<Component {...props} />)
const ToggleDropdown = output.find(AccountDropdown) // no quotes
your ToggleDropdown
will have the component for you to test. Example:
expect(handleDropdown).to.have.property('callCount', 0);
Note that the mount
will return a ReactWrapper
, according to the docs. You can take a look at the props by using the ToggleDropdown.props()
function.
i have this error: TypeError: Cannot read property 'isReactComponent' of undefined
– Эмиль Кулуев
Nov 23 '18 at 11:11
add a comment |
Separate your code.
Give the enhancer one file and the component another.
On the enhancer, export only the generated HOC:
export default compose(
withState('showDropdown', 'handleDropdown', false),
withHandlers({
handleClickOutside: props => () => {
props.handleDropdown(false)
},
menuItemClick: props => () => {
props.handleDropdown(false)
},
}),
onClickOutside,
)
On the other file, export just the AccountDropdown
(without wrapping). Then, somewhere else, export the "combination" of them:
import enhancer from './enhancer.js';
import AccountDropdown from './AccountDropdown.jsx';
export default enhancer(AccountDropdown);
Now, on the tests, import each individual file, "enhance" the component there and find the component properly:
import enhancer from './enhancer.js';
import AccountDropdown from './AccountDropdown.jsx';
const Component = enhancer(AccountDropdown);
const props = {
showDropdown: false,
handleDropdown: jest.fn(),
menuItemClick: jest.fn(),
onLogout: jest.fn(),
user: {},
}
const output = mount(<Component {...props} />)
const ToggleDropdown = output.find(AccountDropdown) // no quotes
your ToggleDropdown
will have the component for you to test. Example:
expect(handleDropdown).to.have.property('callCount', 0);
Note that the mount
will return a ReactWrapper
, according to the docs. You can take a look at the props by using the ToggleDropdown.props()
function.
i have this error: TypeError: Cannot read property 'isReactComponent' of undefined
– Эмиль Кулуев
Nov 23 '18 at 11:11
add a comment |
Separate your code.
Give the enhancer one file and the component another.
On the enhancer, export only the generated HOC:
export default compose(
withState('showDropdown', 'handleDropdown', false),
withHandlers({
handleClickOutside: props => () => {
props.handleDropdown(false)
},
menuItemClick: props => () => {
props.handleDropdown(false)
},
}),
onClickOutside,
)
On the other file, export just the AccountDropdown
(without wrapping). Then, somewhere else, export the "combination" of them:
import enhancer from './enhancer.js';
import AccountDropdown from './AccountDropdown.jsx';
export default enhancer(AccountDropdown);
Now, on the tests, import each individual file, "enhance" the component there and find the component properly:
import enhancer from './enhancer.js';
import AccountDropdown from './AccountDropdown.jsx';
const Component = enhancer(AccountDropdown);
const props = {
showDropdown: false,
handleDropdown: jest.fn(),
menuItemClick: jest.fn(),
onLogout: jest.fn(),
user: {},
}
const output = mount(<Component {...props} />)
const ToggleDropdown = output.find(AccountDropdown) // no quotes
your ToggleDropdown
will have the component for you to test. Example:
expect(handleDropdown).to.have.property('callCount', 0);
Note that the mount
will return a ReactWrapper
, according to the docs. You can take a look at the props by using the ToggleDropdown.props()
function.
Separate your code.
Give the enhancer one file and the component another.
On the enhancer, export only the generated HOC:
export default compose(
withState('showDropdown', 'handleDropdown', false),
withHandlers({
handleClickOutside: props => () => {
props.handleDropdown(false)
},
menuItemClick: props => () => {
props.handleDropdown(false)
},
}),
onClickOutside,
)
On the other file, export just the AccountDropdown
(without wrapping). Then, somewhere else, export the "combination" of them:
import enhancer from './enhancer.js';
import AccountDropdown from './AccountDropdown.jsx';
export default enhancer(AccountDropdown);
Now, on the tests, import each individual file, "enhance" the component there and find the component properly:
import enhancer from './enhancer.js';
import AccountDropdown from './AccountDropdown.jsx';
const Component = enhancer(AccountDropdown);
const props = {
showDropdown: false,
handleDropdown: jest.fn(),
menuItemClick: jest.fn(),
onLogout: jest.fn(),
user: {},
}
const output = mount(<Component {...props} />)
const ToggleDropdown = output.find(AccountDropdown) // no quotes
your ToggleDropdown
will have the component for you to test. Example:
expect(handleDropdown).to.have.property('callCount', 0);
Note that the mount
will return a ReactWrapper
, according to the docs. You can take a look at the props by using the ToggleDropdown.props()
function.
answered Nov 21 '18 at 20:59
Sergio MouraSergio Moura
4,04311631
4,04311631
i have this error: TypeError: Cannot read property 'isReactComponent' of undefined
– Эмиль Кулуев
Nov 23 '18 at 11:11
add a comment |
i have this error: TypeError: Cannot read property 'isReactComponent' of undefined
– Эмиль Кулуев
Nov 23 '18 at 11:11
i have this error: TypeError: Cannot read property 'isReactComponent' of undefined
– Эмиль Кулуев
Nov 23 '18 at 11:11
i have this error: TypeError: Cannot read property 'isReactComponent' of undefined
– Эмиль Кулуев
Nov 23 '18 at 11:11
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%2f53280900%2funit-test-for-compose-hoc%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
have you used
dive()
in your test?– skyboyer
Nov 13 '18 at 12:26
i tried but it didnt help
– Эмиль Кулуев
Nov 15 '18 at 13:01
can you add test's code?
– skyboyer
Nov 15 '18 at 13:26
added test's code
– Эмиль Кулуев
Nov 16 '18 at 11:17
what's
onClickOutside
?– Mihai Răducanu
Nov 20 '18 at 18:42