-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 component methods #208
Comments
@mxstbr |
That is so nice, thanks for the fast response @ljharb! |
Dumb question: is there a new way to do this? When I call
|
@machineghost that's the console inspection which shows enumerable properties. Try actually checking |
Thanks for the quick response, but it's not there for me. I get this:
The real code is something like:
P.S. I know the message says it's not a function (implying that it might be an object or something), but if I |
Hmm - what version of enzyme and React are you using? |
|
And how do you import "shallowRender"? |
Oh dear lord I'm a moron. There was a mistake in my import, and I was actually using a different Thank you SO much, and sorry for the stupidity. |
Hi I have tried below solution for calling methods of component and it is working fine for components having state. But, I have one stateless component and I want to test one method of that component ans I am getting below error. //Error
` // Stateless component method
// Testcase
Please guide where i am doing mistake. Thank you, |
Stateless components can't possibly have any methods, because they're not instances. That's just a function closed inside the SFC, and it's impossible to invoke it from tests unless you extract it from the rendered jsx. |
Thanks for guidance. |
I'm having trouble when I try to get the wrapper instance for a component which is written using class MyComponent extends Component {
myMethod() {
// ...
}
}
// on test files
const wrapper = shallow( <MyComponent /> ).instance();
wrapper.myMethod() // -> undefined In short, the wrapper instance doesn't have the methods of the Component. |
@retrofox that seems to work - if it didn't have the method, it would throw a TypeError. Returning |
Nop. The method is not defined.
|
@retrofox what does |
sorry @aweary. I was wrapping the MyCompnent component with a translation helper. I don't know exactly what is the issue in our code but definitely I'll have to dive on this. |
@ljharb I saw this in one of your comments above. I am working on a similar issue: I tried to do the following
(I might have to setState explicitly here before component.update()? But I am testing for the value itself. What's the right way to test this? ) |
@smoholkar i'll have to see the implementation of |
@ljharb It's huge but this is the skeleton of it :
|
It seems like you want to |
Hey guys, I have the problem that i mounted a redux connected component. When i call .instance() on it i do not have access to the class functions. |
@HelgeWieding use |
Hi guys, I use mocha-enzyme-chai-sinon to testing the react router v4. It's really challenging for me to write a unit test for router v4. So I need to test is redirect really. You can check my routes.js http://stackoverflow.com/questions/43604837/react-router-v4-unit-test `
But I catch the error in the console the find is not a function. When I console result, I see what that expect. how to test the [Function: Redirect] - really redirect
|
I solve the main problem, but why I receive the |
@palaniichukdmytro your comments here seem duplicates of your comments on #736. |
ah, yes, that is true. However, (You should also update to enzyme 3 :-) ) |
Is there any good tutorial on how to use all this dive(), find() and instance() stuff? I am scratching my head for hours now to get this work in a non-trivial scenario, where components are wrapped in Redux stores and IntlProviders but I have to call one of their methods. This API is really pretty complex. |
Worked perfectly. Thanks for this. |
how to mock a method of component which uses store. |
@ashraf1994 export your component without a connection |
I don't recommend doing that; if it's never used in production without the connection, it should never be tested without the connection. |
All data in tests should be mocked including store. So this is more easy todo so. Export disconnected component and pass all required props that comes from store and test them. |
It's easier, but it's still wrong. Prefer correctness over convenience. |
You mean I should write tests like
It's wrong. You should not add result as expectancy criteria. Do it like
You are probably talking about an integration tests is used to check that different pieces of the system work together. Integration tests cover whole applications. But this is not the right place to discuss. Peace 📣 |
@ljharb let's assume for a minute that this is the given state of the component. class Button extends React.Component {
handleClick = () => {
// make props.answer = 2 through magic
}
render = () => (
<div onClick={this.handleClick}/>
)
} Wouldn't it be better to test by simulating the click if and only if you know it's hooking to the click event? it('should do something after user clicks the button', () => {
const wrapper = render(<Button/>);
wrapper.simulate('click');
expect(props.answer).toEqual(2);
}); The example you provided above makes a lot of sense since you do not know the behaviour to test yet so you have to go back to your implementation details and test it. By going above one more level and simulating the click instead of invoking the method in that specific instance, you can safely refactor class Button extends React.Component {
- handleClick = () => {
+ somethingElse = () => {
// make props.answer = 2;
}
render = () => (
- <div onClick={this.handleClick}/>
+ <div onClick={this.somethingElse}/>
)
} If a refactor (no behaviour changes) happens then my test is still fine. Makes sense because I am testing the behaviour of the component (not the method as OP mentioned). There's nothing wrong with your example btw. Yours will be invoking the implementation since you do not know which one |
@retrofox i don't know if you resolved the issue - a year later lol but i had to dive() into my wrapper before executing the instance, after that my class methods were available |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
How to test below method function getDateTimeFormatted(value){
if (typeof value == "undefined") { return "" ; }
return moment.utc(value.valueOf()).locale("en").format("MM/DD/YYYY hh:mm:ss A");
} |
@subrat7 this article would clarify out things about mocking date and |
But where is your definition of store? Would love to know how that looks... |
what is instance() |
@42b883 an enzyme method that exposes the underlying class component instance. |
Thx
…On Mon, May 27, 2019, 21:01 Jordan Harband ***@***.***> wrote:
@42b883 <https://github.com/42b883> an enzyme method that exposes the
underlying class component instance.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#208>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AKPNB3IIEITLH43IM2WV7ELPXPZTRANCNFSM4B37MCTA>
.
|
|
You should be able to access methods without calling |
Most probably its because with React 16 and above, instance() returns null for stateless functional components. |
Also, in that case there is no instance conceptually - closed-over variables can’t be accessed from outside the scope they’re defined in unless they’re exposed explicitly. enzyme can’t violate how JavaScript itself works. |
I couldn't find any information on this, if this is a duplicate excuse me for stealing your time.
I'm trying to test this application, but I ran into a wall when testing component methods. As an example, take this component:
How could I test the
handleClick
method here when shallowly rendering this component? (The same question applies to lifecycle methods)The text was updated successfully, but these errors were encountered: