# React Native में End-to-End Testing

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

## Javascript में cross-platform end-to-end tests लिखें

आजकल code के लिए tests लिखना programming का एक ज़रूरी हिस्सा बन गया है। Software testing के कई तरीके हैं, जैसे *unit tests, integration tests* और *end-to-end tests*। इस article में हम देखेंगे कि एक simple react-native app पर **_end-to-end_** testing कैसे लागू की जा सकती है।

हम [**_Detox_**](https://github.com/wix/Detox) library इस्तेमाल करेंगे, जिसे [**_Wix_**](https://github.com/wix) team ने बनाया है और वही इसे maintain करती है।

App काफ़ी सीधा है! यह users को अपने पसंदीदा Nintendo 64 app को _like_ करने देता है। यानी app में test करने के लिए असल में एक ही behavior है: _Like_ functionality!

### User Story

इस functionality के लिए user के सारे संभावित actions की list बनाते हैं:

1. User app खोलता है।

1. User game card देखता है, और game card पर एक _like_ button और likes की _total_ संख्या वाला text मौजूद है। (User action के बाद text की तुलना करने के लिए)

1. User _like_ button दबाता है।

1. User देखता है कि _like_ text बदल गया है। इस case में likes की संख्या एक बढ़ गई है।

> CAVEAT: सिद्धांत में इस user story को integration test माना जा सकता है। लेकिन क्योंकि app की ज़िम्मेदारी सिर्फ़ users को अपना game like करने देना है, इसलिए इसे end-to-end testing माना गया है।

### **Code**

हम user stories को क्रम से देखते हुए अपना code तैयार करेंगे। हर code block आख़िर में एक खास user story से जुड़ेगा।

1. **User 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. **User game card देखता है, और game card पर एक like button और likes की total संख्या वाला text मौजूद है। (User action के बाद text की तुलना करने के लिए)**

```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.**User like button दबाता है।**

```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. **User देखता है कि _like_ text बदल गया है। इस case में likes की संख्या एक बढ़ गई है।**

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

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

चलिए सब कुछ चलते हुए देखते हैं।

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

---

### निष्कर्ष

इस article में react-native apps में end-to-end testing की बुनियादी बातें कवर की गईं। सरलता के लिए app इतना basic रखा गया है कि end-to-end testing के मूल पहलू दिखाए जा सकें। असली या production applications में यह इससे कहीं ज़्यादा उलझा हुआ हो सकता है।

नीचे दिए link से आप इस app का final source code देख सकते हैं।

सबका दिन bug-free रहे!

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

### यहाँ भी प्रकाशित

यह article यहाँ भी प्रकाशित है:

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

