# اختبار End-to-End في React Native

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

## اكتب اختبارات end-to-end متعددة المنصات بلغة JavaScript

أصبحت كتابة الاختبارات للكود جزءًا أساسيًا من البرمجة في أيامنا هذه. هناك أنواع مختلفة من منهجيات اختبار البرمجيات، مثل *اختبارات الوحدة (unit tests) واختبارات التكامل (integration tests) واختبارات end-to-end*. يشرح هذا المقال كيف نطبّق اختبار **_end-to-end_** على تطبيق react-native بسيط.

سنستخدم مكتبة [**_Detox_**](https://github.com/wix/Detox) التي يطوّرها ويصونها فريق [**_Wix_**](https://github.com/wix).

التطبيق بسيط جدًا! يتيح للمستخدمين أن يضعوا _إعجابًا_ على لعبتهم المفضلة من Nintendo 64. وبالتالي هناك سلوك واحد محدد نريد اختباره في التطبيق: خاصية _الإعجاب_!

### قصة المستخدم (User Story)

لنعدّد كل الإجراءات التي يمكن أن يقوم بها المستخدم في هذه الخاصية:

1. يفتح المستخدم التطبيق.

1. يرى المستخدم بطاقة اللعبة، وعليها زر _إعجاب_ ونص يعرض _العدد الإجمالي_ للإعجابات. (لنقارن النص بعد إجراء المستخدم)

1. يضغط المستخدم على زر _الإعجاب_.

1. يلاحظ المستخدم أن نص _الإعجابات_ تغيّر. في هذه الحالة يزيد عدد الإعجابات بمقدار واحد.

> تنبيه: يمكن اعتبار قصة المستخدم هذه نظريًا اختبار تكامل. لكن بما أن التطبيق نفسه مسؤول فقط عن السماح للمستخدمين بالإعجاب بلعبتهم، فإننا نعاملها كاختبار end-to-end.

### **الكود**

سنكتب الكود ونحن نمرّ على قصص المستخدم بالترتيب. كل كتلة كود ستقابل في النهاية قصة مستخدم محددة.

1. **يفتح المستخدم التطبيق.**

```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. **يرى المستخدم بطاقة اللعبة، وعليها زر إعجاب ونص يعرض العدد الإجمالي للإعجابات. (لنقارن النص بعد إجراء المستخدم)**

```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. **يضغط المستخدم على زر الإعجاب.**

```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. **يلاحظ المستخدم أن نص _الإعجابات_ تغيّر. في هذه الحالة يزيد عدد الإعجابات بمقدار واحد.**

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

### **الكود الكامل**

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

### **العرض التوضيحي**

لنرَ كل شيء وهو يعمل.

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

---

### الخلاصة

تناول هذا المقال أساسيات اختبار end-to-end في تطبيقات react-native. ولتبسيط الأمور، جاء التطبيق بسيطًا بما يكفي لعرض الجوانب الأساسية لاختبار end-to-end. وبالطبع قد يكون الأمر أعقد من ذلك بكثير في التطبيقات الحقيقية وتطبيقات الإنتاج.

يمكنك الاطلاع على الكود المصدري النهائي لهذا التطبيق عبر الرابط أدناه.

أتمنى لكم جميعًا يومًا خاليًا من الأخطاء!

[الكود المصدري على GitHub](https://github.com/tarikfp/react-native-n64-voter-app)

### منشور أيضًا على

هذا المقال منشور أيضًا على:

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

