Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(cn): translate jest/docs/TutorialjQuery.md #24

Open
wants to merge 1 commit into
base: cn
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions docs/TutorialjQuery.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
id: tutorial-jquery
title: DOM Manipulation
title: 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.
通常认为难以测试的另一类功能是直接操作 DOM 的代码。下面是一段 jQuery 代码,监听一个 click 事件,在回调函数中异步获取一些数据并设置 span 元素中的内容,让我们看看如何测试下面这段代码。

```javascript
// displayUser.js
Expand All @@ -20,7 +20,7 @@ $('#button').click(() => {
});
```

Again, we create a test file in the `__tests__/` folder:
同样,我们在 `__tests__/` 文件夹中创建一个测试文件:

```javascript
// __tests__/displayUser-test.js
Expand All @@ -29,40 +29,40 @@ Again, we create a test file in the `__tests__/` folder:
jest.mock('../fetchCurrentUser');

test('displays a user after a click', () => {
// Set up our document body
// 设置 document body 的内容
document.body.innerHTML =
'<div>' +
' <span id="username" />' +
' <button id="button" />' +
'</div>';

// This module has a side-effect
// 这个模块需要引用依赖
require('../displayUser');

const $ = require('jquery');
const fetchCurrentUser = require('../fetchCurrentUser');

// Tell the fetchCurrentUser mock function to automatically invoke
// its callback with some data
// 告诉 fetchCurrentUser 使用一些数据
// 来模拟函数自动调用其回调函数
fetchCurrentUser.mockImplementation(cb => {
cb({
fullName: 'Johnny Cash',
loggedIn: true,
});
});

// Use jquery to emulate a click on our button
// 使用 jquery 模拟点击按钮
$('#button').click();

// Assert that the fetchCurrentUser function was called, and that the
// #username span's inner text was updated as we'd expect it to.
// 验证 fetchCurrentUser 函数被调用了,并且
// #username span 的内部文本按照预料中的进行了更新。
expect(fetchCurrentUser).toBeCalled();
expect($('#username').text()).toEqual('Johnny Cash - Logged In');
});
```

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. Jest ships with `jsdom` which simulates 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!
上述代码中被测试的函数在 `#button` 这个 DOM 元素上添加了一个事件监听器,因此我们需要为测试正确设置 DOM。Jest 附带了 `jsdom`,它可以像在浏览器中一样模拟 DOM 环境。这意味着我们在测试中调用的每个 DOM API 都可以像在浏览器中一样被观察到!

We are mocking `fetchCurrentUser.js` so that our test doesn't make a real network request but instead resolves to mock data locally. This ensures that our test can complete in milliseconds rather than seconds and guarantees a fast unit test iteration speed.
我们模拟 `fetchCurrentUser.js`,因此我们的测试不会发出真正的网络请求,而是在本地模拟数据。这样可以确保我们的测试可以在几毫秒内完成,而不需要几秒钟,并且可以保证单元测试的快速迭代。

The code for this example is available at [examples/jquery](https://github.com/facebook/jest/tree/master/examples/jquery).
可以在 [examples/jquery](https://github.com/facebook/jest/tree/master/examples/jquery) 上找到此示例的代码。