Javascript Unit Testing - DOM Manipulation

后端 未结 3 1126
半阙折子戏
半阙折子戏 2021-02-18 19:09

I\'m quite new to Javacript Unit testing. One thing keep bothering me. When testing javascript, we often need to do the DOM manipulation. It looks like I am unit testing a metho

相关标签:
3条回答
  • 2021-02-18 19:41

    I respectfully disagree with @BentOnCoding. Most often a component is more than just its class. Component combines an HTML template and a JavaScript/TypeScript class. That's why you should test that the template and the class work together as intended. The class-only tests can tell you about class behavior. But they cannot tell you if the component is going to render properly and respond to user input.

    Some people say you should test it in integration tests. But integration tests are slower to write/run and more expensive (in terms of time and resources) to run/maintain. So, testing most of your component functionality in integration tests might slow you down.

    It doesn't mean you should skip integration tests. While integration and E2E tests may be slower and expensive than unit tests, they bring you more confidence that your app is working as intended. Integration test is where individual units/components are combined and tested as a group. It shouldn't be considered as an only place to test your component's template.

    0 讨论(0)
  • 2021-02-18 19:53

    I think I'd second @BentOnCoding's recommendation that what you want to unit test is your code, not anything else. When it comes to DOM manipulation, that's browser code, such as appendChild, replaceChild etc. If you're using jQuery or some other library, the same still applies--you're calling some other code to do the manipulation, and you don't need to test that. So how do you assert that calling some function on your viewmodel/controller resulted in the DOM structure that you wanted? You don't. Just as you wouldn't unit test that calling a stored procedure on a DB resulted in a specific row in a specific table. You need to instead think about how to abstract out the parts of your controller that deal with inputs/outputs from the parts that manipulate the DOM. For instance, if you had a method that called alert() based on some conditions, you'd want to separate the method into two:

    • One that takes and processes the inputs
    • One that calls window.alert()

    During the test, you'd substitute window.alert (or your proxy method to it) with a fake (see SinonJS), and call your input processor with the conditions to cause (or not cause) the alert. You can then assert different values on whether the fake was called, how many times, with what values, etc. You don't actually test window.alert() because it's external to your code. It's assumed that those external dependencies work correctly. If they don't, then that's a bug for that library, but it's not your unit test's job to uncover those bugs. You're only interested in verifying your own code.

    0 讨论(0)
  • 2021-02-18 19:59

    One of the toughest parts of javascript unit testing is not the testing, it's learning how to architect your code so that it is testable.

    You need to structure your code with a clear separation of testable logic and DOM manipulation.

    My rule of thumb is this:

    If you are testing anything that is dependent on the DOM structure, then you are doing it wrong.

    In summary:Try to test data manipulations and logical operations only.

    0 讨论(0)
提交回复
热议问题