|
| 1 | +import * as React from 'react'; |
| 2 | +import { render, screen, userEvent } from '@testing-library/react-native'; |
| 3 | +import TaskList from '../TaskList'; |
| 4 | +import { addTask, getAllTasks, newTaskTitleAtom, store, tasksAtom } from '../state'; |
| 5 | + |
| 6 | +jest.useFakeTimers(); |
| 7 | + |
| 8 | +test('renders an empty task list', () => { |
| 9 | + render(<TaskList />); |
| 10 | + expect(screen.getByText(/no tasks, start by adding one/i)).toBeOnTheScreen(); |
| 11 | +}); |
| 12 | + |
| 13 | +const INITIAL_TASKS: Task[] = [{ id: '1', title: 'Buy bread' }]; |
| 14 | + |
| 15 | +test('renders a to do list with 1 items initially, and adds a new item', async () => { |
| 16 | + renderWithAtoms(<TaskList />, { |
| 17 | + initialValues: [ |
| 18 | + [tasksAtom, INITIAL_TASKS], |
| 19 | + [newTaskTitleAtom, ''], |
| 20 | + ], |
| 21 | + }); |
| 22 | + |
| 23 | + expect(screen.getByText(/buy bread/i)).toBeOnTheScreen(); |
| 24 | + expect(screen.getAllByTestId('task-item')).toHaveLength(1); |
| 25 | + |
| 26 | + const user = userEvent.setup(); |
| 27 | + await user.type(screen.getByPlaceholderText(/new task/i), 'Buy almond milk'); |
| 28 | + await user.press(screen.getByRole('button', { name: /add task/i })); |
| 29 | + |
| 30 | + expect(screen.getByText(/buy almond milk/i)).toBeOnTheScreen(); |
| 31 | + expect(screen.getAllByTestId('task-item')).toHaveLength(2); |
| 32 | +}); |
| 33 | + |
| 34 | +test('modify store outside of components', () => { |
| 35 | + // Set the initial to do items in the store |
| 36 | + store.set(tasksAtom, INITIAL_TASKS); |
| 37 | + expect(getAllTasks()).toEqual(INITIAL_TASKS); |
| 38 | + |
| 39 | + const NEW_TASK = { id: '2', title: 'Buy almond milk' }; |
| 40 | + addTask(NEW_TASK); |
| 41 | + expect(getAllTasks()).toEqual([...INITIAL_TASKS, NEW_TASK]); |
| 42 | +}); |
0 commit comments