JEST component testing

谁说我不能喝 提交于 2019-11-29 17:37:37

It's hard to tell how your code will behave without seeing the code for your LogOutButton component. But here are a couple of pointers:

First of all, note that your test simulates a 'ckick' event and not a click event as your probably meant.

Second point is that .html is a function so you should add parentheses to it in your test: .html().

Third point is that the call to homeWrapper.update() seems redundant.

Lastly and most importantly, even if your component works as intended, the result of clicking the LogOutButton would be a page redirect to https://www.MICROSOFT.com.

Even if the redirect worked - which I don't think it will, since your test does not run in a real browser - there's no reason to expect that a call to homeWrapper.html() would return the URL. It would return the HTML that makes up the rendered component.

In your case, you can expect the call to homeWrapper.html() to return something like:

<div><h1>Hello world! The result is: 0</h1><input type=\"text\"/>+<input type=\"text\"/>= <br/><button>Add</button><br/><br/><button id=\"x\">LogOut</button></div>

This of course, does not prove that onLogoutClick worked correctly. Instead, you might consider replacing onLogoutClick with a Jest Mock in your test and then expect the mock to have been called once.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!