# End-to-end-testning i React Native

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

## Skriv plattformsoberoende end-to-end-tester i Javascript

Att skriva tester för koden är numera en självklar del av programmering. Det finns olika typer av testmetoder för mjukvara, till exempel *enhetstester, integrationstester *och* end-to-end-tester. D*en här artikeln visar hur vi kan använda **_end-to-end_**-testning i en enkel react-native-app.

Vi kommer att använda biblioteket [**_Detox_**](https://github.com/wix/Detox), som utvecklas och underhålls av teamet på [**_Wix_**](https://github.com/wix).

Appen är väldigt enkel! Den låter användare _gilla_ sin favorit bland Nintendo 64-spelen. Det betyder att det egentligen bara finns ett beteende att testa i appen: _gilla_-funktionen!

### User story

Låt oss lista alla användarhandlingar som kan finnas för den här funktionen:

1. Användaren öppnar appen.

1. Användaren ser spelkortet, och spelkortet har en _gilla_-knapp och en text med det _totala_ *antalet gillningar *. (För att kunna jämföra texten efter användarens handling)

1. Användaren trycker på _gilla_-knappen.

1. Användaren märker att _gilla_-texten har ändrats. I det här fallet har antalet gillningar ökat med ett.

> FÖRBEHÅLL: I teorin kan den här user storyn ses som ett integrationstest. Men eftersom appen bara ansvarar för att låta användare gilla sitt spel behandlas den som end-to-end-testning.

### **Kod**

Vi skriver koden medan vi går igenom user stories i tur och ordning. Varje kodblock kommer till slut att motsvara en specifik user story.

1. **Användaren öppnar appen.**

```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. **Användaren ser spelkortet, och spelkortet har en gilla-knapp och en text med det totala antalet gillningar. (För att kunna jämföra texten efter användarens handling)**

```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.**Användaren trycker på gilla-knappen.**

```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. **Användaren märker att _gilla_-texten har ändrats. I det här fallet har antalet gillningar ökat med ett.**

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

### **Hela koden**

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

Låt oss se allt i praktiken.

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

---

### Slutsats

Den här artikeln har gått igenom grunderna i end-to-end-testning av react-native-appar. För enkelhetens skull är appen tillräckligt enkel för att visa de grundläggande delarna av end-to-end-testning. I verkliga appar och produktionsappar kan det förstås bli betydligt mer invecklat än så här.

Du kan titta på appens slutliga källkod via länken nedan.

Ha en buggfri dag allihop!

[Källkod på Github](https://github.com/tarikfp/react-native-n64-voter-app)

### Publicerad på

Den här artikeln är också publicerad på:

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

