/
index.test.tsx
76 lines (68 loc) · 2.89 KB
/
index.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React from 'react'
import { render, fireEvent } from '@testing-library/react'
import Provider from '@laststance/use-app-state'
import '@testing-library/jest-dom'
import TodoList from './index'
import { AppState } from '../../index'
const initialAppState: AppState = {
todoList: [
{
id: 'TsHx9eEN5Y4A',
bodyText: 'monster',
completed: false
},
{
id: 'ba91OwrK0Dt8',
bodyText: 'boss black',
completed: false
},
{
id: 'QwejYipEf5nk',
bodyText: 'caffe latte',
completed: false
}
]
}
test('should render a ppStated todo values', () => {
const { getByTestId, getAllByTestId } = render(
<Provider appState={initialAppState}>
<TodoList path="/" />
</Provider>
)
expect(getByTestId('todo-list')).toBeInTheDocument()
expect(getByTestId('todo-list').children.length).toBe(3)
expect(Array.isArray(getAllByTestId('todo-item'))).toBe(true)
expect(getAllByTestId('todo-item')[0]).toHaveTextContent('monster')
expect(getAllByTestId('todo-item')[1]).toHaveTextContent('boss black')
expect(getAllByTestId('todo-item')[2]).toHaveTextContent('caffe latte')
})
test('should reflect appState value change to render elements', () => {
const { getByTestId, getAllByTestId } = render(
<Provider appState={initialAppState}>
<TodoList path="/" />
</Provider>
)
// delete first todo
fireEvent.click(getAllByTestId('delete-todo-btn')[0])
expect(getByTestId('todo-list').children.length).toBe(2)
expect(Array.isArray(getAllByTestId('todo-item'))).toBe(true)
expect(getAllByTestId('todo-item')[0]).toHaveTextContent('boss black')
expect(getAllByTestId('todo-item')[1]).toHaveTextContent('caffe latte')
})
test('should click toggle-all-button, then all item being comlete checked true and vice versa', () => {
const { getByTestId, getAllByTestId } = render(
<Provider appState={initialAppState}>
<TodoList path="/" />
</Provider>
)
// toggle on
fireEvent.click(getByTestId('toggle-all-btn'))
expect((getAllByTestId('todo-item-complete-check')[0] as HTMLInputElement).checked).toBe(true) /* eslint-disable-line prettier/prettier */
expect((getAllByTestId('todo-item-complete-check')[1] as HTMLInputElement).checked).toBe(true) /* eslint-disable-line prettier/prettier */
expect((getAllByTestId('todo-item-complete-check')[2] as HTMLInputElement).checked).toBe(true) /* eslint-disable-line prettier/prettier */
// toggle off
fireEvent.click(getByTestId('toggle-all-btn'))
expect((getAllByTestId('todo-item-complete-check')[0] as HTMLInputElement).checked).toBe(false) /* eslint-disable-line prettier/prettier */
expect((getAllByTestId('todo-item-complete-check')[1] as HTMLInputElement).checked).toBe(false) /* eslint-disable-line prettier/prettier */
expect((getAllByTestId('todo-item-complete-check')[2] as HTMLInputElement).checked).toBe(false) /* eslint-disable-line prettier/prettier */
})