concept handleAddItem in category javascript

appears as: hdleAddItem
Testing JavaScript Applications

This is an excerpt from Manning's book Testing JavaScript Applications.

In your domController.js file, create a function named handleAddItem. This function will receive an event as its first argument, retrieve the submitted values, call addItem to update the inventory, and then updateItemList to update the DOM.

Because this test directly invokes handleAddItem, it doesn’t care about whether it’s attached to the form as a listener for the submit event. If you try, for example, removing from main.js the call to addEventListener, this test will continue to pass. Again, you’ve found another case in which your application won’t work, but in which your tests will pass.

Figure 6.4. The test for handleAddItem invokes it directly, causing it to update the document.
06 04

Constructing events manually, as you’ve just done, can be useful to iterate quickly, and test your listeners in isolation as you build them. But, when it comes to creating reliable guarantees, this technique is inadequate. This unit test only covers the handleAddItem function itself and, therefore, can’t guarantee that the application will work when users trigger real events.