I recently wrote a series of posts about testing realt applications and phpunit for testing word press plug-ins. Describes how to test and write new code in a testable manner. One of the thorny problems with using tests on the basis of traditional code is that the code is usually written in a way that makes testing more difficult. This article introduces two methods that jQuery is difficult to test. First, we will show how to use mock to artificially separate code from jQuery itself. Then provide an example that includes the code that runs when clicked and the code that creates the Ajax request. The
In the quick introduction unit test of jet mock, a thorny problem is that you can’t write all the code to completely isolate the global system, such as DOM events or word press hooking. The simulation library helps to delete the systems not involved in the current test. For example, you can use 10up\/wp\u mock to replace the word press plug-in API with a test model. This document covers how to test jQuery using jet. Jest is developed by Facebook and is usually used to test realt applications, but it can be used with other test libraries. One of the main functions of jest is the mocking function. The simplest use of a jest simulation is to count the number of times a function is called. If the test is a function that calls another function, you only need to know that the function will be called. The
Let’s take a look at testing a function that takes an array of items and applies the same callback to each function. This snipet contains the updateitems function. Updateports() passes the post array to updatepost(), which is the function to be tested. Later in this article, jQuery. Learn how to test Ajax () calls. Now, I worry about checking whether my updateitems () passes a callback. Now let’s look at the test. I will not provide mock posts at this time. Only an array containing three items is provided, and then it is claimed that three \
The core line here is 4 lines. On that line, jest Use fn() to create a function called \
However, simple refactoring can separate the business logic from the DOM event system. The following is an example of using jQuery to bind to a click event and then add or remove classes based on conditions. You can render the whole or part of the DOM, simulate a click, and then test whether the DOM elements have the correct classes. It tests many things that jQuery is responsible for, not code. Business logic is business, but the event binding and distribution system in jQuery is not. The code fragments shown above do a lot of things that violate the single responsibility principle. Let’s divide it into two functions. One function is