DOM 操作

另一类通常被认为很难进行测试的功能是直接对DOM进行操作的代码。 Another class of functions that is often considered difficult to test is code that directly manipulates the DOM. Let’s see how we can test the following snippet of jQuery code that listens to a click event, fetches some data asynchronously and sets the content of a span.

displayUser.js

  1. 'use strict';
  2. const $ = require('jquery');
  3. const fetchCurrentUser = require('./fetchCurrentUser.js');
  4. $('#button').click(() => {
  5. fetchCurrentUser(user => {
  6. const loggedText = 'Logged ' + (user.loggedIn ? 'In' : 'Out');
  7. $('#username').text(user.fullName + ' - ' + loggedText);
  8. });
  9. });

接着,我们在__tests__/文件夹下创建一个测试文件:

__tests__/displayUser-test.js

  1. 'use strict';
  2. jest.mock('../fetchCurrentUser');
  3. test('displays a user after a click', () => {
  4. // Set up our document body
  5. document.body.innerHTML =
  6. '<div>' +
  7. ' <span id="username" />' +
  8. ' <button id="button" />' +
  9. '</div>';
  10. // This module has a side-effect
  11. require('../displayUser');
  12. const $ = require('jquery');
  13. const fetchCurrentUser = require('../fetchCurrentUser');
  14. // Tell the fetchCurrentUser mock function to automatically invoke
  15. // its callback with some data
  16. fetchCurrentUser.mockImplementation(cb => {
  17. cb({
  18. fullName: 'Johnny Cash',
  19. loggedIn: true,
  20. });
  21. });
  22. // Use jquery to emulate a click on our button
  23. $('#button').click();
  24. // Assert that the fetchCurrentUser function was called, and that the
  25. // #username span's inner text was updated as we'd expect it to.
  26. expect(fetchCurrentUser).toHaveBeenCalled();
  27. expect($('#username').text()).toBe('Johnny Cash - Logged In');
  28. });

我们mock了 fetchCurrentUser.js的实现,这样我们的测试就不会产生真正的网络请求,而是使用本地mock的数据。 这确保了我们的测试能够在毫秒级完成,而不是秒,并且保证了快速的单元测试迭代速度。

Also, the function being tested adds an event listener on the #button DOM element, so we need to set up our DOM correctly for the test. jsdom and the jest-environment-jsdom package simulate a DOM environment as if you were in the browser. This means that every DOM API that we call can be observed in the same way it would be observed in a browser!

To get started with the JSDOM test environment, the jest-environment-jsdom package must be installed if it’s not already:

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev jest-environment-jsdom
  1. yarn add --dev jest-environment-jsdom
  1. pnpm add --save-dev jest-environment-jsdom

The code for this example is available at examples/jquery.