# End-to-End Testing di React Native

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

## Menulis end-to-end test lintas platform dengan Javascript

Menulis test untuk kode sudah menjadi bagian penting dari pemrograman saat ini. Ada beberapa jenis metodologi pengujian perangkat lunak, seperti *unit test, integration test,* dan *end-to-end test.* Artikel ini akan membahas bagaimana kita bisa menerapkan **_end-to-end_** testing pada aplikasi react-native sederhana.

Kita akan memakai library [**_Detox_**](https://github.com/wix/Detox) yang dikembangkan dan dikelola oleh tim [**_Wix_**](https://github.com/wix).

Aplikasinya cukup sederhana! Aplikasi ini memungkinkan pengguna memberi _like_ pada game Nintendo 64 favorit mereka. Jadi, sebenarnya hanya ada satu perilaku tertentu yang perlu diuji di aplikasi ini: fungsi _like_!

### User Story

Mari kita daftar semua aksi pengguna yang mungkin untuk fungsi ini:

1. Pengguna membuka aplikasi.

1. Pengguna melihat kartu game, dan kartu game tersebut memiliki tombol _like_ serta teks jumlah _total_ like. (Untuk membandingkan teks setelah aksi pengguna)

1. Pengguna menekan tombol _like_.

1. Pengguna melihat bahwa teks _like_ berubah. Dalam kasus ini, jumlah like bertambah satu.

> CATATAN: Secara teori, user story ini bisa dianggap sebagai integration test. Namun karena aplikasinya sendiri hanya bertugas memungkinkan pengguna memberi like pada game mereka, ini diperlakukan sebagai end-to-end testing.

### **Kode**

Kita akan menulis kode sambil menelusuri user story secara berurutan. Setiap blok kode nantinya akan berkaitan dengan satu user story tertentu.

1. **Pengguna membuka aplikasi.**

```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. **Pengguna melihat kartu game, dan kartu game tersebut memiliki tombol like serta teks jumlah total like. (Untuk membandingkan teks setelah aksi pengguna)**

```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. **Pengguna menekan tombol 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. **Pengguna melihat bahwa teks _like_ berubah. Dalam kasus ini, jumlah like bertambah satu.**

```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()
  );
});
```

### **Kode lengkap**

```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**

Mari kita lihat semuanya berjalan.

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

---

### Kesimpulan

Artikel ini membahas dasar-dasar end-to-end testing di aplikasi react-native. Demi kesederhanaan, aplikasinya dibuat cukup dasar untuk menunjukkan aspek fundamental dari end-to-end testing. Tentu saja, di aplikasi nyata atau production, semuanya bisa jauh lebih rumit dari ini.

Kamu bisa melihat source code akhir aplikasi ini melalui tautan di bawah.

Semoga hari kalian bebas bug!

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

### Diterbitkan di

Artikel ini juga diterbitkan di:

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

