|
| 1 | +import React from 'react' |
| 2 | +import { render, unmountComponentAtNode } from 'react-dom' |
| 3 | +import { act } from 'react-dom/test-utils' |
| 4 | +import { useSelector } from 'react-redux' |
| 5 | +import Todos from './Todos' |
| 6 | + |
| 7 | +jest.mock('react-redux') |
| 8 | + |
| 9 | +describe('Should test the Todos Component', () => { |
| 10 | + let container = null |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + // setup a DOM element as a render target |
| 14 | + container = document.createElement('div') |
| 15 | + document.body.appendChild(container) |
| 16 | + }) |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + // cleanup on exiting |
| 20 | + unmountComponentAtNode(container) |
| 21 | + container.remove() |
| 22 | + container = null |
| 23 | + }) |
| 24 | + |
| 25 | + it('show empty text when the todo list is empty', () => { |
| 26 | + const todos = [] |
| 27 | + useSelector.mockReturnValue(todos) |
| 28 | + |
| 29 | + act(() => { |
| 30 | + render(<Todos />, container) |
| 31 | + }) |
| 32 | + |
| 33 | + container.querySelectorAll('li') |
| 34 | + expect(container.textContent).toBe('Todo list is empty') |
| 35 | + }) |
| 36 | + |
| 37 | + it('should have one TodoItem in the list', () => { |
| 38 | + const todos = [ |
| 39 | + { |
| 40 | + id: 'todoId', |
| 41 | + name: 'Sample todo', |
| 42 | + text: 'This is for testing todos' |
| 43 | + } |
| 44 | + ] |
| 45 | + |
| 46 | + useSelector.mockReturnValue(todos) |
| 47 | + act(() => { |
| 48 | + render(<Todos />, container) |
| 49 | + }) |
| 50 | + |
| 51 | + const listItems = container.querySelectorAll('li') |
| 52 | + |
| 53 | + // this is not a real id, so only text which can be visible is Delete |
| 54 | + expect(listItems[0].textContent).toBe('Delete') |
| 55 | + }) |
| 56 | +}) |
0 commit comments