# End-to-end testing trong React Native

By Tarik Pinarci (https://tarikpinarci.com). Published 2022-11-08. Canonical: https://tarikpinarci.com/vi/blog/end-to-end-testing-in-react-native

## Viết end-to-end test đa nền tảng bằng Javascript

Ngày nay, viết test cho code đã trở thành một phần không thể thiếu của lập trình. Có nhiều phương pháp kiểm thử phần mềm khác nhau, như *unit test*, *integration test* và *end-to-end test*. Bài viết này sẽ hướng dẫn cách áp dụng **_end-to-end_** testing cho một app react-native đơn giản.

Chúng ta sẽ dùng thư viện [**_Detox_**](https://github.com/wix/Detox), được phát triển và duy trì bởi team [**_Wix_**](https://github.com/wix).

App này khá đơn giản! Nó cho phép người dùng _like_ app Nintendo 64 mà họ yêu thích. Như vậy, trong app thực ra chỉ có một hành vi cụ thể cần test. Đó là chức năng _like_!

### User Story

Hãy liệt kê tất cả những thao tác mà người dùng có thể làm với chức năng này:

1. Người dùng mở app.

1. Người dùng thấy thẻ game, và trên thẻ game có nút _like_ cùng dòng text hiển thị _tổng_ *số lượt like*. (Để so sánh text sau thao tác của người dùng)

1. Người dùng nhấn nút _like_.

1. Người dùng thấy text _like_ đã thay đổi. Trong trường hợp này, số lượt like tăng thêm một.

> LƯU Ý: Về lý thuyết, user story này có thể được xem là một integration test. Tuy nhiên, vì bản thân app chỉ có nhiệm vụ cho phép người dùng like game của họ, nên nó được coi là end-to-end testing.

### **Code**

Chúng ta sẽ viết code trong lúc đi qua từng user story theo thứ tự. Mỗi đoạn code cuối cùng sẽ tương ứng với một user story cụ thể.

1. **Người dùng mở app.**

```tsx
const detox = require("detox");
const config = require("../package.json").detox;

// before doing any test, ensure the app is launched by the device.
beforeAll(async () => {
  await detox.init(config);
  await device.launchApp();
});
```

2. **Người dùng thấy thẻ game, và trên thẻ game có nút like cùng dòng text hiển thị tổng số lượt like. (Để so sánh text sau thao tác của người dùng)**

```tsx
it("Game card has the like text and button are available on the screen", async () => {
  // test if the elements are on the screen
  await expect(element(by.id("game-card-like-text-1"))).toExist();
  await expect(element(by.id("game-card-like-button-1"))).toExist();

  // test game card has the like text available on the screen
  const likeTextAttrs = await element(
    by.id("game-card-like-text-1")
  ).getAttributes();

  await expect(element(by.text(likeTextAttrs.text))).toExist();
});
```

3.**Người dùng nhấn nút like.**

```tsx
it("pressing the like button", async () => {
  // Tap to the like button to increase like count
  await element(by.id("game-card-like-button-1")).tap();
});
```

4. **Người dùng thấy text _like_ đã thay đổi. Trong trường hợp này, số lượt like tăng thêm một.**

```tsx
it("pressing the like button of the first game card should increase the like count by one", async () => {
  // get like text(like count) before the like button is "tapped"
  const beforeLikeTextAttrs = await element(
    by.id("game-card-like-text-1")
  ).getAttributes();

  // Tap to the like button to increase like count
  await element(by.id("game-card-like-button-1")).tap();

  // calculate final like text(count)
  const increasedLike = Number(beforeLikeTextAttrs.text) + 1;

  // get updated like text
  const afterLikeText = await element(
    by.id("game-card-like-text-1")
  ).getAttributes();

  // test whether the like text(count) has been increased
  await expect(element(by.text(afterLikeText.text.toString()))).toHaveText(
    increasedLike.toString()
  );
});
```

### **Code đầy đủ**

```tsx
// For more info on how to write Detox tests, see the official docs:
// https://github.com/wix/Detox/blob/master/docs/README.md

const { reloadApp } = require("./reload");

describe("Example", () => {
  beforeEach(async () => {
    await reloadApp();
  });

  it("First game card has the like text and button are available on the screen", async () => {
    // tests if the elements are on the screen
    await expect(element(by.id("game-card-like-text-1"))).toExist();
    await expect(element(by.id("game-card-like-button-1"))).toExist();

    // test game card has the like text available on the screen
    const likeTextAttrs = await element(
      by.id("game-card-like-text-1")
    ).getAttributes();

    await expect(element(by.text(likeTextAttrs.text))).toExist();
  });

  it("pressing the like button of the first game card should increase the like count by one", async () => {
    // get like text(like count) before the like button is "tapped"
    const beforeLikeTextAttrs = await element(
      by.id("game-card-like-text-1")
    ).getAttributes();

    // Tap to the like button to increase like count
    await element(by.id("game-card-like-button-1")).tap();

    // calculate final like text(count)
    const increasedLike = Number(beforeLikeTextAttrs.text) + 1;

    // get updated like text
    const afterLikeText = await element(
      by.id("game-card-like-text-1")
    ).getAttributes();

    // test whether the like text(count) has been increased
    await expect(element(by.text(afterLikeText.text.toString()))).toHaveText(
      increasedLike.toString()
    );
  });
});
```

### **Demo**

Cùng xem mọi thứ chạy thực tế nhé.

![](https://cdn-images-1.medium.com/max/2000/1*p9YIVHUK5tLqVmMVPI67Tg.gif)

---

### Kết luận

Bài viết này đã đi qua những kiến thức cơ bản về end-to-end testing trong app react-native. Để cho đơn giản, bản thân app đủ cơ bản để minh họa những khía cạnh nền tảng của end-to-end testing. Tất nhiên, trong các ứng dụng thực tế hay production, mọi thứ có thể phức tạp hơn nhiều.

Bạn có thể xem source code hoàn chỉnh của app này qua đường link bên dưới.

Chúc mọi người một ngày không bug!

[Source code trên Github](https://github.com/tarikfp/react-native-n64-voter-app)

### Đã đăng trên

Bài viết này cũng được đăng trên:

- [Medium](https://medium.com/gitconnected/end-to-end-testing-in-react-native-79d23dbf0168)

