Skip to main content

Lesson 10 of 10

Practice & Quiz - Testing & Tooling

Test your knowledge of React Native Testing Library, Detox E2E, Sentry crash reporting, and Flipper debugging with a practical exercise and 5 quiz questions.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise - Testing a Navigation-Aware Component

Practical Exercise - Testing a Navigation-Aware Component
// ─── screens/__tests__/LoginScreen.test.tsx ──────────────────
import React from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { LoginScreen } from '../LoginScreen';
import { server } from '../../__mocks__/server'; // MSW server
import { http, HttpResponse } from 'msw';

// ─── Navigation wrapper ───────────────────────────────────────
const Stack = createNativeStackNavigator();
function TestNavigator({ initialRoute = 'Login' }: { initialRoute?: string }) {
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName={initialRoute}>
        <Stack.Screen name="Login" component={LoginScreen} />
        <Stack.Screen name="Home" component={() => <></>} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

// ─── Tests ────────────────────────────────────────────────────
describe('LoginScreen', () => {
  it('shows validation errors on empty submit', async () => {
    render(<TestNavigator />);

    // RNTL uses accessible queries - find by label, not by testID
    const submitBtn = screen.getByRole('button', { name: /sign in/i });
    fireEvent.press(submitBtn);

    await waitFor(() => {
      expect(screen.getByText('Invalid email')).toBeTruthy();
      expect(screen.getByText('Min 8 characters')).toBeTruthy();
    });
  });

  it('navigates to Home on successful login', async () => {
    // Override MSW handler for this test
    server.use(
      http.post('/api/auth/login', () =>
        HttpResponse.json({ token: 'test-jwt', user: { id: '1', name: 'Alice' } })
      )
    );

    render(<TestNavigator />);

    fireEvent.changeText(
      screen.getByLabelText('Email address'),
      'alice@example.com'
    );
    fireEvent.changeText(
      screen.getByLabelText('Password'),
      'SecurePass1!'
    );
    fireEvent.press(screen.getByRole('button', { name: /sign in/i }));

    // Wait for async login + navigation
    await waitFor(() => {
      // Home screen is now rendered
      expect(screen.queryByText(/sign in/i)).toBeNull();
    }, { timeout: 3000 });
  });

  it('shows error message when login fails', async () => {
    server.use(
      http.post('/api/auth/login', () =>
        HttpResponse.json({ error: 'Invalid credentials' }, { status: 401 })
      )
    );

    render(<TestNavigator />);
    fireEvent.changeText(screen.getByLabelText('Email address'), 'bad@bad.com');
    fireEvent.changeText(screen.getByLabelText('Password'), 'wrongpassword1');
    fireEvent.press(screen.getByRole('button', { name: /sign in/i }));

    await waitFor(() => {
      expect(screen.getByText(/invalid credentials/i)).toBeTruthy();
    });
  });
});

// jest.config.js
// module.exports = {
//   preset: 'react-native',
//   setupFilesAfterFramework: ['./jest.setup.ts'],
//   transformIgnorePatterns: [
//     'node_modules/(?!(react-native|@react-native|@react-navigation|expo|@expo)/)'
//   ]
// };

Module Quiz

Module Quiz
Question 1 of 5

Why must React Navigation's NavigationContainer be included in test renders?

Tip

Tip

Practice Practice Quiz Testing Tooling in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Module 11 Knowledge Check: Jest, RNTL, Detox, Sentry crash symbolication, and Git hooks

Practice Task

Note

Practice Task - (1) Write a working example of Practice Quiz Testing Tooling from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Practice Quiz Testing Tooling is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.

Topics in This Module

0/10 done