# React Native-এ এন্ড-টু-এন্ড টেস্টিং

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

## Javascript দিয়ে ক্রস-প্ল্যাটফর্ম এন্ড-টু-এন্ড টেস্ট লিখুন

কোডের জন্য টেস্ট লেখা আজকাল প্রোগ্রামিংয়ের একটি অপরিহার্য অংশ। সফটওয়্যার টেস্টিংয়ের নানা পদ্ধতি আছে, যেমন *ইউনিট টেস্ট, ইন্টিগ্রেশন টেস্ট* এবং *এন্ড-টু-এন্ড টেস্ট*। এই আর্টিকেলে দেখাব কীভাবে একটি সাধারণ react-native অ্যাপে **_এন্ড-টু-এন্ড_** টেস্টিং প্রয়োগ করা যায়।

আমরা [**_Detox_**](https://github.com/wix/Detox) লাইব্রেরি ব্যবহার করব, যেটি [**_Wix_**](https://github.com/wix) টিম তৈরি করেছে এবং রক্ষণাবেক্ষণ করে।

অ্যাপটি একদম সোজাসাপ্টা! এটি ব্যবহারকারীদের তাদের প্রিয় Nintendo 64 গেমে _লাইক_ দিতে দেয়। তাই অ্যাপে টেস্ট করার মতো আসলে একটিই নির্দিষ্ট আচরণ আছে: _লাইক_ ফাংশনালিটি!

### ইউজার স্টোরি

চলুন এই ফাংশনালিটির জন্য ব্যবহারকারীর সম্ভাব্য সব অ্যাকশন তালিকা করি:

1. ব্যবহারকারী অ্যাপটি খোলেন।

1. ব্যবহারকারী গেম কার্ডটি দেখেন, এবং কার্ডে একটি _লাইক_ বাটন ও _মোট_ লাইকের সংখ্যার একটি টেক্সট থাকে। (ব্যবহারকারীর অ্যাকশনের পরে টেক্সট তুলনা করার জন্য)

1. ব্যবহারকারী _লাইক_ বাটনে চাপ দেন।

1. ব্যবহারকারী লক্ষ করেন যে _লাইক_ টেক্সট বদলে গেছে। এই ক্ষেত্রে লাইকের সংখ্যা এক বেড়েছে।

> সতর্কতা: তাত্ত্বিকভাবে এই ইউজার স্টোরিকে একটি ইন্টিগ্রেশন টেস্ট হিসেবে ধরা যায়। তবে অ্যাপটির একমাত্র দায়িত্ব যেহেতু ব্যবহারকারীকে গেমে লাইক দিতে দেওয়া, তাই একে এন্ড-টু-এন্ড টেস্টিং হিসেবেই ধরা হয়েছে।

### **কোড**

ইউজার স্টোরিগুলো ক্রমানুসারে দেখতে দেখতে আমরা কোড লিখব। প্রতিটি কোড ব্লক শেষ পর্যন্ত একটি নির্দিষ্ট ইউজার স্টোরির সাথে মিলে যাবে।

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)

---

### উপসংহার

এই আর্টিকেলে react-native অ্যাপে এন্ড-টু-এন্ড টেস্টিংয়ের প্রাথমিক বিষয়গুলো দেখানো হয়েছে। সহজ রাখার জন্য অ্যাপটি এতটাই সাধারণ যে এন্ড-টু-এন্ড টেস্টিংয়ের মৌলিক দিকগুলো দেখানো যায়। বাস্তব বা প্রোডাকশন অ্যাপ্লিকেশনে অবশ্যই বিষয়টি এর চেয়ে অনেক জটিল হতে পারে।

নিচের লিংক থেকে এই অ্যাপের চূড়ান্ত সোর্স কোড দেখে নিতে পারেন।

সবার দিন বাগমুক্ত কাটুক!

[Github সোর্স কোড](https://github.com/tarikfp/react-native-n64-voter-app)

### প্রকাশিত হয়েছে

এই আর্টিকেলটি এখানেও প্রকাশিত হয়েছে:

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

