Skip to main content

Lesson 9 of 10

Mini Project: Tested Todo + Pre-Commit Quality

Add a complete test suite and dev tooling setup to the Todo app from Module 6: RNTL component tests, renderHook store tests, mock AsyncStorage, Detox E2E smoke test, ESLint/Prettier config, Husky pre-commit hook, and .env for API URL.

60 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Test Suite

Test Suite
// __tests__/TodoItem.test.tsx
import { render, screen, userEvent } from '@testing-library/react-native';
import { TodoItem } from '../src/features/todos/components/TodoItem';

const todo = { id: '1', text: 'Buy groceries', done: false, createdAt: Date.now() };

describe('TodoItem', () => {
  it('renders the todo text', () => {
    render(<TodoItem todo={todo} onToggle={jest.fn()} onDelete={jest.fn()} />);
    expect(screen.getByText('Buy groceries')).toBeTruthy();
  });

  it('calls onToggle with the todo id when checkbox pressed', async () => {
    const onToggle = jest.fn();
    render(<TodoItem todo={todo} onToggle={onToggle} onDelete={jest.fn()} />);

    const user = userEvent.setup();
    await user.press(screen.getByRole('checkbox', { name: 'Buy groceries' }));

    expect(onToggle).toHaveBeenCalledWith('1');
  });

  it('applies strikethrough style when done', () => {
    render(<TodoItem todo={{ ...todo, done: true }} onToggle={jest.fn()} onDelete={jest.fn()} />);
    const text = screen.getByText('Buy groceries');
    expect(text.props.style).toMatchObject(
      expect.arrayContaining([expect.objectContaining({ textDecorationLine: 'line-through' })])
    );
  });

  it('calls onDelete when delete button pressed', async () => {
    const onDelete = jest.fn();
    render(<TodoItem todo={todo} onToggle={jest.fn()} onDelete={onDelete} />);

    const user = userEvent.setup();
    await user.press(screen.getByLabelText('Delete Buy groceries'));

    expect(onDelete).toHaveBeenCalledWith('1');
  });
});

// __tests__/todoStore.test.ts
import { renderHook, act } from '@testing-library/react-native';
import { useTodoStore } from '../src/features/todos/store/todoStore';

describe('useTodoStore', () => {
  beforeEach(() => useTodoStore.setState({ todos: [], filter: 'all' }));

  it('adds a todo', () => {
    const { result } = renderHook(() => useTodoStore());
    act(() => result.current.addTodo('Write tests'));
    expect(result.current.todos).toHaveLength(1);
    expect(result.current.todos[0].text).toBe('Write tests');
    expect(result.current.todos[0].done).toBe(false);
  });

  it('toggles a todo', () => {
    const { result } = renderHook(() => useTodoStore());
    act(() => result.current.addTodo('Read book'));
    const id = result.current.todos[0].id;
    act(() => result.current.toggleTodo(id));
    expect(result.current.todos[0].done).toBe(true);
  });

  it('clears completed todos', () => {
    const { result } = renderHook(() => useTodoStore());
    act(() => { result.current.addTodo('Done item'); result.current.addTodo('Active item'); });
    const doneId = result.current.todos[0].id;
    act(() => result.current.toggleTodo(doneId));
    act(() => result.current.clearCompleted());
    expect(result.current.todos).toHaveLength(1);
    expect(result.current.todos[0].text).toBe('Active item');
  });
});

// Run tests: npm test
// Run with coverage: npm test -- --coverage

Tip

Tip

Practice Mini Project Tested Todo PreCommit Quality in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Complete test suite featuring unit tests, RNTL integration, and Detox E2E

Practice Task

Note

Practice Task - (1) Write a working example of Mini Project Tested Todo PreCommit Quality 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.

Quick Quiz

Common Mistake

Warning

A common mistake with Mini Project Tested Todo PreCommit Quality 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