In this issue, the topic we are going to discuss is JavaScript testing to check whether the status and working mode of components are as expected. We will also introduce a testing method that can easily write test cases. The testing mentioned here is of course using automated testing methods, which is an important part of software quality assurance (QA). As far as the Smart Queue introduced in this series of articles is concerned, our test goals include:
For the first goal, just check that the properties of the object after creation are as expected. We have mentioned "conforming to expectations" many times, and Assert is designed for this. Simply put, assertion is to ensure that the tested expression results in "true", otherwise, notify the tester in some way and help him locate the test case where the assertion failed.
The second goal is a little more complicated. Since we hide the sorted queue (_sorted) in the closure during component coding and implementation, it is inaccessible from the outside. There are two methods to consider: (1) Refactor the code to increase the testability of the code. There are also two refactoring methods: (a) Set the debug switch and expose _sorted to the outside when it is turned on; (b) Add independence files, splicing the code in a build manner to finally generate a test version. (2) Test the results of the behavior rather than the process. The former method essentially goes deep into the runtime status of the component, while this method only checks the running results of the component. This issue uses the latter test method, and the first test method is left for interested readers to practice :)
It should be noted that I personally do not agree with the first method a. Why? Let me first talk about the design concept of this task queue:
Source: Alipay UED